我的VBA项目包括清理数据库。
我正在使用Userform,这是我现在写的代码。
我成功导入了一个文件,但我仍然无法对其执行操作。
码
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
答案 0 :(得分:1)
最好使用声明的变量来引用打开的CSV表
HashMap<String,Book> hashMap = HashMap<String,Book>) getIntent().getSerializableExtra("hashMap");
hashMap.put(bookIsbn,new Book(bookTitle,bookIsbn,bookFee,bookAuthor));
finish();
变量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
现在引用CSV工作表,可以在UserForm模块的其他代码中引用
答案 1 :(得分:0)
您可以打开第二个Excel实例以使用隐藏文件
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