VBA - select file to be loaded

albertod8

New Member
Joined
Mar 31, 2023
Messages
10
Office Version
  1. 2021
Platform
  1. Windows
Hi, I am trying to import (applying some transformations) several tabs from a file. First I want the user to pick the file to be used for the import and then i have some filters applied. When i run the Macro, it gets loaded the original file that i loaded when run the macro. Even chosing a different file during the file picker, macro continues and loads data from a different file.

Sub Import_Report()


Dim FilePicker As FileDialog
Dim mypath10 As String

Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)

With FilePicker
.Title = "Please Select Your File"
.AllowMultiSelect = False
.ButtonName = "Open"
If .Show = -1 Then
mypath = .SelectedItems(1)
Else
End
End If
End With

On Error Resume Next
ActiveWorkbook.Queries("Customer_Account").Delete
On Error GoTo 0
'
ActiveWorkbook.Queries.Add Name:="Customer_Account", Formula _
:= _
"let" & Chr(13) & "" & Chr(10) & " Source = mypath10, null, true)," & Chr(13) & "" & Chr(10) & " #""Customer Account Information_Sheet"" = Source{[Item=""Customer Account Information"",Kind=""Sheet""]}[Data]," &

{fields and filters}

ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Customer Account Information (3)"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Customer Account Information (3)]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False

Range("I2").Select
End With
Columns("A:A").Select
ActiveSheet.Name = "Customer_Summary"
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I guess one issue would be mypath versus Dim mypath10 which tells me you don't use Option Explicit. The rest of it is not only hard to read because you didn't use code tags (vba button on posting toolbar) but it's unfamiliar stuff to me anyway. Suggest you examine whether or not you're using the wrong variable name as a start.
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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