Macro to open CSV docs wont stop looping?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I have an excel document with a macro i need help with,

all I'm trying to do is open every CSV file in the folder where my excel document is saved and do the following.

"
ActiveSheet.Range("H1") = "ISRC"
ActiveSheet.Range("H2:H" & LR1).FormulaR1C1 = "=LEFT(RC[-2],2)" "

but im stuck in a continues loop?


heres my code,

VBA Code:
Sub Open_Docs_v2()
   Dim Fname As String, Pth As String
   
   Pth = ThisWorkbook.Path & "\"

   Fname = Dir(Pth & "*.csv")
Application.ScreenUpdating = False
   Do Until Fname = ""
If Fname <> ThisWorkbook.Name Then
         With Workbooks.Open(Pth & Fname)


    LR1 = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    ActiveSheet.Range("H1") = "ISRC"
    ActiveSheet.Range("H2:H" & LR1).FormulaR1C1 = "=LEFT(RC[-2],2)"


            .Close True
         End With
End If
      Fname = Dir
   Loop
End Sub



please change or just give me a better way of doing it if there is one.
thanks
Tony
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I have tested this and seem to work for me
I changed a couple of things

VBA Code:
Sub Open_Docs_v2()

Dim Fname As String, Pth As String
Pth = ThisWorkbook.Path & "\"
Fname = Dir(Pth & "*.csv")
Application.ScreenUpdating = False
Do Until Fname = ""
    If Fname <> ThisWorkbook.Name Then
        With Workbooks.Open(Pth & Fname)
            LR1 = .Sheets(1).Cells(Rows.Count, "A").End(xlUp).row
            .Sheets(1).Range("H1") = "ISRC"
            .Sheets(1).Range("H2:H" & LR1).FormulaR1C1 = "=LEFT(RC[-2],2)"
            .Close True
        End With
    End If
    Fname = Dir
Loop
End Sub
 
Upvote 0
I have tested this and seem to work for me
I changed a couple of things

VBA Code:
Sub Open_Docs_v2()

Dim Fname As String, Pth As String
Pth = ThisWorkbook.Path & "\"
Fname = Dir(Pth & "*.csv")
Application.ScreenUpdating = False
Do Until Fname = ""
    If Fname <> ThisWorkbook.Name Then
        With Workbooks.Open(Pth & Fname)
            LR1 = .Sheets(1).Cells(Rows.Count, "A").End(xlUp).row
            .Sheets(1).Range("H1") = "ISRC"
            .Sheets(1).Range("H2:H" & LR1).FormulaR1C1 = "=LEFT(RC[-2],2)"
            .Close True
        End With
    End If
    Fname = Dir
Loop
End Sub

Thanks so much!!! You have solved my problem!
Greetings from Spain :)
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

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