VBA Code To Return Formula To A Cell

Rupert Bennett

Active Member
Joined
Nov 20, 2002
Messages
276
Good Afternoon,
I have the following bit of code which gets the correct values from each tab in the workbook and puts that value in the correct cell. However, instead of this value e.g. 123,456 I would like to return a formula that says e.g. =Sheet2!C10, or =Sheet2!C50 as the case may be. In short, instead of putting the value on the Summary page, I would like a formula that references a cell on the respective page. Here is the code I have at the moment

Code:
Dim lCount As Long
Dim lSheet As Long
Dim LastRowValue As Long

ThisWorkbook.Activate
Sheets("Summary").Select
lCount = Worksheets.Count

For lSheet = 1 To lCount
Range("A3".Offset(lSheet).Value = Worksheets(lSheet).Name
LastRowValue = Worksheets(lSheet).Cells(Rows.Count,3).End(xlUp).Value
Range("A3").Offset(lSheet,1).Value = LastRowValue
ActiveSheet.Hyperlinks.Add anchor:=Range("A3").Offset(lSheet,0), Address:="",SubAddress:= " ' " & Worksheets(lSheet).Name & " ' !A3"

Next lSheet

End Sub


Your help with this would be greatly appreciated.

Thanks,

Rupert
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hello Rupert,

Try this:
Code:
Dim lCount As Long
Dim lSheet As Long
Dim LastRowValue As Long

ThisWorkbook.Activate
Sheets("Summary").Select
lCount = Worksheets.Count

For lSheet = 1 To lCount
Range("A3").Offset(lSheet).Value = Worksheets(lSheet).Name
Range("A3").Offset(lSheet, 1).Value = "='" & Worksheets(lSheet).Name & "'!" & Worksheets(lSheet).Cells(Rows.Count, 3).End(xlUp).Address
ActiveSheet.Hyperlinks.Add anchor:=Range("A3").Offset(lSheet, 0), Address:="", SubAddress:=" ' " & Worksheets(lSheet).Name & " ' !A3"

Next lSheet

This bit of code here says puts in 3 different components into the cell
Code:
Range("A3").Offset(lSheet, 1).Value = "='" & Worksheets(lSheet).Name  & "'!" & Worksheets(lSheet).Cells(Rows.Count,  3).End(xlUp).Address

1) The = sign to start the formula
2) 'SheetName'! to indicate the sheet for the cell reference
3) Cell Address to indicate the referenced location

The result looks like this : ='SheetName'!A1

I hope this helps!
 
Upvote 0
Perfect!!. Exactly what I am looking for. I made several attempts, but could not get the syntax right. Thank you very much and I appreciate your assistance.

Rupert
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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