关于#VBA#的问题:没有循环填入缺少个判断

现状是只跑最后一行,没有循环填入
缺少个判断自动从序列进行填充跟循环

img


img

img

根据你提供的截图,可以看出问题出在填充数据的部分。具体来说,代码只执行了最后一行的填充操作,并没有循环填充其他行。

为了解决这个问题,需要在函数中添加循环语句来遍历所有需要填充的行(例如,从第2行到最后一行)。同时,需要加入一个判断语句,判断当前行的产品名称是否为空。如果为空,则使用上一行的产品名称进行填充。

修改后的代码如下所示:

Function FillUpProduct()
    Dim i As Integer
    For i = 2 To ActiveSheet.UsedRange.Rows.Count
        If Range("A" & i) = "" Then
            Range("A" & i) = Range("A" & i - 1)
        End If
    Next i
End Function

在原有的代码基础上,我们添加了一个For循环,用来遍历表格中第2行到最后一行的所有行。对于每一行,我们使用If语句来判断该行的产品名称是否为空。如果为空,我们将该行的产品名称的值设置为上一行的产品名称的值,即Range("A" & i - 1),其中i代表当前行数。

注意,在VBA中,函数需要返回一个值。因此,你需要在代码末尾添加Return语句,以便函数能够正确返回。由于该函数不需要返回任何值,因此你可以将返回值设置为任意值。

修改后的完整代码如下:

Function FillUpProduct()
    Dim i As Integer
    For i = 2 To ActiveSheet.UsedRange.Rows.Count
        If Range("A" & i) = "" Then
            Range("A" & i) = Range("A" & i - 1)
        End If
    Next i
    Return True
End Function

你可以将该代码复制粘贴到VBA编辑器中,保存并运行该函数。这样就能够实现填充产品名称的功能了。

大体思路,可以考虑使用循环结构来遍历需要填充的行,并根据判断条件来决定如何填充。代码构思,使用一个For循环来遍历需要填充的行。在每次循环中,使用If语句来判断当前行是否为偶数行,并根据判断结果来决定如何填充数据。使用Range对象来指定需要填充的单元格,并使用Value属性来设置单元格的值。
小小代码示例:

Sub FillData()  
    Dim i As Integer  
    For i = 1 To 10 ' 循环10次,可以根据需要修改  
        If i Mod 2 = 0 ' 判断是否为偶数  
            Range("A" & i).Value = " even number" ' 填充偶数行的数据  
        Else  
            Range("A" & i).Value = " odd number" ' 填充奇数行的数据  
        End If  
    Next i  
End Sub

#如有帮助,恭请采纳

Excel VBA的循环与判断

Sub «ö1_Click()
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim n As Integer
Dim biao_zhi As Boolean
Dim yong_liang As Double
n = 2
For i = 3 To 10000
    If Len(Sheet1.Cells(i, 16).Value) < 2 Then
       Exit For
    End If
    yong_liang = 0
    biao_zhi = True
    For m = 2 To 10000
        If Len(Sheet3.Cells(m, 1).Value) < 2 Then
           Exit For
        End If
        If Sheet3.Cells(m, 2) = Sheet1.Cells(i, 2).Value _
        And Sheet3.Cells(m, 1).Value = Sheet1.Cells(i, 16).Value Then
            biao_zhi = False
            Exit For
        End If
    Next m
    If biao_zhi = True Then
       For j = 3 To 10000
          If Len(Sheet1.Cells(j, 16).Value) < 2 Then
             Exit For
          End If
          If Sheet1.Cells(j, 2).Value = Sheet1.Cells(i, 2).Value _
              And Sheet1.Cells(j, 16).Value = Sheet1.Cells(i, 16).Value Then
                yong_liang = yong_liang + Sheet1.Cells(j, 9).Value
          End If
        Next j
          Sheet3.Cells(n, 1).Value = Sheet1.Cells(i, 16).Value
          Sheet3.Cells(n, 2).Value = Sheet1.Cells(i, 2).Value
          Sheet3.Cells(n, 3).Value = yong_liang
          n = n + 1
     End If
Next i
End Sub
Sub «ö2_Click()
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim n As Integer
Dim biao_zhi As Boolean
Dim yong_liang As Double
n = 2
For i = 3 To 10000
    If Len(Sheet2.Cells(i, 16).Value) < 2 Then
       Exit For
    End If
    yong_liang = 0
    biao_zhi = True
    For m = 2 To 10000
        If Len(Sheet3.Cells(7, 1).Value) < 2 Then
           Exit For
        End If
        If Sheet3.Cells(m, 8) = Sheet2.Cells(i, 2).Value _
        And Sheet3.Cells(m, 7).Value = Sheet2.Cells(i, 16).Value Then
            biao_zhi = False
            Exit For
        End If
    Next m
    If biao_zhi = True Then
       For j = 3 To 10000
          If Len(Sheet2.Cells(j, 16).Value) < 2 Then
             Exit For
          End If
          If Sheet2.Cells(j, 2).Value = Sheet2.Cells(i, 2).Value _
              And Sheet2.Cells(j, 16).Value = Sheet2.Cells(i, 16).Value Then
                yong_liang = yong_liang + Sheet2.Cells(j, 9).Value
          End If
        Next j
          Sheet3.Cells(n, 7).Value = Sheet2.Cells(i, 16).Value
          Sheet3.Cells(n, 8).Value = Sheet2.Cells(i, 2).Value
          Sheet3.Cells(n, 9).Value = yong_liang
          n = n + 1
     End If
Next i
End Sub
Sub «ö3_Click()
'Selection.Value = "Test1"
Dim i As Integer
Dim j As Integer
Dim n As Integer
n = 2
For i = 2 To 10000
    If Len(Sheet3.Cells(i, 1).Value) < 2 Then
        Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
    End If
    For j = 2 To 10000
        If Len(Sheet3.Cells(j, 1).Value) < 2 Then
             Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
        End If
        If Sheet3.Cells(i, 1).Value = Sheet3.Cells(j, 7).Value _
        And Sheet3.Cells(i, 2).Value = Sheet3.Cells(j, 8).Value _
        And Sheet3.Cells(j, 9).Value - Sheet3.Cells(i, 3).Value <> 0 Then
            Sheet3.Cells(n, 12).Value = Sheet3.Cells(i, 1).Value
            Sheet3.Cells(n, 13).Value = Sheet3.Cells(i, 2).Value
            Sheet3.Cells(n, 14).Value = Sheet3.Cells(i, 3).Value
            Sheet3.Cells(n, 15).Value = Sheet3.Cells(j, 9).Value
            n = n + 1
        End If
    Next j
Next i
End Sub
Sub «ö4_Click()
'Selection.Value = "Test2"
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim a As Integer
n = 2
For i = 3 To 10000
    a = 0
    If Len(Sheet3.Cells(i, 1).Value) < 2 Then
        Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
    End If
    For j = 3 To 10000
        If Len(Sheet3.Cells(j, 7).Value) < 2 Then
             Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
        End If
        If Sheet3.Cells(i, 1).Value = Sheet3.Cells(j, 7).Value _
        And Sheet3.Cells(i, 2).Value = Sheet3.Cells(j, 8).Value Then
            a = 1
        End If
    Next j
    If a = 0 Then
       Sheet3.Cells(n, 18).Value = Sheet3.Cells(i, 1).Value
       Sheet3.Cells(n, 19).Value = Sheet3.Cells(i, 2).Value
       Sheet3.Cells(n, 20).Value = Sheet3.Cells(i, 3).Value
       'Selection.Value = i
       n = n + 1
    End If
Next i
End Sub
Sub «ö5_Click()
'Selection.Value = "Test2"
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim a As Integer
n = 2
For i = 3 To 10000
    a = 0
    If Len(Sheet3.Cells(i, 7).Value) < 2 Then
        Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
    End If
    For j = 3 To 10000
        If Len(Sheet3.Cells(j, 1).Value) < 2 Then
             Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
        End If
        If Sheet3.Cells(i, 7).Value = Sheet3.Cells(j, 1).Value _
        And Sheet3.Cells(i, 8).Value = Sheet3.Cells(j, 2).Value Then
            a = 1
        End If
    Next j
    If a = 0 Then
       Sheet3.Cells(n, 23).Value = Sheet3.Cells(i, 7).Value
       Sheet3.Cells(n, 24).Value = Sheet3.Cells(i, 8).Value
       Sheet3.Cells(n, 25).Value = Sheet3.Cells(i, 9).Value
       'Selection.Value = i
       n = n + 1
    End If
Next i
End Sub



循环里面的程序逻辑好像有点问题啊,在循环里面的赋值语句,赋值的时候,没有跟着循环i的变化而变化,导致一直赋值给一个单元格,所以出现的错误现象就是看起来只有最后一行填充了数据进去啊。根据你的需求,修改下这个逻辑。

循环的时候,循环条件要跟随依次变化才行

因为你前面都是 r ,而r是不会变化的
是不是把 i 写成了 r