Copy data from one sheet to another

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Copy data from one sheet to another

Post by ABabeNChrist »

I have mutable worksheets within my workbook; on each of these worksheets I have a command button that will open a separate UserForm pertaining to that worksheet, from there the user may select varies checkboxes that refer to different locations on this sheet. So that when selection is made it would then copy selected data over to the Summary sheet at designated location from code.
Here is the code I was able to come up with it seems to work Ok
Am I missing anything?

Code: Select all

Private Sub CommandButton1_Click()
'Utilities to Summary
    ActiveWorkbook.Protect Password:="benji", Structure:=False, Windows:=False

    If CheckBox1.Value = True Then
            Sheets("Summary").Range("A9:A10").EntireRow.Hidden = False
            Sheets("Summary").Range("A9").Value = Range("A7").Value & _
                                                  ": " & Range("A10").Value
            Sheets("Summary").Range("A10").Value = " o " & Range("A16").Value

        Else
            Sheets("Summary").Range("A9:A10").EntireRow.Hidden = True
            Sheets("Summary").Range("A9:A10").Value = ""
        End If

        If CheckBox2.Value = True Then
                Sheets("Summary").Range("A12").EntireRow.Hidden = False
                Sheets("Summary").Range("A12").Value = Range("A18").Value & _
                                                       ": " & Range("A21").Value
                Sheets("Summary").Range("A13").Value = Range("A27").Value

            Else
                Sheets("Summary").Range("A12:A13").EntireRow.Hidden = True
                Sheets("Summary").Range("A12:A13").Value = ""
            End If

      If CheckBox3.Value = True Then

            End If

      If CheckBox4.Value = True Then

            End If

            ActiveWorkbook.Protect Password:="benji", Structure:=True, Windows:=True

            Unload Me
            Unload UserForm5
End Sub

User avatar
HansV
Administrator
Posts: 78596
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy data from one sheet to another

Post by HansV »

ABabeNChrist wrote:Am I missing anything?
Correct indentation - the Else and End If belonging to an If ... Then line should begin straight below the If.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Copy data from one sheet to another

Post by ABabeNChrist »

Thank you Hans
Now that you pointed out the Else and End If, it made me realize that anything after the Else to the End If is not even needed at all. I only need the first action to occur when checkbox is selected. So I shorten the code too.

Code: Select all

Private Sub CommandButton1_Click()
'Utilities to Summary
    ActiveWorkbook.Protect Password:="benji", Structure:=False, Windows:=False

    If CheckBox1.Value = True Then
            Sheets("Summary").Range("A9:A10").EntireRow.Hidden = False
            Sheets("Summary").Range("A9").Value = Range("A7").Value & _
                                                  ": " & Range("A10").Value
            Sheets("Summary").Range("A10").Value = " o " & Range("A16").Value
    End If
            
    If CheckBox2.Value = True Then
            Sheets("Summary").Range("A12").EntireRow.Hidden = False
            Sheets("Summary").Range("A12").Value = Range("A18").Value & _
                                                       ": " & Range("A21").Value
            Sheets("Summary").Range("A13").Value = Range("A27").Value
    End If
      
            ActiveWorkbook.Protect Password:="benji", Structure:=True, Windows:=True

            Unload Me
            Unload UserForm5
End Sub

User avatar
HansV
Administrator
Posts: 78596
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy data from one sheet to another

Post by HansV »

The part between the last "End If" and the final "End Sub" should begin straight below the "End If". It shouldn't be indented as if it's between "If" and "End If".
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Copy data from one sheet to another

Post by ABabeNChrist »

Thank you Hans
Most greatly appreciated :thankyou:
I'm slowly starting to figure this out, somewhat :dizzy: