Excel VBA - Finde einen Fehler nicht

jerry0110

Erfahrenes Mitglied
Hi,

habe jetzt alles schon so hinbekommen wie ich es wollte. Nur einen Fehler bekomme ich nicht in der Griff. Ich habe von Yaslaw 2 Funktionen zum errechnen der letzten Spalte und Zeile:



Visual Basic:
Public Function xlsGetLastRow(ByRef Sheet As Excel.Worksheet) As Long
    Dim r As Variant
    xlsGetLastRow = Sheet.Cells.SpecialCells(xlCellTypeLastCell).Row
    For r = xlsGetLastRow To 1 Step -1
        If Sheet.Application.WorksheetFunction.CountA(Sheet.Rows(r)) = 0 Then
            xlsGetLastRow = r - 1
        Else
            Exit For
        End If
    Next r
End Function

Und:

Visual Basic:
Public Function xlsGetLastCol(ByRef Sheet As Excel.Worksheet) As Long
    Dim i As Variant
    xlsGetLastCol = Sheet.Cells.SpecialCells(xlCellTypeLastCell).Column
    For i = xlsGetLastCol To 1 Step -1
        If Sheet.Application.WorksheetFunction.CountA(Sheet.Columns(i)) = 0 Then
            xlsGetLastCol = i - 1
        Else
            Exit For
        End If
    Next i
End Function

Und ich möchte die Überschrift anpassen mit:

Visual Basic:
         With Worksheets("Tabelle1")
            .Columns("A:A").ColumnWidth = 10.8
            .Columns("B:B").ColumnWidth = 12
            .Columns("C:C").ColumnWidth = 12.6
            .Columns("D:D").ColumnWidth = 15
            .Columns("E:E").ColumnWidth = 16
            .Columns("F:F").ColumnWidth = 14
            .Columns("G:G").ColumnWidth = 13
            .Columns("H:H").ColumnWidth = 16.6
            .Columns("I:I").ColumnWidth = 16
            .Columns("J:J").ColumnWidth = 9.4
            .Columns("K:K").ColumnWidth = 6.4
            .Columns("L:L").ColumnWidth = 24
            .Columns("M:M").ColumnWidth = 55
            .Columns("N:N").ColumnWidth = 11.33
            .Columns("O:P").ColumnWidth = 55
            .Columns("Q:Q").ColumnWidth = 17
            .Columns("R:" & lastCol).ColumnWidth = 12
            .Rows("1:2000").AutoFit
            .Columns("A:T").WrapText = True
        End With
       
        With Range("A1", Cells(lastRow, lastCol)).Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = 1
        End With
        With Range("A1", Cells(lastRow, lastCol)).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = 1
        End With
        With Range("A1", Cells(lastRow, lastCol)).Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = 1
        End With
        With Range("A1", Cells(lastRow, lastCol)).Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = 1
        End With
        With Range("A1", Cells(lastRow, lastCol)).Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = 1
        End With
        With Range("A1", Cells(lastRow, lastCol)).Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = 1
        End With
       
        Range("A1", Cells(lastRow, lastCol)).Range(Cells(1, 1), Cells(1, lastCol)).Interior.ColorIndex = 6
        Range("A1", Cells(lastRow, lastCol)).Range(Cells(1, 1), Cells(1, lastCol)).Font.Bold = True

Und er macht folgendes. Er geht nur bis Spalte "I" und dann bis zur Zeile "23".
Warum? Was mache ich falsch. Bei meinen anderen Macros funktioniert alles.
Die Zellen da drunter sind auch gefüllt. Also keine Leeren.
 
Habe den Fehler selber rausgefunden.

Da ich eine Datei nutze um auf eine andere Datei zuzugreifen, habe ich bei dem Code "activeworkbook" vergessen zu schreiben.
Dadurch hat er bei meinem Workbook wo das Makro drin liegt die Fläche genommen und nicht von dem erstellen Workbook was aktive war.

Man muss nur selber "lange" suchen! :D
 
Zurück