Liliana Martinez
Liliana Martinez

Reputation: 5

How to use If..Then properly?

I'm the newest girl using VBA for Excel, so I've been doing a lot of research but since this isn't my knowledge field I'm pretty sure I'm missing something. I used an UserForm for selecting 5 different columns of data, if you choose option 1, you get data from "x" column, if you choose option 2, you get data from "y" column and so on. This data has to be copied and pasted from one worksheet to another, but I can't make it work.
Can you tell me what am I doing wrong?

Oh yeah! As a background, this isn't mine. The workbook was sent to me, and I have to edit it according to the needs of my workplace.

This is my code.

Private Sub UserForm_Click()
    If UserForm6.OptionButton1.Value = True Then
        Sheets("Operations").Select
        Range("B7:M33").Select
        Selection.ClearContents
        Sheets("Processes").Select
        Range("D2:D33").Select
        Selection.Copy
        Sheets("Operations").Select
        Range("B7").Select
        Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

    ElseIf UserForm6.OptionButton2.Value = True Then

        Sheets("Operations").Select
        Range("B7:M33").Select
        Selection.ClearContents
        Sheets("Processes").Select
        Range("I2:I33").Select
        Selection.Copy
        Sheets("Operations").Select
        Range("B7").Select
        Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

    ElseIf UserForm6.OptionButton3.Value = True Then

        Sheets("Operations").Select
        Range("B7:M33").Select
        Selection.ClearContents
        Sheets("Processes").Select
        Range("N2:N33").Select
        Selection.Copy
        Sheets("Operations").Select
        Range("B7").Select
        Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

    ElseIf UserForm6.OptionButton4.Value = True Then

        Sheets("Operations").Select
        Range("B7:M33").Select
        Selection.ClearContents
        Sheets("Processes").Select
        Range("S2:S33").Select
        Selection.Copy
        Sheets("Operations").Select
        Range("B7").Select
        Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

    ElseIf UserForm6.OptionButton5.Value = True Then

        Sheets("Operations").Select
        Range("B7:M33").Select
        Selection.ClearContents
        Sheets("Processes").Select
        Range("X2:X33").Select
        Selection.Copy
        Sheets("Operations").Select
        Range("B7").Select
        Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

    End If
End Sub

I'd love any help you can give! And by the way, English isn't my native language so I hope you can understand most of what I'm asking.

Thank you!

Upvotes: 0

Views: 96

Answers (2)

Scott Craner
Scott Craner

Reputation: 152585

We can avoid the clipboard by setting the values directly and by using a range object we can rewrite a little and avoid the duplicate code:

Private Sub UserForm_Click()
    Dim rng As Range

    With Worksheets("Processes")
        Select Case True
            Case Me.OptionButton1.Value
                Set rng = .Range("D2:D33")
            Case Me.OptionButton2.Value
                Set rng = .Range("I2:M33")
            Case Me.OptionButton3.Value
                Set rng = .Range("N2:N33")
            Case Me.OptionButton4.Value
                Set rng = Range("S2:S33")
            Case Me.OptionButton5.Value
                Set rng = .Range("X2:X33")
        End Select
    End With
    With Worksheets("Operations")
        .Range("B7:M33").ClearContents
        .Range("B7").Resize(rng.Rows.Count).Value = rng.Value
    End With
End Sub

Upvotes: 2

TourEiffel
TourEiffel

Reputation: 4424

Your code without Select would be more readable and also improve performances:

Private Sub UserForm_Click()
    If Me.OptionButton1.Value = True Then
        Sheets("Operations").Range("B7:M33").ClearContents
        Sheets("Processes").Range("D2:D33").Copy
        Sheets("Operations").Range("B7").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks:=False, Transpose:=False

    ElseIf Me.OptionButton2.Value = True Then

        Sheets("Operations").Range("B7:M33").ClearContents
        Sheets("Processes").Range("I2:I33").Copy
        Sheets("Operations").Range("B7").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks:=False, Transpose:=False

    ElseIf Me.OptionButton3.Value = True Then

        Sheets("Operations").Range("B7:M33").ClearContents
        Sheets("Processes").Range("N2:N33").Copy
        Sheets("Operations").Range("B7").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks:=False, Transpose:=False

    ElseIf Me.OptionButton4.Value = True Then

        Sheets("Operations").Range("B7:M33").ClearContents
        Sheets("Processes").Range("S2:S33").Copy
        Sheets("Operations").Range("B7").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks:=False, Transpose:=False

    ElseIf Me.OptionButton5.Value = True Then

        Sheets("Operations").Range("B7:M33").ClearContents
        Sheets("Processes").Range("X2:X33").Copy
        Sheets("Operations").Range("B7").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks:=False, Transpose:=False

    End If
End Sub

Upvotes: 1

Related Questions