AW: Benutzer Abfrage
13.11.2008 11:31:28
fcs
Hallo Christian,
die Zahl der Spalten kann man z.B. mit einer der beiden nachfolgenden Methoden prüfen
Entweder die letzte Spalte mit Daten oder die Anzahl Spalten mit Daten.
Gruß
Franz
Sub SpalteLoeschen()
Dim wks As Worksheet, varEingabe As Range
On Error GoTo Fehler
' If fncSpalten_mit_Daten(wks:=ActiveSheet) > 3 Then
If fncLetzteSpalte(wks:=ActiveSheet) > 3 Then
Set varEingabe = Application.InputBox( _
Prompt:="Bitte Zelle in zu löschender Spalte wählen", _
Title:="Spalte Löschen", Default:=ActiveCell.Address, Type:=8)
With varEingabe
If MsgBox(Prompt:="Spalte " & .Column _
& " (" & Mid(.Address, 2, InStr(2, .Address, "$") - 2) _
& ") wirklich löschen?", _
Buttons:=vbQuestion + vbOKCancel, Title:="Spalte Löschen") = vbOK Then
ActiveSheet.Columns(varEingabe.Column).Delete
Else
'do nothing
End If
End With
Else
MsgBox "Es sind nur noch 3 Spalten mit Daten vorhanden!"
End If
Fehler:
'Abbruch der Eingabe bei Zellenauswahl führt zu Fehler
If Err.Number 0 Then
If Not varEingabe Is Nothing Then
MsgBox "Fehler-Nr.: " & Err.Number & vbLf & Err.Description
End If
End If
End Sub
Function fncSpalten_mit_Daten(wks As Worksheet) As Long
'Ermittelt die Anzahl Spalten mit Daten
Dim Spalte As Long
With wks
For Spalte = 1 To .Cells.SpecialCells(xlCellTypeLastCell).Column
If Not (IsEmpty(.Cells(1, Spalte)) _
And .Cells(.Rows.Count, Spalte).End(xlUp).Row = 1) Then
fncSpalten_mit_Daten = fncSpalten_mit_Daten + 1
End If
Next
End With
End Function
Function fncLetzteSpalte(wks As Worksheet) As Long
'Ermittelt die Letzte Spalte mit Daten
Dim Spalte As Long
With wks
For Spalte = .Cells.SpecialCells(xlCellTypeLastCell).Column To 1 Step -1
If Not (IsEmpty(.Cells(1, Spalte)) _
And .Cells(.Rows.Count, Spalte).End(xlUp).Row = 1) Then
fncLetzteSpalte = Spalte
Exit For
End If
Next
End With
End Function