我正在尝试编写一个在用户提示操作后将数据导出到 excel 的代码。基本上,我已经能够成功导出到 Excel,但是第二个实例我想写入新选项卡而不是新的 Excel 应用程序。
oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(3)
oSheet.Delete()
oSheet = oBook.Worksheets(2)
oSheet.Delete()
oSheet = oBook.Worksheets(1)
oSheet.Name = "Run " & Counter
此时,用户将按下一个按钮,使 Excel 不再处于活动状态。因此,当我想将更多数据写入新工作表时,除非我完全重复代码,否则 Object 命令不起作用。
我试过:
Counter +=1
'For the first instance
If Counter = 1 Then
oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(3)
oSheet.Delete()
oSheet = oBook.Worksheets(2)
oSheet.Delete()
oSheet = oBook.Worksheets(1)
oSheet.Name = "Run " & Counter
Else
'For every instance after that the user wants to do another run
oExcel.ActivateObject(Excel.Application)
oBook = oExcel.Workbooks(1)
oSheet = oBook.Worksheets.Add
oSheet.Name = "Run " & Counter
End If
我已经找了好几天了,我很沮丧。我不知道如何引用打开的 excel 以继续写入数据......在用户按下 VB 表单上的按钮确认他们想要再次运行之后。