Excel:基于输入的动态数字列表

时间:2019-03-12 20:38:03

标签: excel vba

我正在尝试根据硬编码输入列出数字。

如果我在A1中写“ 5”,那么我想在下面的列中列出数字1,2,3,4,5。如果输入为25,则列表将增加到1-25,依此类推。...

有人可以帮我吗?

最诚挚的问候

3 个答案:

答案 0 :(得分:2)

您需要一个Worksheet_Change()事件:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$A$1" Then

        Range("A2:A1048576").ClearContents

        For i = 1 To Target.Value
            Cells(i + 1, 1).Value = i
        Next i

    End If

End Sub

答案 1 :(得分:2)

使用公式:

A2

使用以下公式:

=IFERROR(IF(A1="","",1),"")

A3中使用:

=IFERROR(IF(A2+1>A$1,"",A2+1),"")

然后填充

答案 2 :(得分:1)

编号

功能

  • 通过用户输入或通过VBA更改Source Cell Range中的值时, 程序被触发。如果Worksheet Calculate包含公式并且值由于另一个单元格的更改而更改,则它将不会运行。对于该功能,您必须使用1.5事件。
  • 如果该值不是数字,则不会发生任何事情。
  • 如果该值为十进制数字(2),则将四舍五入到最接近的值 整数(Source Cell Range Address
  • 如果该值超过工作表中的行数,则 工作表将填充到底部单元格中。其余的值 将被忽略。
  • 更改Worksheet_Change中的A1(设置为C17)以适合您的需求,例如VBE >> Insert >> Module仅以下单元格会受到影响。

代码

例如,将以下代码复制到标准模块Option Explicit Sub Numbered(CellRange As Range) Dim vntT As Variant ' Target Array/Value Dim srcVal As Variant ' Value Dim srcMax As Long ' Maximum Value Dim srcSgn As Long ' Sign (+-) Dim srcAbs As Long ' Absolute Value Dim i As Long ' Target Array Row Counter ' In Cell Range With CellRange ' Write value of CellRange to Value. srcVal = .Value ' Calculate Maximum Value. srcMax = .Worksheet.Rows.Count - .Offset(1).Row + 1 End With ' Check if Value is a number. If IsNumeric(srcVal) Then ' Convert Value to whole number. srcVal = CLng(srcVal) ' Write the sign of Value to Sign. srcSgn = Sgn(srcVal) ' Check if the absolute value of Value is greater than Maximum Value. If Abs(srcVal) > srcMax Then ' Write Maximum Value with (correct) Sign to Value. srcVal = srcSgn * srcMax End If ' Write the absolute value of Value to Absolute Value. srcAbs = Abs(srcVal) ' Check Absolute Value Select Case srcAbs Case Is > 1 ' Resize Target Array to Absolute Value rows and one column. ReDim vntT(1 To srcAbs, 1 To 1) ' Loop through rows of Target Array. For i = 1 To srcAbs ' Write to element at i-th row and 1st column ' of Target Array. vntT(i, 1) = srcSgn * i Next Case 1 ' If Absolute Value is 1, vntT will not be an array, but a ' variant containing one value. vntT = srcSgn * 1 Case 0 ' If Absolute Value is 0, vntT will not be an array, but a ' variant containing one value. vntT = 0 ' or "" End Select End If ' In First Cell of Target Range (Cell Below Cell Range) With CellRange.Offset(1) ' Resize to bottom cell and clear contents. .Resize(srcMax).ClearContents ' Check if vntT is an array. If IsArray(vntT) Then ' Multiple values ' Calculate Target Range: Resize First Cell of Target Range by ' Absolute Value. ' Copy Target Array to Target Range. .Resize(srcAbs) = vntT Else ' One value ' Write Target Value to First Cell of Target Range. .Value = vntT End If End With End Sub

Module1

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    Const cSrc As String = "A1"  ' Source Cell Range Address

    If Target.Address = Range(cSrc).Address Then
        Numbered Target
    End If

End Sub

将以下代码复制到任何要运行该程序的工作表模块

Sheet1

{{1}}
相关问题