set function and variable file names

Ox1138

New Member
Joined
Feb 18, 2019
Messages
2
Hi All

Thanks for any help on this, I am fairly new and still learning.

I am trying to develop a macro that will open, copy, (and if possible close) from about 50 closed workbooks. I am currently using this code:

Dim wb As Workbook
Set wb = Workbooks.Open("C:\Users\Me\Path\AA.csv", True, True)
ThisWorkbook.Activate

But this only opens the file AA.csv, I have a variable "Live" which cycles through the names of the closed workbooks. However, everytime I try to change the Set wb to something that has a variable in it, I seem to fail.

For clarity - I want to be able to change "aa.csv" (or any longer part of the string if necessary) within the macro.

Thanks for any help you can offer.

Cheers
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,619
Office Version
  1. 2007
Platform
  1. Windows
For example:

Code:
Sub OpenWb()
    Dim wb As Workbook
    Dim live As Variant, wPath As String
    
    wPath = "C:\Users\Me\Path\"
    live = Dir(wPath & "*.csv")
    Do While live <> ""
        Set wb = Workbooks.Open(wPath & live, True, True)
        ThisWorkbook.Activate
        '
        'here your code
        '
        live = Dir()
    Loop
End Sub
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,619
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,383
Messages
5,635,942
Members
416,889
Latest member
dhegs

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
Top