Macro to create HYPERLINK referencing cell to the left

AjNau

New Member
Joined
Sep 13, 2015
Messages
6
I've createed a macro that will insert a new column to the right of the active cell and create a formula in the new column using the HYPERLINK function.

My existing macro works if the spreadsheet columns remain constant, but how can I make the macro more robust so it can work in other spreadsheets with a different column layout?

For example...

ActiveCell is A2

A2 = "0042902"

...the macro creates the following formula in B2, a new column.

B2 = HYPERLINK("http://someurl/query.asp?"&A2,A2)

How can I modify the macro for when ActiveCell is A5 and is "0042904"?

Current code...

Code:
Sub HYPLink()
    ActiveCell.Offset(0, 1).Range("Table2[[#Headers],[number]]").Select
    Selection.ListObject.ListColumns.Add Position:=2
    ActiveCell.FormulaR1C1 = _
        "=HYPERLINK(""https://someurl/query.asp?number=""&[@number],[@number])"
End Sub


I have been trying to use the ActiveCell.Address in place of [@number], as the data is not always in a table, nor is it always in column 1 (A).

Code:
    Dim MyRef As String
    MsgBox ActiveCell.Address(RowAbsolute:=False, ColumnAbsolute:=False)
    Set MyRef = ActiveCell.Address(RowAbsolute:=False, ColumnAbsolute:=False)
    ActiveCell.FormulaR1C1 = _
        "=HYPERLINK(""https://someurl/query.asp?number=""&MyRef,MyRef)"

However setting MyRef returns an error : "Object required".

I don't understand why this error is occurring. Other posts suggest it should work. What silly mistake am I making?

Or is there an easier way to acheive my original goal?

Thanks, AjNau
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top