For 循环 - 每次返回什么都没有

问题描述 投票:0回答:1

我在 excel 中循环遍历单元格时遇到问题,我必须将每个单元格值填充到 SAP 中,

但是我无法让它工作,因为最后细胞什么都没有返回。

这是我的代码

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

这里的问题是在

For Each cell2 In Range("D" & ActiveCell.row)
即使我使用
cell2.Offset(1, 0).Select
选择下一行,cell2也没有识别下一行的新值。

我也不能对循环中的每个使用 Range("D6"),因为下一次循环时,它将按照下图选择空白单元格后的新数据。

谢谢。

excel vba loops foreach row
1个回答
0
投票

这应该有效:

Dim c As Range

Set c = Activesheet.Range("D6")  'Starting cell (for example)
Do While Len(c.Value) > 0        'loop while 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

    Set c = c.offset(1)          'next cell down
Loop
© www.soinside.com 2019 - 2024. All rights reserved.