Import another spreadsheet with VBA

Mykiej23

New Member
Joined
Jun 2, 2016
Messages
22
Hi,

I have some vba code that a former colleague created and said there wasnt a way for us to change the code to stop it asking for the file name. Its a static file that we download each day and overwrite the previous one. However, each day we need to navigate to the folder to select it.

The code is as follows:

Sub Import()


Dim ws As Worksheet


Call ClearTextToColumns
On Error GoTo errorHandler
Set ws = ActiveWorkbook.Sheets("Sysr_1_Raw Data")
strFile = Application.GetOpenFilename("CSV Files(*.csv),*.csv", , "Please select csv file...")
With ws.QueryTables.Add(Connection:="TEXT;" & strFile, _
Destination:=ws.Range("A1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With
Exit Sub
errorHandler:
MsgBox "Data import cancelled.", vbCritical, "Now Recon Import"
errorState = True
End Sub


Is there an easy way to change this without changing the entire code to look only at one file path?

Thanks,
Michaela
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this macro. Change the path and file name (in red) to suit your needs.
Code:
Sub Import()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, wbOpen As Workbook
    Call ClearTextToColumns
    On Error GoTo errorHandler
    Set ws = ActiveWorkbook.Sheets("Sysr_1_Raw Data")
    Set wbOpen = Workbooks.Open("[COLOR="#FF0000"]C:\Test\Sample.csv[/COLOR]")
    With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
        .Refresh
    End With
    Application.ScreenUpdating = True
    Exit Sub
errorHandler:
    MsgBox "Data import cancelled.", vbCritical, "Now Recon Import"
    errorState = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this macro. Change the path and file name (in red) to suit your needs.
Code:
Sub Import()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, wbOpen As Workbook
    Call ClearTextToColumns
    On Error GoTo errorHandler
    Set ws = ActiveWorkbook.Sheets("Sysr_1_Raw Data")
    Set wbOpen = Workbooks.Open("[COLOR=#FF0000]C:\Test\Sample.csv[/COLOR]")
    With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
        .Refresh
    End With
    Application.ScreenUpdating = True
    Exit Sub
errorHandler:
    MsgBox "Data import cancelled.", vbCritical, "Now Recon Import"
    errorState = True
    Application.ScreenUpdating = True
End Sub


Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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