VBA Code to Import File on Desktop

rmobley232

New Member
Joined
Jun 30, 2016
Messages
15
I have created a code to import a workbook that will be saved on my desktop on a daily basis to do a few calculations. I need assistance with the two following requests:
1. There will be occasions when there will not be a file, so I would like to write a command to skip the import and move onto the next step in the code if the file does not exist
2. Since this is creating a query where the name of the tab will be the same everyday, how do I avoid the run time error message of "A query with the name 'SS' already exists' (let me know if you need the full error#)

I am running Windows 10 and Office 365 version of Excel. Thanks so much for the input!!

ActiveWorkbook.Queries.Add Name:="SS", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""C:\Users\br4rkm\OneDrive - W.W Grainger, inc\Desktop\Offline Maint SS " & Format(Now(), "mmddyy") & ".xlsx""), null, true)," & Chr(13) & "" & Chr(10) & " SS_Sheet = Source{[Item=""SS"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(SS_Sheet, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers""" & _
",{{""Material"", type text}, {""Location"", type text}, {""New Safety Stock"", type number}, {""Request Type"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=SS;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [SS]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "SS"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.ListObjects("SS").TableStyle = ""
ActiveSheet.ListObjects("SS").ShowAutoFilterDropDown = False
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this:
VBA Code:
    Dim workbookFile As String
    Dim SSworkbookQuery As WorkbookQuery
    
    workbookFile = "C:\Users\br4rkm\OneDrive - W.W Grainger, inc\Desktop\Offline Maint SS " & Format(Date, "mmddyy") & ".xlsx"
    
    If Dir(workbookFile) <> vbNullString Then
    
        Set SSworkbookQuery = Nothing
        On Error Resume Next
        Set SSworkbookQuery = ActiveWorkbook.Queries("SS")
        On Error GoTo 0
        
        If SSworkbookQuery Is Nothing Then

            ActiveWorkbook.Queries.Add Name:="SS", Formula:= _
                "let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""" & workbookFile & """), null, true)," & Chr(13) & "" & Chr(10) & " SS_Sheet = Source{[Item=""SS"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(SS_Sheet, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers""" & _
                ",{{""Material"", type text}, {""Location"", type text}, {""New Safety Stock"", type number}, {""Request Type"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
            With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
                "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=SS;Extended Properties=""""" _
                , Destination:=Range("$A$1")).QueryTable
                .CommandType = xlCmdSql
                .CommandText = Array("SELECT * FROM [SS]")
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .BackgroundQuery = True
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .PreserveColumnInfo = True
                .ListObject.DisplayName = "SS"
                .Refresh BackgroundQuery:=False
            End With
    
        Else
       
            ActiveSheet.ListObjects("SS").QueryTable.Refresh
        
        End If
        
    End If
    
    ActiveSheet.ListObjects("SS").TableStyle = ""
    ActiveSheet.ListObjects("SS").ShowAutoFilterDropDown = False
PS please use vba code tags.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,402
Messages
6,119,299
Members
448,885
Latest member
LokiSonic

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