I know there are a ton of related questions throughout the site, but for whatever reason I cannot get this macro to produce the correct results… please help!!
Sheets("Interface Catalogue").Select Dim UniqID As String UniqID = Trim(ActiveCell.Value) Sheets("Data Elements").Select LastB = Cells(Rows.Count, "B").End(xlUp).Row LastA = Cells(Rows.Count, "A").End(xlUp).Row If LastA > LastB Then Range("A" & Rows.Count).End(xlUp).Offset(10).Select Selection.Formula = "=HYPERLINK(""#""&CELL(""address"",INDEX('Interface Catalogue'!F:F,MATCH(""& UniqID &"",'Interface Catalogue'!F:F,0))),""& UniqID &"")" Else Range("B" & Rows.Count).End(xlUp).Offset(2).Previous.Select Selection.Formula = "=HYPERLINK(""#""&CELL(""address"",INDEX('Interface Catalogue'!F:F,MATCH(""& UniqID &"",'Interface Catalogue'!F:F,0))),""& UniqID &"")" End If End Sub
Running the macro should enter a hyperlink in a cell within the ‘Data Elements’ worksheet (either column A or column B, based on the ‘If’) that links to the value assigned to the UniqID variable (ActiveCell when the Macro is run) found within the ‘Interface Catalogue’ worksheet…
I’m almost certain the problem is occurring in how the Formula is interpreting my Variable (“” & UniqID & “” – – Correct syntax???) … When I hardcode a value in the formula instead of using the UniqID variable, it works. When I debug and check the value assigned to the UniqID variable, it is the ActiveCell value (as expected). What I’m currently getting is a hyperlinked “#N/A” in the appropriate ‘Data Elements’ cell, so the rest of the code appears to be working. The formula simply isn’t finding the value in the ‘Interface Catalogue’ tab (again, it really seems to be a matter of how the variable is being used in the selection.formula statement, because when I hardcode a value there it works).
To be clear, I’m a complete novice in VBA (as in I just started yesterday)… Really just hoping to figure this out. THanks, in advance!!
""& UniqID &"" to """ & UniqID &"""
Two double quotes inside the string translates to a single doublequote. So you need one more to end the string so you can concatenate your variable. Then one more to start the string back up and then two more to turn into a literal “.
That’s confusing, but it makes sense after you wrap your head around it. You could also do:
" & Chr(34) & UniqID & Chr(34) & "
where Chr(34) is a double quote.