VBA not working when cell value holding file path string changed

Lola223

New Member
Joined
Jan 18, 2022
Messages
26
Office Version
  1. 2010
Hi,

I am using the following VBA to combine some daily reports into an end of month report. The files in question are all .Csv files.

The user should enter the current months filepath into cell B1. I am finding that this works for the current month ie February 22 then when I change the path in cell B1 to another month, I get an error indicating that the first csv file that the code is looking for is still the first csv file in Feb and therefore the code fails. Can anyone help point me in the direction of where I am going wrong?

VBA Code:
Public strPath As Range

Sub CopyRange()

    Application.DisplayAlerts = False

    Application.ScreenUpdating = False

    Dim wkbDest As Workbook

    Dim wkbSource As Workbook

    Set wkbDest = ThisWorkbook

    Dim LastRow As Long

    Set strPath = Worksheets("Combine Dataset").Range("B1")

    strExtension = Dir("*.csv*")

    Do While strExtension <> ""

        Set wkbSource = Workbooks.Open(strPath & strExtension)

        With wkbSource

             LastRow = .ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

            .ActiveSheet.Range("A5:K" & LastRow).Copy wkbDest.Sheets("Banking Combined").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)

            .Close savechanges:=False

        End With

        strExtension = Dir

    Loop

    Application.ScreenUpdating = True

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
We do not know how's your folder/files are organised but try:

VBA Code:
strExtension = Dir(strPath & "*.csv")
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

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