Kan dit niet makkelijker ??

Status
Niet open voor verdere reacties.

Theknurt

Gebruiker
Lid geworden
11 nov 2006
Berichten
130
Hallo mede excelisten,

Ik heb onderstaande makro gebouwd, die via een USERFORM de optionbuttons gevoed krijgt.

Nou vroeg ik mij af of dit onderstaande misschien op een kortere / makkelijkere manier te (be)schrijven was geweest......


Code:
Option Explicit
Sub weergeven()
Dim selectie As String
Dim rng As String

If OptionButton1 Then

selectie = "Selectienw"
rng = "AV9"
Sorteren
ElseIf OptionButton2 Then

selectie = "SelectiePa"
rng = "CR9"
Sorteren
ElseIf OptionButton3 Then

selectie = "SelectieKo"
rng = "AV9"
Sorteren
ElseIf OptionButton4 Then

selectie = "SelectieBe"
rng = "AV9"
Sorteren
ElseIf OptionButton5 Then

selectie = "SelectieHe"
rng = "AV9"
Sorteren
ElseIf OptionButton6 Then

selectie = "SelectiePi"
rng = "CR9"
Sorteren
ElseIf OptionButton7 Then

selectie = "SelectieKe"
rng = "CR9"
Sorteren
End If

: Sorteren
   Application.ScreenUpdating = False
   Application.Goto Reference:=selectie
    Selection.Sort Key1:=Range(rng), Order1:=xlDescending, Header:=xlGuess _
        , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Range("a1").Select
    Application.ScreenUpdating = True
End Sub

Private Sub CommandButton1_Click()
If Not (OptionButton1 Or OptionButton2 Or OptionButton3 Or OptionButton4 Or OptionButton5 Or OptionButton6 Or OptionButton7) Then
MsgBox "Geen selectie gemaakt"
Else
weergeven
End If
Unload Me
End Sub

Graag hoor ik jullie reactie's.

Txs
Theknurt
 
Code:
Sub weergeven()

    Dim selectie As String
    Dim rng As String

    Select Case True
        
        Case OptionButton1:
            selectie = "Selectienw"
            rng = "AV9"
            
        Case OptionButton2:
            selectie = "SelectiePa"
            rng = "CR9"
        
        Case OptionButton3:
            selectie = "SelectieKo"
            rng = "AV9"
            
        Case OptionButton4:
    
            selectie = "SelectieBe"
            rng = "AV9"

        Case OptionButton5:
    
            selectie = "SelectieHe"
            rng = "AV9"
            
        Case OptionButton6:
    
            selectie = "SelectiePi"
            rng = "CR9"

        Case OptionButton7:
    
            selectie = "SelectieKe"
            rng = "CR9"

    End Select
        
    Application.ScreenUpdating = False
    
    Range(selectie).Sort Key1:=Range(rng), Order1:=xlDescending, Header:=xlGuess, OrderCustom:=1, _
        MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    
    Application.ScreenUpdating = True
    
End Sub

Private Sub CommandButton1_Click()
    
    If Not (OptionButton1 Or OptionButton2 Or OptionButton3 Or OptionButton4 Or OptionButton5 Or OptionButton6 Or OptionButton7) Then
        MsgBox "Geen selectie gemaakt"
    Else
        weergeven
    End If
    
    Unload Me
    
End Sub

Wigi
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan