adamchater
New Member
- Joined
- Jul 17, 2024
- Messages
- 7
- Office Version
- 365
- 2010
- Platform
- Windows
Hi, I have the following code, which I know is very inefficient and I want to make it much better.
I have a column of dynamic values in cells AO9 to AO104, and what I need to happen is that when one of the cells in that range is clicked on, Excel performs a VLOOKUP against the value in the cell, copies the results of the VLOOKUP to a cell on another sheet, then activates that sheet.
This is achieved currently by using the code below, although the example given only covers cells AO9 and AO10. Each five lines of code would be repeated for all the cells between AO9 to AO104 (I haven't actually done this yet as I know this not a good way to achieve this), and the only thing which would change is that in the second line the range value is increased by 1 each time. The formula that's being referenced in the second line is just a VLOOKUP, which looks up the value of the cell that was clicked on in a range called ConsData2 and returns the value in the 70th column. This value is then copied to another sheet, in cell W19, and that sheet activated.
I know there must be a much more efficient way of doing this, without repeating the section of code for each cell, and also without doing a VLOOKUP for each cell separately too, but I cannot work out how to do this.
Could someone please kindly advise me how to make this better?
I have a column of dynamic values in cells AO9 to AO104, and what I need to happen is that when one of the cells in that range is clicked on, Excel performs a VLOOKUP against the value in the cell, copies the results of the VLOOKUP to a cell on another sheet, then activates that sheet.
This is achieved currently by using the code below, although the example given only covers cells AO9 and AO10. Each five lines of code would be repeated for all the cells between AO9 to AO104 (I haven't actually done this yet as I know this not a good way to achieve this), and the only thing which would change is that in the second line the range value is increased by 1 each time. The formula that's being referenced in the second line is just a VLOOKUP, which looks up the value of the cell that was clicked on in a range called ConsData2 and returns the value in the 70th column. This value is then copied to another sheet, in cell W19, and that sheet activated.
VBA Code:
If Target.address = "$AO$9" Then
CurrentConsultant = Worksheets("Formulas").Range("B256").Value
Worksheets("Consultants").Range("W19") = CurrentConsultant
Sheets("Consultants").Activate
End If
If Target.address = "$AO$10" Then
CurrentConsultant = Worksheets("Formulas").Range("B257").Value
Worksheets("Consultants").Range("W19") = CurrentConsultant
Sheets("Consultants").Activate
End If
I know there must be a much more efficient way of doing this, without repeating the section of code for each cell, and also without doing a VLOOKUP for each cell separately too, but I cannot work out how to do this.
Could someone please kindly advise me how to make this better?