Excel中的VBA – 粘贴到活动单元,然后右键单击

我需要Excel VBA的一些帮助。 我从Excel和另一个应用程序来回切换,从另一个应用程序粘贴到Excel中。 我已经有了这个过程,但是我需要关于如何粘贴到当前活动的任何单元格的build议,选项卡右侧,并在该行的末尾下行,然后从D列开始。 其实,这里是我需要在Excel应用程序中发生的确切过程的列表:

  1. [数字格式]粘贴到当前活动的单元格(将始终在D:D列中)
  2. 右键单击一个单元格
  3. [date格式:“d-mmm”]今天的date
  4. 选项卡正确
  5. [文本]粘贴
  6. 选项卡正确
  7. [会计]粘贴
  8. 选项卡正确
  9. 在该列中键入字母“X”
  10. input一行,从D列开始。

在所有这些步骤之间,我已经发现了大部分代码与其他应用程序交互。 但是我也有一个关于这个问题的问题 – 在这个应用程序中,我运行这个语句:

With ATC.ActiveSession (ATC简单地引用应用程序的types库来与其他应用程序进行交互)

With每次应用程序来回复制和粘贴时结束With语句相反,我需要使用as语句来使用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 

我没有安装AccuTerm,但我认为你可以粘贴到Excel,而无需每次激活它。 而不是使用With块,你可以用最小的input来分配一个对象variables…不是variables命名的最佳做法,但它可以做到这一点。 声明特定types的variables将使您可以访问Excel的库。

这是我在想…(部分testing,所以你可能需要调整一下)

 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