Reputation: 31
My VBA project consists on cleaning databases.
I'm using a Userform, and this is the code i wrote till now.
I have been successful at importing a file, but i still can't perform actions on it.
code
Dim fNameAndPath As Variant
Private Sub importedworkbook_Click()
MsgBox "imported workbook : " & _
fNameAndPath
End Sub
Private Sub Importbutton_Click()
fNameAndPath = Application.GetOpenFilename(FileFilter:="CSV Files Only (*.CSV), *.XLS", Title:="Select File To Be Opened")
If fNameAndPath = False Then Exit Sub
Workbooks.Open Filename:=fNameAndPath
End Sub
Upvotes: 2
Views: 2141
Reputation: 55692
You can open a second instance of Excel to work with the hidden file
Dim objExcel As Excel.Application
Dim wb As Workbook
fNameAndPath = Application.GetOpenFilename(FileFilter:="CSV Files Only (*.CSV), *.XLS", Title:="Select File To Be Opened")
If fNameAndPath = False Then Exit Sub
Set objExcel = New Excel.Application
Set wb = objExcel.Workbooks.Open(fNameAndPath)
Set wsCSV = wb.Worksheets(1)
MsgBox wsCSV.Name
' do stuff
'close second instance
wb.Close False
objExcel.Quit
Set objExcel = Nothing
Upvotes: 0
Reputation: 53136
Best to use declared variables to reference the opened CSV sheet
Private wsCSV as Worksheet
Private Sub Importbutton_Click()
Dim wb as Workbook
fNameAndPath = Application.GetOpenFilename(FileFilter:="CSV Files Only (*.CSV), *.XLS", Title:="Select File To Be Opened")
If fNameAndPath = False Then Exit Sub
Set wb = Workbooks.Open(Filename:=fNameAndPath)
Set wsCSV = wb.Worksheets(1) ' A csv file will only ever have one sheet
End Sub
Variable wsCSV
now refers to the CSV sheet, and can be referenced in other code in the UserForm module
Upvotes: 1