Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
720to724
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
720to724
720to724
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

Überlangen Code kürzen?

Überlangen Code kürzen?
19.01.2006 19:45:44
Lorenz
Guten Abend!
Kann man folgenden Code kürzer gestalten?
Wenn ja, wie?
Der Code dient zur Auswahl von verschiedenen Texten in verschiedensten Spalten.
Der CODE:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
If Target.Row > 5 And Target.Row <= 89 Then
Dim oBar As CommandBar
Dim oBtn As CommandBarButton
Dim wks As Worksheet
Dim iCol As Integer
Set wks = Worksheets("Zeit")
On Error GoTo Fehler
Select Case Target.Column
Case 3
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(5, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(5, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 5
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(1, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(1, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 6
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(12, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(12, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 16, 17, 19, 20
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(4, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(4, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 8, 11
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(2, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(2, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 9, 12
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(3, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(3, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 15
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(13, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(13, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 7, 10, 13, 18, 21
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(11, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(11, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 14
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(6, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(6, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 23
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(7, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(7, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 26
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(9, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(9, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 28
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(10, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(10, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
Case 22, 25, 27, 29
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(8, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(8, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
End Select
End If
Fehler: Cancel = False
Cancel = True
If Target.Row > 89 Then
Cancel = False
End If
End Sub

Das ganze spielt sich in 31 Sheets ab.
Grüße
Lorenz

2
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
soweit ich das überblicke . . .
19.01.2006 20:49:56
J.L.
ist es immer der gleiche Code nur die Zeile ändert sich
also schreibe:
If Target.Row > 5 And Target.Row Dim oBar As CommandBar
Dim oBtn As CommandBarButton
Dim wks As Worksheet
Dim iCol As Integer, iRow as integer
Set wks = Worksheets("Zeit")
On Error GoTo Fehler
Select Case Target.Column
Case 3
iRow = 5
Case 5
iRow = 1
Case usw. ' jetzt hier jeweils immer iRow festlegen
End Eelect
Sheets("Zeit").Visible = xlHidden
Call DeleteCmdBar
Set oBar = Application.CommandBars.Add( _
Name:="StringInsert", _
Position:=msoBarPopup, _
MenuBar:=False, _
temporary:=True)
iCol = 1
Do Until IsEmpty(wks.Cells(iRow, iCol))
Set oBtn = oBar.Controls.Add
With oBtn
.Caption = wks.Cells(iRow, iCol).Value
.Style = msoButtonCaption
.OnAction = "GetValue"
End With
iCol = iCol + 1
Loop
CommandBars("StringInsert").ShowPopup
Sheets("Zeit").Visible = xlVeryHidden
End If
Fehler: Cancel = False
Cancel = True
If Target.Row &gt 89 Then
Cancel = False
End If
End Sub
und schon sollte der Code etwas kürzer sein
Gruß
Jörg
Anzeige
AW: soweit ich das überblicke . . .
19.01.2006 21:13:25
Lorenz
Hallo Jörg!
Richtig überblickt!!
.)Tipp umgesetzt
.)funktioniert
.)sollte der Code etwas kürzer sein!! Na klar!!!!
.)funzt tadellos
.)DANKE!!
Gruß
Lorenz

300 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige