0

我的查询是如果我使用按钮插入行,它还应该将序列号添加到 1、2、3 等行中......

我在工作表的 Sheet1 中有以下代码,用于在添加行时添加序列号

Private Sub Worksheet_Change1(ByVal Target As Range)
    Dim StartNum As Integer
    Dim FirstCell As Integer
    Dim LastCell As Integer

    StartNum = 2
    FirstCell = 3
    LastCell = 17

    Application.EnableEvents = False
    Do While FirstCell <= LastCell
        Range("B" & FirstCell).Value = StartNum
        FirstCell = FirstCell + 1
        StartNum = StartNum + 1
    Loop
    Range("B" & LastCell + 1).Value = ""
    Application.EnableEvents = True
End Sub

下面的代码写在 module1 中以插入行,将 A1 的公式复制到新行

Sub Macro2()
    Rows("2:2").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B1:D1").Select
    Selection.AutoFill Destination:=Range("B1:D2"), Type:=xlFillDefault
    Range("B1:D2").Select
End Sub

现在我的问题是如何在插入行时从 Module Macro2 代码调用私有子

任何建议,第一时间等待您的回复。

4

1 回答 1

0

就像我提到的那样,您不需要此Worksheet_Change代码。将以下代码粘贴到模块中并尝试..

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, i As Long

    '~~> Set this to the relevant sheet
    Set ws = Sheets("Sheet1")

    With ws
        '~~> Insert at row 2
        .Rows(2).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        '~~> Autofill B1:D1 to C1:D2
        .Range("B1:D1").AutoFill Destination:=.Range("B1:D2"), Type:=xlFillDefault

        '~~> Find the last row
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lRow = .Cells.Find(What:="*", _
                               After:=.Range("A1"), _
                               Lookat:=xlPart, _
                               LookIn:=xlFormulas, _
                               SearchOrder:=xlByRows, _
                               SearchDirection:=xlPrevious, _
                               MatchCase:=False).Row
        Else
            lRow = 1
        End If

        '~~> Renumber the cells in Col B
        For i = 1 To lRow
            .Range("B" & i).Value = i
        Next i
    End With
End Sub

跟进

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, i As Long, j As Double

    '~~> Set this to the relevant sheet
    Set ws = Sheets("Sheet1")

    With ws
        '~~> Insert at row 2
        .Rows(2).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        '~~> Autofill B1:D1 to C1:D2
        .Range("B1:D1").AutoFill Destination:=.Range("B1:D2"), Type:=xlFillDefault

        '~~> Find the last row
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lRow = .Cells.Find(What:="*", _
                               After:=.Range("A1"), _
                               Lookat:=xlPart, _
                               LookIn:=xlFormulas, _
                               SearchOrder:=xlByRows, _
                               SearchDirection:=xlPrevious, _
                               MatchCase:=False).Row
        Else
            lRow = 1
        End If

        '~~> Renumber the cells in Col B 1,1.1,1.2,1.3 etc
        j = 1

        For i = 1 To lRow
            .Range("B" & i).Value = j
            j = j + 0.1
        Next i
    End With
End Sub
于 2013-01-16T14:42:49.883 回答