gpt4 book ai didi

excel - 我如何使Excel VBA移动到for循环中的下一个单元格?

转载 作者:行者123 更新时间:2023-12-03 01:14:01 27 4
gpt4 key购买 nike

我这里有一个宏的代码,它可以将一些数据从一个工作表拾取并放置到另一个工作表中。现在这段代码完成了我想要它完成的 90% 的工作。问题是,如果它遇到一个没有任何内容的源单元格(因此 Len(cellVal) = 0cellVal = "" 它会覆盖目标单元格。

您将看到在第二个 ElseIf block 中有一条注释。现在它什么也没做,但如果该语句的计算结果为 true,即源单元格中没有任何内容,我希望 Excel 移至下一个源单元格而不修改目标单元格的内容。

关于如何实现这一点有什么想法吗?

富兰克林

For i = 7 To endPointFlash
Dim cellVal As String
cellVal = Cells(i, "G")

If (Len(cellVal)) > 0 Then
RawData.Activate
ElseIf (Len(cellVal)) = 0 Or cellVal = "" Then
' need to tell excel to do nothing and move to the next cell
End If

For j = 1 To endPointRaw
If cellVal = Mid(Cells(j, "A"), 1, Len(cellVal)) Then
val2 = Mid(Cells(j, "A"), 1, Len(cellVal))
val3 = Cells(j, "D")
Flash.Cells(i, "H").Value = val3
Exit For
Else: Flash.Cells(i, "H").Value = 0
End If
Next j
Flash.Activate
Next i

最佳答案

像这样:

For i = 7 To endPointFlash
Dim cellVal As String
cellVal = Cells(i, "G")

If (Len(cellVal)) > 0 Then
RawData.Activate
ElseIf (Len(cellVal)) = 0 Or cellVal = "" Then
' need to tell excel to do nothing and move to the next cell
Else
For j = 1 To endPointRaw
If cellVal = Mid(Cells(j, "A"), 1, Len(cellVal)) Then
val2 = Mid(Cells(j, "A"), 1, Len(cellVal))
val3 = Cells(j, "D")
Flash.Cells(i, "H").Value = val3
Exit For
Else: Flash.Cells(i, "H").Value = 0
End If
Next j
End If

Flash.Activate
Next i

或者像这样:

For i = 7 To endPointFlash
Dim cellVal As String
cellVal = Cells(i, "G")

If (Len(cellVal)) > 0 Then
RawData.Activate
ElseIf (Len(cellVal)) = 0 Or cellVal = "" Then
' need to tell excel to do nothing and move to the next cell
Goto NextLoop
End If

For j = 1 To endPointRaw
If cellVal = Mid(Cells(j, "A"), 1, Len(cellVal)) Then
val2 = Mid(Cells(j, "A"), 1, Len(cellVal))
val3 = Cells(j, "D")
Flash.Cells(i, "H").Value = val3
Exit For
Else: Flash.Cells(i, "H").Value = 0
End If
Next j
NextLoop:
Flash.Activate
Next i

关于excel - 我如何使Excel VBA移动到for循环中的下一个单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9301419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com