溜溜问答 > 日常办公 > Excel > vb 时时获取Excel 活动表
kiss mayue
kiss mayue
邀请你来回答
155人阅读 2022-04-27

vb 时时获取Excel 活动表

为什么我每次切换工作表后,点按钮获取当前活动表名,都是窗体第一次载入时候获得的活动表表名呢?也就是说我Excel这边表切换了,但是vb这边不能时时监控到,它获得当前活动表都是窗体load时候的表,窗体load时候获取已经打开的 工作簿.工作表如下 set xlapp = getobject(, 'Excel.application') set xlbook = xlapp.activeworkbook set xlsheet = xlbook.activesheet 。按照这个我每次获得活动表都要,都要写以上三句代码。有没有其他解决的方法呢?
我要回答
2个回答

Dim xlApp As Object
Dim xlbook As Object
Dim xlsheet As Object
Private Sub Command1_Click()
Set xlApp = CreateObject("excel.application")
xlApp.Visible = True
Timer1.Enabled = True
End Sub

Private Sub Command2_Click() '关闭
xlApp.Quit
Set xlApp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 1000
End Sub

Private Sub Timer1_Timer()
On Error GoTo er
Set xlbook = xlApp.ActiveWorkbook
Set xlsheet = xlbook.ActiveSheet
Print xlsheet.Name '当前工作表
er:
End Sub

查看全部
2022-04-27
回复 采纳

代码是在你没切换的时候就运行了,你切换之后又没运行过。

查看全部
2022-04-27
回复 采纳
发表成功!
感谢您的分享!
好的