If CheckBox is True, copy all rows

taNkbilen123

New Member
Joined
Feb 14, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi,
Im new to this site and also creating macros, but this macro feels like it should be easy…
I have a checkbox that hide and unhide rows. If that checkbox is true when i press a specific button i want the rows that are visible to be copied into a new sheet for printing otherwise not. But it doesn't work for me...

The code for the checkbox that hide and unhide rows:

Code:
Private Sub cb_as_Click()
If Me.cb_as = True Then
    Rows("182:191").Select
    Selection.EntireRow.Hidden = False
    Range("H177").Select
Else
    Rows("182:191").Select
    Selection.EntireRow.Hidden = True
    Range("H177").Select
End If

End Sub

And the code for the button that should copy the rows if the checkbox is true:

Code:
Sub GenereraRB()

    If cb_as = True Then
    Sheets("Data").Select
    Range("A180:H191").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("sheet 2").Select
    Range("A43").Select
    ActiveSheet.Paste
End If

End Sub


If i delete the if-statement in the code for the button it always copy the rows. But i only want it to copy if the checkbox is true.

Can someone help me?

Thank you in advance
 
Last edited by a moderator:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi & welcome to MrExcel.
In what way doesn't your code?
Do you get any messages?
 
Upvote 0
Hi & welcome to MrExcel.
In what way doesn't your code?
Do you get any messages?
No i don't get any messages at all, it just won't "print" any rows if i use the if-statement and print Everything (even if i check the checkboxes or not) if i skip the if-statement.
 
Upvote 0
As that code works for me, a few questions.
1) Is it an ActiveX checkbox?
2) Is it on the sheet called Data?
3) Is the Sub GenereraRB() code in the Data sheet module?
 
Upvote 0
As that code works for me, a few questions.
1) Is it an ActiveX checkbox?
2) Is it on the sheet called Data?
3) Is the Sub GenereraRB() code in the Data sheet module?

Oh okey
1) Is it an ActiveX checkbox?
Yes
2) Is it on the sheet called Data? Yes, i have a sheet called data where i have different data on different rows. Not all data is relevant for the user so it is divided in to different chapters (on the same sheet).Every chapter has an ActiveX Checkbox next to its title where you can hide or unhide the information (data). If the user chooses to use a chapter by activating a checkbox he can then press a separate button Where all the information from all the chosen chapters are being copied to a different sheet (named sheet 2).
3) Is the Sub GenereraRB() code in the Data sheet module? Yes

Hope this clarifies what i'm trying to accomplish
 
Upvote 0
In that case your code works for me, although you can simplify it to
VBA Code:
Sub GenereraRB()

   If cb_as = True Then
      Range("A180:H191").Copy Sheets("sheet 2").Range("A43")
   End If

End Sub
 
Upvote 0
In that case your code works for me, although you can simplify it to
VBA Code:
Sub GenereraRB()

   If cb_as = True Then
      Range("A180:H191").Copy Sheets("sheet 2").Range("A43")
   End If

End Sub
Hmm okey, thanks i'll simplify it.
 
Upvote 0
Does that code work for you, or do you still get the same problem?
 
Upvote 0

Forum statistics

Threads
1,215,674
Messages
6,126,140
Members
449,294
Latest member
Jitesh_Sharma

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top