excel For循环-对于每一个返回无

bnlyeluc  于 2023-04-22  发布在  其他
关注(0)|答案(2)|浏览(102)

我有一个问题,在Excel中循环遍历单元格,我必须将每个单元格的值填充到SAP中,
但是我不能让它工作,因为在结束时细胞返回为空。
这是我的代码

Sub ClearingTest()

 

If Not IsObject(SAPApp) Then

   Set SapGuiAuto = GetObject("SAPGUI")

   Set SAPApp = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(SAPConnection) Then

   Set SAPCon = SAPApp.Children(0)

End If

If Not IsObject(session) Then

   Set session = SAPCon.Children(0)

End If

If IsObject(WScript) Then

   WScript.ConnectObject session, "on"

   WScript.ConnectObject SAPApp, "on"

   End If

  

LastRow = ThisWorkbook.Sheets("Auto Post").cells(Rows.Count, "N").End(xlUp).row

Dim rng As Range

Dim cell As Range

Dim cell2 As Range

 

For Each cell In Range("N:N")

    If (cell.Value = "ZERO BALANCE" Or cell.Value = "SHORTPAYMENT" Or cell.Value = "ON ACCOUNT" Or cell.Value = "WRITE OFF") Then

        cell.Offset(1).EntireRow.Insert

End If

LastRow = ThisWorkbook.Sheets("Auto Post").cells(Rows.Count, "N").End(xlUp).row

If cell.Value = "ZERO BALANCE" Then

cell.Select

Range("I" & ActiveCell.row).Select

If Selection.End(xlUp).Value = "Amount in doc. curr." Then

Selection.End(xlUp).Offset(1, 0).Select

ElseIf Not Selection.End(xlUp).Value = "Amount in doc. curr." Then

Selection.End(xlUp).Select

End If

Range("B" & ActiveCell.row).Select

 

CoCD = Worksheets("Auto Post").Range("C3").Value

PstDate = Worksheets("Auto Post").Range("D3").Value

PeriodYear = Worksheets("Auto Post").Range("E3").Value

PstKey = Worksheets("Auto Post").Range("D5").Value

Payer = Worksheets("Auto Post").Range("B" & ActiveCell.row).Value

Amount = Worksheets("Auto Post").Range("J5").Value

Curr = Worksheets("Auto Post").Range("F3").Value

Text = Worksheets("Auto Post").Range("L" & ActiveCell.row).Value

RCD = Worksheets("Auto Post").Range("N5").Value

 

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[0]/okcd").Text = "/NF-32"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/sub:SAPMF05A:0131/radRF05A-XPOS1[2,0]").Select

session.findById("wnd[0]/usr/ctxtRF05A-AGKON").Text = Payer

session.findById("wnd[0]/usr/ctxtBKPF-BUDAT").Text = PstDate

session.findById("wnd[0]/usr/txtBKPF-MONAT").Text = PeriodYear

session.findById("wnd[0]/usr/ctxtBKPF-BUKRS").Text = CoCD

session.findById("wnd[0]/usr/ctxtBKPF-WAERS").Text = Curr

session.findById("wnd[0]/usr/ctxtRF05A-AGUMS").Text = "OA"

session.findById("wnd[0]/usr/sub:SAPMF05A:0131/radRF05A-XPOS1[2,0]").SetFocus

session.findById("wnd[0]").sendVKey 2

session.findById("wnd[0]/tbar[1]/btn[7]").press

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/sub:SAPMF05A:0710/radRF05A-XPOS1[2,0]").Select

session.findById("wnd[0]/usr/ctxtRF05A-AGKON").Text = ""

session.findById("wnd[0]/usr/sub:SAPMF05A:0710/radRF05A-XPOS1[2,0]").SetFocus

session.findById("wnd[0]").sendVKey 2

 

'Loop Document number

 

For Each cell2 In Range("D" & ActiveCell.row)

If cell2 = Empty Then GoTo nextstep:

 

session.findById("wnd[0]/usr/sub:SAPMF05A:0731/txtRF05A-SEL01[0,0]").Text = cell2

session.findById("wnd[0]/usr/sub:SAPMF05A:0731/txtRF05A-SEL01[0,0]").caretPosition = 9

session.findById("wnd[0]").sendVKey 0

cell2.Offset(1, 0).Select

Next cell2

 

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/tbar[1]/btn[16]").press

session.findById("wnd[0]/usr/tabsTS/tabpREST").Select

session.findById("wnd[0]/mbar/menu[0]/menu[1]").Select

    

End If

Next cell

   

End Sub

这里的问题是在For Each cell2 In Range("D" & ActiveCell.row)处,即使在我使用cell2.Offset(1, 0).Select选择下一行之后,cell2也不能识别下一行中的新值。
我也不能使用范围(“D6”),因为在循环中的每一个,由于当下一个循环,它将选择新的数据后,空白单元格,如下图。

谢谢。

6bc51xsx

6bc51xsx1#

EDIT-更新为循环D列中的所有值,从D 6开始

这应该可以工作:

Dim c As Range, ws As Worksheet

Set ws = ActiveSheet

'loop from D6 to last cell in ColD with a value
For Each c In ws.Range("D6:D" & ws.Cells(Rows.Count, "D").End(xlUp).Row).Cells
    If Len(c.value) > 0 Then        'if there's a value...
        
        With session.findById("wnd[0]/usr/sub:SAPMF05A:0731/txtRF05A-SEL01[0,0]")
            .Text = c.value
            .caretPosition = 9
        End With
        session.findById("wnd[0]").sendVKey 0

    End If
Next c
ujv3wf0j

ujv3wf0j2#

从你的问题中看不出当你到达空白的第11和12行时你想做什么。你想宏停止吗?或者你想它继续到第13行?
正如braX上面所说,Range("D" & ActiveCell.row)只是一个单元格,所以你不能循环遍历它。你需要定义你想要循环遍历的单元格范围。下面可能是你想要的:

Dim rFirst As Range, rLast As Range, rSource As Range, cell2 As Range

Set rFirst = Range("D" & ActiveCell.Row)    'First cell you want to copy to SAP. You can use Range("D6") if that will always be the first cell.
Set rLast = Range("D" & ActiveSheet.Rows.Count).End(xlUp)   'Last cell you want to copy to SAP  'This code will go to the last cell in column D
Set rSource = Range(rFirst, rLast)  'All the cells between the first and last cell. This is now a range you can loop through.

For Each cell2 In rSource

    If cell2 = Empty Then GoTo nextstep:

    session.findById("wnd[0]/usr/sub:SAPMF05A:0731/txtRF05A-SEL01[0,0]").Text = cell2

    session.findById("wnd[0]/usr/sub:SAPMF05A:0731/txtRF05A-SEL01[0,0]").caretPosition = 9

    session.findById("wnd[0]").sendVKey 0
       
    'cell2.Offset(1, 0).Select  'Not necessary to select each cell
    
Next cell2

我已经注解掉了你选择下一个单元格的那一行,因为它可能是不必要的。如果你真的需要在Excel中选择,你可以取消注解,否则你可以删除它。

相关问题