VBA code to link from one sheet

lzunke

New Member
Joined
Aug 9, 2022
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have two excel sheets in same workbook. I want to link data from one worksheet to another , but it is giving an error. I want to link the data which is spread into multiple rows and colums. Pasting was easy , but linking , I am not able to understand which code to use
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Discovered that this machine is not allowed the mini sheet add-in so I will give an example
VBA Code:
Sub LinkSheets()
Dim WbName1 As Workbook
Dim WsName1 As Worksheet, WsName2 As Worksheet

Set WbName1 = ThisWorkbook
Windows(ThisWorkbook.Name).Activate
Set WsName1 = WbName.Sheets(1) ' Or replace with "SheetName1"
Set WsName2 = WbName.Sheets(2) ' Or replace with "SheetName2"


WsName1.Range("A2").Value = WsName2.Range("A2").Value
WsName1.Range("A5:A22").Value = WsName2.Range("A5:A22").Value
WsName1.Range("E7:Z7").Value = WsName2.Range("E7:Z7").Value

End Sub
 
Upvote 0
Discovered that this machine is not allowed the mini sheet add-in so I will give an example
VBA Code:
Sub LinkSheets()
Dim WbName1 As Workbook
Dim WsName1 As Worksheet, WsName2 As Worksheet

Set WbName1 = ThisWorkbook
Windows(ThisWorkbook.Name).Activate
Set WsName1 = WbName.Sheets(1) ' Or replace with "SheetName1"
Set WsName2 = WbName.Sheets(2) ' Or replace with "SheetName2"


WsName1.Range("A2").Value = WsName2.Range("A2").Value
WsName1.Range("A5:A22").Value = WsName2.Range("A5:A22").Value
WsName1.Range("E7:Z7").Value = WsName2.Range("E7:Z7").Value

End Sub
Thank you, but this will paste special value. I needed a equal to link, so that in the consolidate sheet I could see from which sheet the value is coming from
 
Upvote 0
No, it makes the 1st cell equal the value in the 2nd. There is no copy and paste
 
Upvote 0
Hi lzunke,

both posts with attachments just deliver an empty cell for me in my Excel.

The result in Sheets Collect looks like this:
Cell Formulas
RangeFormula
B2:H6B2=Data1!A1
B7:F11B7=Data2!A1


Code used:
VBA Code:
Sub lzunke2_Hyperlinks()
'https://www.mrexcel.com/board/threads/vba-code-to-link-from-one-sheet.1213006/
'2022-08-10

Dim wsDest        As Worksheet
Dim ws            As Worksheet
Dim lngStart      As Long

Set wsDest = ThisWorkbook.Sheets(1)
wsDest.Cells.Delete               'Clear all data in Collection sheet

lngStart = 2
wsDest.Range("A1").Value = "Hyperlink to sheet"
wsDest.Range("B1").Value = "Start of Data"

For Each ws In Worksheets
  If ws.Name <> wsDest.Name Then
    ws.UsedRange.Copy
    Application.Goto wsDest.Range("B" & lngStart)
    wsDest.Paste Link:=True
    wsDest.Hyperlinks.Add Anchor:=wsDest.Range("A" & lngStart), _
        Address:="", SubAddress:=ws.Name & "!A1", TextToDisplay:=ws.Name
    lngStart = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Row + 1
  End If
Next ws

Set wsDest = Nothing

End Sub
Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,214,963
Messages
6,122,484
Members
449,088
Latest member
Melvetica

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