使用Excel宏移动到工作表中的下一行(as400)

时间:2019-08-26 10:07:01

标签: excel vba ibm-midrange

我对VBA有非常基本的了解,我正在尝试将数据从excel文件复制到AS400中。我搜索了该网站,发现我更改了代码,并且可以正常工作,但是在完成电子表格的第一行后,它始终会停止。我需要添加什么代码,因此它将跳到第二,第三位。 ...在宏执行完之后排成一行。

非常感谢您的帮助。

我已经尝试过在此网站和在线上进行搜索。

[PCOMM SCRIPT HEADER]
LANGUAGE=VBSCRIPT
[PCOMM SCRIPT SOURCE]
autECLSession.SetConnectionByName(ThisSessionName)
Dim excel
Dim row
Dim inputFilename 

inputFilename = "C:\testfile.xlsx"

Set excel = CreateObject("Excel.Application")
Set objWorkbook = excel.Workbooks.Open(inputFilename)
excel.visible = true 'If you want to

row = 2 'Start at row 2 of your excel sheet

'Loop until AS400 has blocked input (error in most cases) or there are no values in column A of excel sheet left
while autECLSession.autECLOIA.InputInhibited = 0 AND excel.Cells(row,2).Value <> "" 

   '### YOUR MACRO STARTS HERE
   autECLSession.autECLOIA.WaitForAppAvailable

   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf6]"
   autECLSession.autECLPS.SendKeys excel.Cells(row,2).Value 'Value of column B / current row from excel sheet
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
autECLSession.autECLPS.SendKeys excel.Cells(row,3).Value 'Value of column C / current row from excel sheet
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
autECLSession.autECLPS.SendKeys excel.Cells(row,4).Value 'Value of column D / current row from excel sheet
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
autECLSession.autECLPS.SendKeys "[tab]"
autECLSession.autECLPS.SendKeys excel.Cells(row,5).Value 'Value of column E / current row from excel sheet
   autECLSession.autECLPS.SendKeys "[pf2]"
autECLSession.autECLPS.SendKeys "[pf13]"
autECLSession.autECLPS.SendKeys "0"
autECLSession.autECLPS.SendKeys "[tab]"
autECLSession.autECLPS.SendKeys excel.Cells(row,6).Value 'Value of column F / current row from excel sheet
autECLSession.autECLPS.SendKeys "[enter]"
autECLSession.autECLPS.SendKeys "[pf10]"




   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[tab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[tab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[tab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[tab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[tab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[tab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[eraseeof]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "100"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
autECLSession.autECLPS.SendKeys "[enter]"
autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[tab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "4"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[enter]"

   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[attn]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[enter]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[enter]"


   ' ...

   '### YOUR MACRO ENDS HERE

   row = row + 1
wend

excel.Quit
Set excel = Nothing

1 个答案:

答案 0 :(得分:0)

现在,代码静态地实例化row

row = 2 'Start at row 2 of your excel sheet

并在While循环中使用它:

while autECLSession.autECLOIA.InputInhibited = 0 AND excel.Cells(row,2).Value <> "" 

所需的是“遍历”各行,这可以使用For...Next完成(您将需要阅读该书)。您没有告诉我们要停止多少行,或要停止什么条件,而是告诉我们以下代码段。

这将实例化并分配工作表的对象,然后实例化包含数据的范围。它假定工作簿中的第一个工作表(因为根据正确的问题中的代码)和该工作表上的UsedRangeUsedRange是包含工作表上内容的连续单元格。

'Declare a Worksheet object and set it to the correct Sheet in the workbook
Dim ws as Worksheet
'Declare the Range that contains the rows
Dim theRange as Range
Set ws = objWorkbook.Worksheets(1)
Set theRange = ws.UsedRange

For Each row in theRange.Rows
  while autECLSession.autECLOIA.InputInhibited = 0 AND theRange.Cells(row,2).Value <> "" 
  'More code here
Next