I have been trying for several days to figure out how to assign a value to a specific cell using VBA, but instead of using the excel coordinates (B4, D9..) or (5.6 - 6.9 - etc.), I use the name of the row and column, that is, use the first row as header, and the first column as index, to be able to dynamically locate the cells that interest me, since they will not always be located at the same coordinates.
That is, instead of doing this:
Range(Cells(4, 5), Cells(8, 9))
Range("C4")
Cells(4, 6).Value = variable_x
Being able to do something like this:
Cells("ID374839", "SALARIO")
Where ID374839 would be the row ID, and "SALARY" the column header.
Thank you very much in advance.
What you want can be achieved by assigning each column to constants and thus dynamically refer to them. I show you with an example:
Now assigning the name of a row to a variable, for example:
This way, we can dynamically reference cells like this:
After a few days I found the most correct way to find a field from the values of the header and the first column, I expose it below:
In the variable "row" I store the index of the row where the worker "Antonio" is located (in this case the name would be his ID). And in the Column variable I store the index of the column where the "Premiums" column is located. In this way I will know Antonio's prime (100) doing, for example, "Cells(row, column).Copy".
The utility is that the workers change and can't always be located in the same row, so I can dynamically locate them and modify the values I need. Later, I use the fetched values to get the value of the located cell and copy it somewhere else.
For more information on how to work with this function, which is called "Range.Find", I recommend the page where I found this solution:
Range.Find in VBA
The Cells object is rarely used as it offers few options. The most logical thing is to use Range. See this article: Object Range
In any case, what you need is this?