What is the VBA code for referring another sheet which contains raw data?

mailashish123

New Member
Joined
Mar 14, 2019
Messages
12
[FONT=&quot]I am writing a code in a Module of WB1 and this code shall refer to all the raw data given in Sheet1 of WB2.[/FONT]
[FONT=&quot]
[/FONT]

[FONT=&quot]What would be the piece of code???[/FONT]
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Perhaps...

Code:
Sub TestThis()
    Dim fPath As String, wb2 As Workbook, ws As Worksheet, rng As Range
    fPath = "C:\FullPath\ToFile\wb2Name.xlsx"
    
    Set wb2 = Workbooks.Open(fPath)             [I][COLOR=#006400]'open and set the workbook[/COLOR][/I]
    Set ws = wb2.Sheets("Sheet1")             [COLOR=#006400][I]  'set the sheet[/I][/COLOR]
    Set rng = ws.UsedRange                      [COLOR=#006400][I]'set the range[/I][/COLOR]
    
   [I][COLOR=#006400] 'or if data starts in row 2, is in columns A to J and column A used to determine last row[/COLOR][/I]
    Set rng = ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp)).Resize(, 10)
  [COLOR=#006400][I]  'etc[/I][/COLOR]
    
    MsgBox rng.Address
    wb2.Close False                             [I][COLOR=#006400]'close workbook without saving it[/COLOR][/I]
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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