【问题标题】:Excel VBA Nested Loops to start count from 0 againExcel VBA嵌套循环再次从0开始计数
【发布时间】:2018-09-26 11:28:43
【问题描述】:

我正在编写一个脚本以在消息框中打印,单元格值和重复数从 1 到 5 计数。

目前,我有一个 for 循环来计算电子表格中的总行数。我不确定如何添加另一个 for 循环(嵌套 for 循环)来调用程序以将 1 到 5 添加到前 5 行,并在第 6 行再次从 1 到 5 重新启动,依此类推。

例如,

如果单元格 A1 到 A10 中的值分别为“Apple”,我想将 1 到 5 的数字连接起来,以便得到以下结果:

A1 = "Apple1"
A2 = "Apple2"
A3 = "Apple3"
A4 = "Apple4"
A5 = "Apple5"
A6 = "Apple1" 'it starts from 1 again
A7 = "Apple2"

等等

下面是我的示例代码:

Option Explicit
Sub appendCount()
    Dim q, i, rowStart, rowEnd , rowNum, LR as Long

    LR = Cells(Rows.Count, 1).End(xlUp).Row
    rowNum = Range("A1:A" & LR).Count

    For q = 1 To rowNum Step 1
        If Not IsNull(Range("A" & q)) Then
        For i = 1 to 5        
            MsgBox Range("A" & q).Value & i
        Next i
        End If
    Next q
End Sub

任何帮助将不胜感激!

【问题讨论】:

  • 我反复阅读你的句子,但不能确定你想要什么。什么有效,你想要什么?
  • 我想将数字连接到每个单元格中的值。例如,单元格 A1、A2、A3、A4、A5 的值为“apple”。我想在每个单元格中添加从 1 到 5 的数字,并在之前以 5 结束时从 1 开始重复。所以我会得到 A1 = “apple1”,A2 = “apple2”,A3 = “Apple3”,A4 = “Apple4”,A5 = “Apple5”,A6 = “Apple1”,A7 = “Apple2”....
  • rowNum 似乎是不必要的。您已经在使用 LR 变量计算最后一行。
  • 请注意Dim q, i, rowStart, rowEnd , rowNum, LR as Long 仅声明LR as Long 而其他所有声明为Variant。您必须为 VBA 中的 每个 变量指定一个类型:Dim q as Long, i as Long, rowStart as Long, rowEnd as Long, rowNum as Long, LR as Long 否则 VBA 会自动假定为 Variant

标签: vba excel


【解决方案1】:

我相信以下内容会达到您的预期,它将查看 A 列上的值并将计数添加到 B 列上:

Option Explicit

Sub appendCount()
    Dim LR As Long, rownumber As Long, counter As Long
    Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
    'declare and set the worksheet you are working with, amend as required
    counter = 0
    LR = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    For rownumber = 1 To LR Step 1
        If Not IsEmpty(ws.Range("A" & rownumber)) Then
            counter = counter + 1
            If counter = 6 Then counter = 1
            ws.Range("B" & rownumber).Value =ws.Range("A" & rownumber).value & counter
        End If
    Next rownumber
End Sub

【讨论】:

    【解决方案2】:

    单元格上的IsNull() 将始终返回False。将IsNull 替换为IsEmpty,
    或使用someCell <> ""

    https://stackoverflow.com/a/2009754/78522

    【讨论】:

      【解决方案3】:

      使用数组会更快。此外,mod 会因大量数字而失败,因此编写以下内容以处理大量数字。开始重新编号的点也被放入一个常量中,以便于更改。因此,整体代码更加灵活和有弹性。

      Option Explicit
      Public Sub AddNumbering()
          Dim arr(), i As Long, lastRow As Long, index As Long
          Const RENUMBER_AT = 6
      
          With ThisWorkbook.Worksheets("Sheet1")
              lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
              Select Case lastRow
              Case 1
                  ReDim arr(1, 1): arr(1, 1) = .Range("A1").Value
              Case Else
                  arr = .Range("A1:A" & lastRow).Value
              End Select
              index = 1
              For i = LBound(arr, 1) To UBound(arr, 1)
                  If arr(i, 1) <> vbNullString Then
                      If i - (CLng(i / RENUMBER_AT) * RENUMBER_AT) <> 0 And i <> 1 Then
                          index = index + 1
                      Else
                          index = 1
                      End If
                      arr(i, 1) = arr(i, 1) & CStr(index)
                  End If
              Next
              .Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
          End With
      End Sub
      

      【讨论】:

        【解决方案4】:

        我了解您的问题是单元格 A1 到 A10 中的值分别是“Apple”,您想要内容数字从 1 到 5,然后 A6 到 A10 内容数字也从 1 到 5。

        这是我的测试代码,你可以试试:

        选项显式

        Sub appendCount()
            Dim q, i, cou, rowStart, rowEnd, rowNum, LR As Long
        
        LR = Cells(Rows.count, 1).End(xlUp).Row
        rowNum = Range("A1:A" & LR).count
        cou = 1
        
        For q = 1 To rowNum Step 1
            If Not IsNull(Range("A" & q)) Then
        
              For i = 1 To 5
                MsgBox Range("A" & q).Value & cou
                cou = cou + 1
                If cou = 6 Then
                    cou = 1
                End If
              Next i
            End If
        Next q
        End Sub
        

        【讨论】:

          【解决方案5】:

          您的声明是错误的,尽管您可能期望这些变量没有声明为 Long 而是声明为 Variant: q, i, rowStart, rowEnd , rowNum 您必须分别包含每个变量的类型。

          这段代码应该可以为您解决问题:

          Sub appendCount()
              Dim q As Long, LR As Long, rowNum As Long
          
              LR = Cells(Rows.Count, 1).End(xlUp).Row
              rowNum = Range("A1:A" & LR).Count
          
              For q = 1 To rowNum Step 1
                  If Not Len(Range("A" & q).Value) = 0 Then
                      If q Mod 5 = 0 Then
                          MsgBox Range("A" & q).Value & 5
                      Else
                          MsgBox Range("A" & q).Value & (q Mod 5)
                      End If
                  End If
              Next q
          End Sub
          

          【讨论】:

            【解决方案6】:
            Sub appendCount()
            Dim q, c, i, rowStart, rowEnd, rowNum, LR As Long
            
            LR = Cells(Rows.Count, 1).End(xlUp).Row
            rowNum = Range("A1:A" & LR).Count
            
            c = 1
            For q = 1 To rowNum Step 1
                If Not IsEmpty(Range("A" & q)) Then
                If (c Mod 6) <> 0 Then
                Range("B" & q).Value = Range("A" & q).Value & (c Mod 6)
                Else
                c = c + 1
                Range("B" & q).Value = Range("A" & q).Value & (c Mod 6)
                End If
                End If
            c = c + 1
            Next q
            

            结束子

            【讨论】:

              【解决方案7】:

              这样就可以了:

              Sub Loops()
              Dim i As Long, iMultiples As Long, iMultiple As Long
              
              iMultiples = WorksheetFunction.Ceiling_Math(Cells(Rows.Count, 1).End(xlUp).Row, 5, 0) ' this rounds up to the nearest 5 (giving the number of multiples
              
              For iMultiple = 1 To iMultiples
                  For i = 1 To 5
                      If Not IsNull(Range("A" & i).Value) Then Range("A" & i).Value = "Apple" & i 'This can be tweaked as needed
                  Next
              Next
              
              
              End Sub
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-06-30
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多