Excel VBA-Code zum Hinzufügen einer zusätzlichen Zeile

Excel VBA-Code zum Hinzufügen einer zusätzlichen Zeile

Ich erstelle VBA-Code in Excel, um eine Zeile hinzuzufügen und zu formatieren.

Ich brauche den Wert für „i“ als Variable (statt 20 wie gezeigt), abhängig von der Anzahl der Einträge in der ersten Spalte meiner Excel-Tabelle.

Sub NextLine()

'
' AddLine Macro
' Adds Line

Dim i As Integer
i = 20

    ActiveCell.Offset(1, 0).Select '1 row down
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.SmallScroll ToRight:=1
    Range("$A$1:$M$" & i).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlHairline
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlHairline
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlHairline
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlHairline
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlHairline
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlHairline
    End With
    ActiveSheet.PageSetup.PrintArea = "$A$1:$M$" & i

End Sub

Antwort1

Ersetzen Sie die Zeile:

i=20

mit:

With ActiveSheet
    i = .Cells(.Rows.Count, "A").End(xlUp).Row 
End With

Es zählt die Anzahl der Zeilen mit Daten in Spalte A.

Übrigens habe ich Ihr Makro getestet und es fügt keine Zeile hinzu.

verwandte Informationen