Macro to copy data from File

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have the source CSV File in C:\Downloads

The source file starts with 9101810 followed by _ for e.g. 911810_202219174.csv


When runing the macro "Copy_SourceDat ()", I get a run time error "file not found and the code below is highlighted

Code:
 x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll, vbNewLine)


It would be appreciated if somene could kindly amend my code


Code:
 Sub Open_911810_CSV()
  Dim ws As Workbook, File As String
    File = Dir("C:\Extract" & "\911810*.csv")
    Set ws = Workbooks.Open("C:\downloads\" & File)
    ActiveSheet.UsedRange.Columns("A:D").Offset(1).Clear
Close_111900
End Sub
Sub Close_911810()
 
    Dim wbk As Workbook
    For Each wbk In Application.Workbooks
        If wbk.Name Like "*911810*.csv" Then
            wbk.Close SaveChanges:=True
        End If
    Next wbk
    '
End Sub

 Sub Copy_SourceData()
 Clear_DaTA
Open_911810_CSV
   Dim fn As String, x, y, z, a(2), e, w, cols
    Dim i As Long, ii As Long, n As Long
    fn = Dir("C:\Extract" & "\*911810*.csv")
If fn = "" Then MsgBox "No csv files": Exit Sub
 
    x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll, vbNewLine)
    Sheets("Fassets").Select
    
    ReDim w(1 To UBound(x), 1 To 1)
    For i = 0 To 2: a(i) = w: Next
    cols = Array(Array(5, 9), Array(9, 5), Array(6, 12))
    For i = 1 To UBound(x)
        If x(i) <> "" Then
            n = n + 1: y = Split(x(i), ",")
            For ii = 0 To UBound(cols)
                If y(cols(ii)(0) - 1) Like "*/*/*" Then
                    z = Split(y(cols(ii)(0) - 1), "/")
                    a(ii)(n, 1) = DateSerial(z(2), z(1), z(0))
                Else
                    a(ii)(n, 1) = y(cols(ii)(0) - 1)
                End If
            Next
        End If
    Next
    For i = 0 To UBound(cols)
        Cells(2, cols(i)(1)).Resize(n) = a(i)
    Next
    Sheets("Macro").Select
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Maybe you should replace the code line
VBA Code:
x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll, vbNewLine)
by
VBA Code:
x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Extract\" &fn).ReadAll, vbNewLine)

another issue.
Why do you open the "*911810*.csv" twice ?
Excel Formula:
Set ws = Workbooks.Open("C:\downloads\" & File)
Above also open and the "C:\downloads\" should be "C:\Extract\"

anyway, have a good time.
 
Upvote 0
Solution
Thanks for the help HongRu

The code below was not necessary but did not realise it until you brought it up. Thanks for pointing it out

Hope you have a good day as well

Code:
 Set ws = Workbooks.Open("C:\downloads\" & File)
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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