AW: Zusatzfrage zum Thema
28.01.2010 16:32:25
Tino
Hallo,
vielleicht würde ich es so machen,
dass Makro kannst Du nach erneuten öffnen von Excel mit Strg+Alt+b starten.
kommt als Code in DieseArbeitsmappe
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^%b"
End Sub
Private Sub Workbook_Open()
Application.OnKey "^%b", "Test_Leer_Spalten_Aus"
End Sub
kommt als Code in Modul1
Option Explicit
Function ZaehleSichtbareLeere(ByVal Bereich As Range, MaxOG As Long) As Boolean
Dim LCounter As Long
Dim LAnzahlZellen As Long
On Error Resume Next
Set Bereich = Bereich.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Bereich Is Nothing Then Exit Function
If Bereich.Cells.Count = MaxOG Then Exit Function
LAnzahlZellen = Bereich.Cells.Count
With Application.WorksheetFunction
For Each Bereich In Bereich.Areas
LCounter = LCounter + .CountIf(Bereich, "")
Next Bereich
End With
ZaehleSichtbareLeere = LCounter = (LAnzahlZellen - MaxOG)
End Function
Sub Test_Leer_Spalten_Aus()
Dim Bereich As Range, booIsLeer As Boolean
Dim tmpRng As Range
Dim oSh As Worksheet
Static booVisible As Boolean
With ActiveWorkbook.ActiveSheet
If Not booVisible Then
Set Bereich = .UsedRange
For Each Bereich In Bereich.Columns
If ZaehleSichtbareLeere(Bereich, 1) Then
If Not tmpRng Is Nothing Then
Set tmpRng = Union(Bereich, tmpRng)
Else
Set tmpRng = Bereich
End If
End If
Next Bereich
If Not tmpRng Is Nothing Then
tmpRng.EntireColumn.Hidden = True
booVisible = True
End If
Else
.UsedRange.EntireColumn.Hidden = False
booVisible = False
End If
End With
End Sub
Gruß Tino