我如何让excel vba移动到for循环中的下一个单元格?

时间:2012-02-15 21:08:11

标签: excel excel-vba vba

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

你会看到第二个ElseIf区块有评论。现在它没有,但如果语句评估为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

3 个答案:

答案 0 :(得分:3)

像这样:

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

答案 1 :(得分:2)

要非常清楚明确地写下这个:

Dim cellVal As String

For i = 7 To endPointFlash
    cellVal = Flash.Cells(i, "G")

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

Next i

在这里,读者很清楚包含空字符串的单元格应该没有动作;在所有其他情况下,执行以下操作。

另外,我不会来回激活每张纸。这样做会使代码变慢(并使屏幕闪烁)。相反,我使用正确的工作表名称识别对Cells的每次调用。再一次,这使得它非常明确地说明了你在做什么。

注意:cellVal = ""必然意味着Len(cellVal)) = 0,因此无需在条件中同时写两者。

答案 2 :(得分:0)

您可以尝试此操作(不确定当单元格为空时是否应执行Flash.Activate - 如果是,请将其移至End If下方):

Dim cellVal As String

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

    If cellVal <> "" Then
      RawData.Activate

      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

    End If 'Else cell is empty: do nothing
Next i