Best way to loop this? copying from other WB

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
785
Office Version
  1. 365
Platform
  1. Windows
Hi,
i have the following:

Code:
Sub fg()Dim WB1 As Workbook, WB2 As Workbook
Dim WS1 As Worksheet, WS2 As Worksheet


file1 = Sheets("variables").Range("A1")


myFile = "C:\Temp\1\" & file1 & ".xlsx"


Set WB1 = ThisWorkbook
Set WS1 = WB1.Sheets("Sheet1")


Set WB2 = Workbooks.Open(myFile)
Set WS2 = WB2.Sheets("sheet1")


LR = WS1.Range("A65000").End(xlUp).Row + 1


WS2.Range("A1:A10").Copy: WS1.Range("A" & LR).PasteSpecial xlPasteValues


WB2.Close
End Sub

but say if i had filenames in Sheets("variables").Range("A1:A50")
how could i loop through them all ?

appreciate any help
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
How about
Code:
Sub fg()
Dim WB1 As Workbook, WB2 As Workbook
Dim WS1 As Worksheet, WS2 As Worksheet
Dim Cl As Range

For Each Cl In Sheets("Variables").Range("A1:A50")
   If Cl.Value <> "" Then
      File1 = Cl.Value
      
      myFile = "C:\Temp\1\" & File1 & ".xlsx"
      
      
      Set WB1 = ThisWorkbook
      Set WS1 = WB1.Sheets("Sheet1")
      
      
      Set WB2 = Workbooks.Open(myFile)
      Set WS2 = WB2.Sheets("sheet1")
      
      
      LR = WS1.Range("A65000").End(xlUp).Row + 1
      
      
      WS2.Range("A1:A10").Copy: WS1.Range("A" & LR).PasteSpecial xlPasteValues
      
      
      WB2.Close
   End If
Next Cl
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,899
Messages
6,122,155
Members
449,068
Latest member
shiz11713

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