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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
what do you mean by linking? You can set up a reference to both
 
Upvote 0
what do you mean by linking? You can set up a reference to both
I am trying to use Activecell.formula function to link the cells , but it is giving an error. I will upload worksheet and the code I have written
 
Upvote 0
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"
End Sub
 
Upvote 0
Thank You,
I have attached a sample sheet
There is a main sheet "consolidate" and other data sheet (data 1 to data 4).

I want to consolidate the data from all the four sheets one below the other .i.e data from data 1 sheet is linked to consolidate sheet, then data from data 2 sheet linked to consolidate sheet below the linked data 1

sample.xlsx
H
13
Data 4
 
Upvote 0
So basically, not a paste special value but the linked value from each of the data sheet to consolidate sheet one below the other where data from one sheet ends and data from other sheet starts
 
Upvote 0
So is it a case of all "A1"s linked etc?
 
Upvote 0
Do not seem to be able to get the book
 
Upvote 0
Hi lzunke,

how about
VBA Code:
Sub lzunke()
'https://www.mrexcel.com/board/threads/vba-code-to-link-from-one-sheet.1213006/
'2022-08-09

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

Set wsDest = ThisWorkbook.Sheets(1)
wsDest.Cells.ClearContents

lngStart = 2
For Each ws In Worksheets
  If ws.Name <> wsDest.Name Then
    ws.UsedRange.Copy
    wsDest.Range("A" & lngStart).Select
    wsDest.Paste Link:=True
    lngStart = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Row + 1
  End If
Next ws

Set wsDest = Nothing
End Sub
Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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