【问题标题】:Change Number Generation Sequence更改号码生成顺序
【发布时间】:2019-03-13 02:34:21
【问题描述】:

我正在尝试编写一个 VBA 脚本,其中对于数字的每 5 个计数,编号从 1 开始。我正在使用用户输入来循环用户希望设置数字的次数。问题是,数字不是从 1 开始重新开始。请帮忙!!

示例:用户输入 2 次,预期结果是 - 1 2 3 4 5 1 2 3 4 5

代码:

   Dim myValue, myRows As Variant
   Dim i, j, x, y, m, n, k As Integer
   Sub Button1_Click()
   'myValue = InputBox("Enter a starting number")
   myRows = InputBox("Enter how times to repeat")
   'Call repeat(myRows)
   Call repeat2(myRows)
   End Sub

   Sub repeat2(rep1)
   m = 0
   n = 0
   k = 1

   For k = 1 To myRows
   'k = k + 1
    m = m + 5
    n = n + 5
   Call autogen2(k)

   Next k

   End Sub

   Sub autogen2(z)
   For k = 1 To m + 5
   Range("F" & k).Select
   ActiveCell.FormulaR1C1 = k
   ActiveCell.Offset(1, 0).Select
   Next k
   End Sub

【问题讨论】:

  • 您是否尝试过单步调试调试器中的代码,看看为什么它没有按您的意愿工作?

标签: excel vba


【解决方案1】:

您的方法似乎过于复杂,并且会循环遍历每个单元格,而不是遍历目标单元格组。

为了简化计算目标的数学运算,我建议将Option Base 1 作为代码表声明区域中的编译器指令。这会将数组的默认值从基于零更改为基于一;即从0 to 41 to 5

通过使用 LBound 和 UBound,您可以更改数组,而无需更改任何其他塑造目标的代码。

Option Explicit
Option Base 1

Sub Button1_Click()

    Dim myRows As Long, seq As Variant
'myValue = InputBox("Enter a starting number")
    myRows = Application.InputBox(prompt:="Enter how times to repeat", Type:=xlNumbers)

    If IsNumeric(myRows) Then

        seq = Array(1, 2, 3, 4, 5)

        autogen seq, myRows

    End If

End Sub

Sub autogen(vals As Variant, n As Long)

    Dim k As Long

    For k = LBound(vals) To (n * UBound(vals)) Step UBound(vals)
        Cells(1, "F").Offset(k - 1, 0).Resize(UBound(vals), 1) = _
            Application.Transpose(vals)
    Next k

End Sub

【讨论】:

  • 感谢您审阅并提供更好的解决方案。
【解决方案2】:

数字生成器(快速)

调整常量 (Const) 以满足您的需要。

Option Explicit

Sub Button1_Click()

    Dim myRows As Variant

    myRows = InputBox("Enter how times to repeat")
    If IsNumeric(myRows) Then
        Repeat CLng(myRows)
    End If

End Sub

Sub Repeat(Rows As Long)

    Const cRange As String = "F1"   ' First Cell Range Address
    Const cStart As Long = 1        ' Starting Value
    Const cEnd As Long = 5          ' Ending Value

    Dim vntT As Variant             ' Target Array
    Dim i As Long                   ' Rows Counter
    Dim k As Long                   ' Value Counter
    Dim m As Long                   ' Target Array Row Counter

    ' Resize Target Array: Number of Values times Rows.
    ReDim vntT(1 To (cEnd - cStart + 1) * Rows, 1 To 1)
    ' Loop through Rows.
    For i = 1 To Rows
        ' Loop through Values.
        For k = cStart To cEnd
            ' Count Target Array Row.
            m = m + 1
            ' Write current Value to element in current Target Array Row.
            vntT(m, 1) = k
        Next
    Next

    With Range(cRange)
        ' Clear contents of Target Column Range from First Cell Range to
        ' bottom cell.
        .Resize(.Worksheet.Rows.Count - .Row + 1).ClearContents
        ' Calculate Target Range: resize First Cell Range by Target Array Rows.
        ' Copy Target Array to Target Range.
        .Resize(m) = vntT
    End With

End Sub

【讨论】:

  • 感谢您的宝贵时间和回复。
【解决方案3】:

还有一个:

Dim myRows As Long, tot As Long

myRows = Application.InputBox("Enter how times to repeat")
tot = myRows * 5

With ActiveSheet.Range("K1").Resize(tot, 1)
    .FormulaArray = "=1+MOD(ROW(1:" & tot & ")-1,5)"
    .Value = .Value
End With

【讨论】:

  • 感谢您的宝贵时间和回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
  • 2023-04-02
  • 2013-04-01
  • 1970-01-01
  • 2012-04-18
  • 2013-12-11
  • 2019-06-11
相关资源
最近更新 更多