我已经在PCOMM-VBA宏上工作了几个月了。这个问题主要发生在我写的每个宏上。虽然不是每次尝试运行宏时的情况。
我很怀疑问题的根源是PCOMM无法跟上excel因此会跳过代码的某些部分。但是阅读几篇帖子让我觉得在我的代码中循环和活动表格可能有些问题。我不确定导致这个问题的原因,但我希望你能指导我找到答案。
宏的目的是更新供应商'电子邮件地址。为了选择供应商,我创建了一个包含供应商代码的模板,并且需要上传其他信息以及电子邮件地址。(4列,带有简单的.xls)
简而言之:当我在F8中运行代码时,代码会运行而没有任何错误。当我尝试在F5中运行它时,编译器将跳过一些行并进行无限循环(因为请求的数据永远不会出现在字段中)或更糟糕的情况下实际使用错误的数据更新它。
以下是实际代码:
Sub email_upload()
Dim appExcel As Excel.Application
Dim appWb As Excel.Workbook
Dim appWs As Excel.Worksheet
'This part creates a connection between an existing PCOMM session and Excel
Set autECLConnMgrObj = CreateObject("PCOMM.autECLConnMgr")
Set autECLOIAObj = CreateObject("PCOMM.autECLOIA")
Set autECLSessionObj = CreateObject("PCOMM.autECLSession")
Set autECLPSObj = CreateObject("PCOMM.autECLPS")
Set autECLConnList = CreateObject("PCOMM.autECLConnList")
autECLOIAObj.SetConnectionByHandle (autECLConnList(1).Handle)
autECLPSObj.SetConnectionByHandle (autECLConnList(1).Handle)
autECLConnList.Refresh
'Opens the template where the supplier codes are in (normal .xls)
With Application.FileDialog(1)
.Title = "Please select a file to import!"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
FileSelected = .SelectedItems(1)
End With
fileName = FileSelected
Set appExcel = Excel.Application
Set appWb = appExcel.Workbooks.Open(fileName:=fileName)
Set appWs = appWb.Worksheets("input")
appExcel.Visible = True
appWb.Worksheets("input").Activate
With appWs
autECLOIAObj.WaitForAppAvailable
'Loop thru every row in the template worksheet
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row Step 1
'Start system navigation in AS/400
autECLPSObj.SetCursorPos 6, 23
autECLPSObj.SendKeys Cells(i, "A")
autECLPSObj.WaitForAttrib 6, 23, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[enter]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf13]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf10]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SetCursorPos 13, 24
autECLPSObj.SendKeys Cells(i, "B")
autECLPSObj.WaitForAttrib 13, 24, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[enter]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf8]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
If Cells(i, "C") = "" Then
autECLPSObj.SetCursorPos 13, 21
autECLPSObj.SendKeys "1"
autECLPSObj.WaitForAttrib 13, 21, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
Else
autECLPSObj.SetCursorPos 13, 21
autECLPSObj.SendKeys Cells(i, "C")
autECLPSObj.WaitForAttrib 13, 21, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
End If
autECLPSObj.SetCursorPos 21, 1
autECLPSObj.SendKeys Cells(i, "D")
autECLPSObj.WaitForAttrib 21, 1, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf8]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf12]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf12]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
Next i
End With
appWb.Close
MsgBox ("Input done!")
End Sub