Vba 取单元格值并在下面添加那么多行

Vba 取单元格值并在下面添加那么多行,vba,excel,rows,Vba,Excel,Rows,我有一张Excel表格,其中有A1到A10范围内的不同数字。我需要从单元格中获取值,并在该单元格下添加那么多行 假设A1作为3作为值,宏应该在A1下面添加2行 我试过使用“行”函数,但找不到出路 请帮助。Dim i,max,num作为整数 i=1 最大值=10 趁我不在的时候做 范围(“A”和i)。选择 num=Selection.Value 作为整数的Dim x x=0 当x

我有一张Excel表格,其中有A1到A10范围内的不同数字。我需要从单元格中获取值,并在该单元格下添加那么多行

假设A1作为3作为值,宏应该在A1下面添加2行

我试过使用“行”函数,但找不到出路


请帮助。

Dim i,max,num作为整数
i=1
最大值=10
趁我不在的时候做
范围(“A”和i)。选择
num=Selection.Value
作为整数的Dim x
x=0
当x结束子节点

尺寸i、最大值、数值为整数
i=1
最大值=10
趁我不在的时候做
范围(“A”和i)。选择
num=Selection.Value
作为整数的Dim x
x=0
当x结束子项

这应该可以让您继续。如果你需要进一步的帮助,请告诉我

Sub CellsValue()
    Dim Colm As Integer
    Dim lastrow As Long, deflastrow As Long

    'Get the Position of the Required column which has the numbers that it has to shift towards
    Colm = WorksheetFunction.Match("Cells Value", Sheets("Sheet1").Rows(1), 0)

    'Get the lastrow of that column
    lastrow = ActiveSheet.Cells(Rows.Count, Colm).End(xlUp).Row
    deflastrow = Application.Sum(Range(Cells(1, Colm), Cells(lastrow, Colm)))

    For i = 2 To deflastrow + lastrow
        Range("A" & i + 1).Select
        InsertRow = Range("A" & i).Value

        If InsertRow > 0 Then
            InsertRow = InsertRow - 1
        End If

        If InsertRow = 0 Then
            Range("A" & i + 1).Select
        Else
            For j = 1 To InsertRow
                Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            Next
        End If
    Next
End Sub

我已经做了改变。现在它可以工作了。如果答案对您有效,请接受。

这应该会让您满意。如果你需要进一步的帮助,请告诉我

Sub CellsValue()
    Dim Colm As Integer
    Dim lastrow As Long, deflastrow As Long

    'Get the Position of the Required column which has the numbers that it has to shift towards
    Colm = WorksheetFunction.Match("Cells Value", Sheets("Sheet1").Rows(1), 0)

    'Get the lastrow of that column
    lastrow = ActiveSheet.Cells(Rows.Count, Colm).End(xlUp).Row
    deflastrow = Application.Sum(Range(Cells(1, Colm), Cells(lastrow, Colm)))

    For i = 2 To deflastrow + lastrow
        Range("A" & i + 1).Select
        InsertRow = Range("A" & i).Value

        If InsertRow > 0 Then
            InsertRow = InsertRow - 1
        End If

        If InsertRow = 0 Then
            Range("A" & i + 1).Select
        Else
            For j = 1 To InsertRow
                Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            Next
        End If
    Next
End Sub
我已经做了改变。现在它可以工作了。如果答案对您有效,请接受。

替代解决方案:

Sub tgr()

    Dim ws As Worksheet
    Dim i As Long

    Const sCol As String = "A"

    Set ws = ActiveWorkbook.ActiveSheet
    For i = ws.Cells(ws.Rows.Count, sCol).End(xlUp).Row - 1 To 1 Step -1
        With ws.Cells(i, sCol)
            If IsNumeric(.Value) Then
                If .Value - 1 > 0 Then .Offset(1).Resize(.Value - 1).EntireRow.Insert xlShiftDown
            End If
        End With
    Next i

End Sub
替代解决方案:

Sub tgr()

    Dim ws As Worksheet
    Dim i As Long

    Const sCol As String = "A"

    Set ws = ActiveWorkbook.ActiveSheet
    For i = ws.Cells(ws.Rows.Count, sCol).End(xlUp).Row - 1 To 1 Step -1
        With ws.Cells(i, sCol)
            If IsNumeric(.Value) Then
                If .Value - 1 > 0 Then .Offset(1).Resize(.Value - 1).EntireRow.Insert xlShiftDown
            End If
        End With
    Next i

End Sub

到目前为止,你能显示你的代码吗?这是我所有行中最成功的一次尝试(ActiveCell.Row&“:”&ActiveCell.Row+ActiveCell.Value-1)。Insert shift:=xlDown但这会在活动单元格上方添加单元格。你能显示你的代码吗?这是我所有行中最成功的一次尝试(ActiveCell.Row&“:”&ActiveCell.Row+ActiveCell.Value-1).Insert shift:=xlDown,但这会在活动单元格上方添加单元格。代码会动态工作。无论列A中的行数是多少,它都应该工作。单元格值只是列A中的“我的列标题”。请记住,lastrow将通过添加rows@2red13你现在在想什么?我相信这会奏效。我测试了它,它工作了注意:
Dim lastrow,deflastrow As Long
只声明最后一个变量为
Long
,另一个变量保留为
Variant
。使用
Dim lastrow As Long,deflastrow As Long
将两者声明为
Long
@Sid29仅供参考。您所做的工作在VB(真正的Visual Basic)中运行良好,但在VBA(Office)中不起作用。在VBA中,需要指定每个变量的类型,否则它默认为
Variant
。代码动态工作。无论列A中的行数是多少,它都应该工作。单元格值只是列A中的“我的列标题”。请记住,lastrow将通过添加rows@2red13你现在在想什么?我相信这会奏效。我测试了它,它工作了注意:
Dim lastrow,deflastrow As Long
只声明最后一个变量为
Long
,另一个变量保留为
Variant
。使用
Dim lastrow As Long,deflastrow As Long
将两者声明为
Long
@Sid29仅供参考。您所做的工作在VB(真正的Visual Basic)中运行良好,但在VBA(Office)中不起作用。在VBA中,需要指定每个变量的类型,否则它默认为
Variant
。您知道
Dim i,max,num作为整数
只声明
num
作为整数,而将
i
max
作为变量吗?将这三个变量都声明为整数应该是
Dim i作为整数,max作为整数,num作为整数
(这里VBA与VB非常不同!)。您应该指定的
shift
参数(如果省略此参数,Excel将根据范围的形状决定,这可能会失败!)答案可能需要一些高质量的更新,只是发布格式错误的代码远远不是一个好答案。您知道
Dim i,max,num作为整数
仅声明
num
作为整数,并将
i
max
作为变量?将这三个变量都声明为整数应该是
Dim i作为整数,max作为整数,num作为整数
(这里VBA与VB非常不同!)。您应该指定的
shift
参数(如果省略此参数,Excel将根据范围的形状决定,这可能会失败!)答案可能需要一些高质量的更新,只是发布格式错误的代码远远不是一个好答案。