我需要Excel VBA的一些帮助。我在Excel和其他应用程序中来回切换,从其他应用程序复制粘贴到Excel中。我已经有了这个过程,但我需要建议如何粘贴到当前活动的任何单元格,右键,并在该行的末尾向下行,然后在列D
中开始。实际上,这里是我需要在Excel应用程序中发生的确切过程的列表:
D:D
列中)D
列开始,输入一行。在所有这些步骤之间,我发现大部分代码都与其他应用程序进行交互。但我确实也有一个问题 - 在那个应用程序中我正在运行这个声明:
With ATC.ActiveSession
(ATC只是引用应用程序的类型库与其他应用程序进行交互)
与我每次应用程序来回切换复制和粘贴时结束With
语句相反,我需要做什么才能使用with
语句来使用excel的库?
我不想发生的事例:
Sub New_ATS()
Set ATC = GetObject(, "ATWin32.AccuTerm")
AppActivate "AccuTerm 2K2"
With ATC.ActiveSession
.InputMode = 1
.SetSelection 6, 15, 12, 15
.Copy
.InputMode = 0
End With
AppActivate "Microsoft Excel"
Selection.Paste '(not complete, part of question)
Selection.Offset 1, 0 'again, part of the question
AppActivate "AccuTerm 2K2"
With ATC.ActiveSession
.InputMode = 1
.SetSelection 14, 3, 20, 3
.Copy
.InputMode = 0
End With
AppActivate "Microsoft Excel"
' .... end of partial code (continues on)
End Sub
但相反,我想链接' With
语句,但我不知道用什么语句指向Excel的VBA。这就是我想要的:
Sub New_ATS()
Set ATC = GetObject(, "ATWin32.AccuTerm")
AppActivate "AccuTerm 2K2"
With ATC.ActiveSession
.InputMode = 1
.SetSelection 6, 15, 12, 15
.Copy
.InputMode = 0
With Excels_Statement '?????
AppActivate "Microsoft Excel"
Selection.Paste '(not complete, part of question)
Selection.Offset 1, 0 'again, part of the question
AppActivate "AccuTerm 2K2"
With ATC.ActiveSession
.InputMode = 1
.SetSelection 14, 3, 20, 3
.Copy
.InputMode = 0
With Excels_Statement '????
AppActivate "Microsoft Excel"
End With
End With
End With
End With
' .... end of partial code (continues on)
End Sub
答案 0 :(得分:1)
我没有安装AccuTerm,但我认为您可以粘贴到Excel中,而无需每次都激活它。您可以使用最少的输入来分配对象变量,而不是使用With块,这不是变量命名的最佳实践,但它可以解决问题。声明特定类型的变量可以访问Excel的库。
这就是我在想的......(经过部分测试,所以你可能需要稍微调整一下)
Sub New_ATS()
Set ATC = GetObject(, "ATWin32.AccuTerm")
Dim Sesh as ATWin32.AccuTerm.Session 'Not sure this exists
Dim XL as Excel.Range
AppActivate "AccuTerm 2K2"
Set Sesh = ATC.ActiveSession
Sesh.InputMode = 1
Sesh.SetSelection 6, 15, 12, 15
Sesh.Copy
Sesh.InputMode = 0
'AppActivate "Microsoft Excel" - don't need it
Set XL = application.activecell
XL.PasteSpecial
Set XL = XL.offset(0,1)
'AppActivate "AccuTerm 2K2" - no need, still active
Sesh.InputMode = 1 'Once this is set, do you need to set it again?
Sesh.SetSelection 14, 3, 20, 3
Sesh.Copy
Sesh.InputMode = 0 'Once this is set, do you need to set it again?
XL.PasteSpecial
XL.NumberFormat = "General" 'Bullet #1
Set XL = XL.offset(1,0)
'...and so on...
XL.PasteSpecial
XL.NumberFormat = "d-mmm" 'Bullet #3
Set XL = XL.offset(1,0)
XL.PasteSpecial
XL.NumberFormat = "@" 'Bullet #5
Set XL = XL.offset(1,0)
XL.PasteSpecial
XL.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)" 'Bullet #7
Set XL = XL.offset(1,0)
XL = "X" 'Bullet #9
'When you've reached the end of the row
Set XL = XL.offset(1, 4 - XL.Column) 'Since col D = 4
'And repeat your procedure
End Sub