Pages

Monday, March 25, 2019

How to hide checkboxes and rows in Excel with VBA

I recently received a question about how to hide or collapse a row if a checkbox in that row wasn’t checked. There are a lot of good lessons in this example. When you’re stuck on a problem, the best thing to do is to break it down into smaller problems and solve each one at a time. Let's do it!


How to Insert Checkboxes in Excel


The first lesson is how to add a checkbox to an Excel sheet. Go to Developer tab then Insert. There are two types of checkboxes: Form Control and ActiveX Control. For this example, we are going to use Form Control checkboxes because you can link these directly to a cell.




How to link a checkbox to a cell


The next lesson is how to assign a checkbox to a cell. We’re going to do this to help with hiding the rows later. When you create the first Form Control checkbox in cell A2, in the formula bar type “=$B$2” to assign B2 to the checkbox. Now, if the checkbox is checked B2 should read TRUE, if not selected it should say FALSE.

**The one downside to this method is if you need 100 checkboxes it could take some time to manually assign each checkbox to a cell. There is probably a way to automate this task, however, for simplicity of this example we’re going to say we only need five checkboxes and assign them all manually.**

Now your example sheet should look like this:



How to hide rows based on a cell’s value in VBA

The hiding of all the checkboxes and rows will be done automatically with a VBA macro. If you’re new to macros see this gettingstarted guide. 

Again, break it down into smaller problems. First, let’s figure out how to hide rows based on a condition, the TRUE or FALSE value in our helper column.

Define the first row that contains data we might want to hide:
BeginRow = 2

Define the last row that might contain data we want to hide.
EndRow = 5

Define the column number of our helper info, the column with the true or false values.
ChkCol = 2

Loop through the range of rows we just defined and if the value of the cell in our helper column is false, then hide that entire row:

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = False Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt

How to hide checkboxes automatically with macro

Dim CB As Shape
Dim sh As Worksheet

Set sh = ActiveSheet

We need to loop through all the shapes in the active sheet, see if they are the form control type of Check Box:

For Each CB In sh.Shapes
  If CB.Type = msoFormControl Then
    If CB.FormControlType = xlCheckBox Then
      'MsgBox CB.Name, vbOKOnly
     
      'if check box is checked, keep it visible, otherwise, hide it
      If CB.OLEFormat.Object.Value = 1 Then
        CB.OLEFormat.Object.Visible = True
        Else
        CB.OLEFormat.Object.Visible = False
End If
     
    End If
  End If
Next CB

Full Macro to Hide Rows and Checkboxes

Here’s the final code that hides entire rows based on checkbox is checked or not.

Sub Hide_checkboxes()

Dim CB As Shape
Dim sh As Worksheet

Set sh = ActiveSheet
For Each CB In sh.Shapes
  If CB.Type = msoFormControl Then
    If CB.FormControlType = xlCheckBox Then
      'MsgBox CB.Name, vbOKOnly
     
      'if check box is checked, keep it visible, otherwise, hide it
      If CB.OLEFormat.Object.Value = 1 Then
        CB.OLEFormat.Object.Visible = True
        Else
        CB.OLEFormat.Object.Visible = False
End If
     
    End If
  End If
Next CB

'now hide the rows
    BeginRow = 2
    EndRow = 5
    ChkCol = 2

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = False Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt


End Sub

How to unhide rows and checkboxes in Excel

Conversely, I also made a code to reset everything and unhide all checkboxes and unhide all rows:

Sub Unhide_checkboxes()

Dim CB As Shape
Dim sh As Worksheet

Set sh = ActiveSheet
For Each CB In sh.Shapes
  If CB.Type = msoFormControl Then
    If CB.FormControlType = xlCheckBox Then
      'MsgBox CB.Name, vbOKOnly
     
      'if check box is checked, keep it visible, otherwise, hide it
      If CB.OLEFormat.Object.Value = 1 Then
        CB.OLEFormat.Object.Visible = True
        Else
        CB.OLEFormat.Object.Visible = True
End If
     
    End If
  End If
Next CB

'now hide the rows
    BeginRow = 1
    EndRow = 5
    ChkCol = 2

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = False Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = False
        End If
    Next RowCnt
   
End Sub

How to insert a button and link to a macro

Finally, for ease of use we can add two buttons to our sheet to run each one of the macros. Go to Developer tab, insert command button.



Watch the video below to see how the macro to hide checkboxes and rows works.



As you can see, there is much to glean just from this one example.

7 comments:

  1. Seeing mistake messages can destroy your framework experience.
    Error 3035 Norton can be because of deficient establishment of Norton security programming and you can fix it by downloading and running the reinstall apparatus. Spare the document to windows work area and the record is consequently spared to its default area. In the event that you think that its hard to do, at that point associate with our specialists to help.

    ReplyDelete
  2. There is a safe & effective Natural Herbal Medicine. For Total Cure Call    +2349010754824,  or email him   drrealakhigbe@gmail.com       For an Appointment with (Dr.) AKHIGBE contact him. Treatment with Natural Herbal Cure. For:Dengue Fever, Malaria. Painful or Irregular Menstruation. HIV/Aids. Diabetics. Vaginal Infections. Vaginal Discharge. Itching Of the Private Part. Breast Infection. Discharge from Breast. Breast Pain & Itching. Lower Abdominal Pain. No Periods or Periods Suddenly Stop. Women Sexual Problems. High Blood Pressure Chronic Disease. Pain during Sex inside the Pelvis. Pain during Urination. Pelvic Inflammatory Disease, (PID). Dripping Of Sperm from the Vagina As Well As for Low sperm count. Parkinson disease. Obesity, Lupus.  Cancer.  Tuberculosis.  Zero sperm count. Bacteria, Toxoplasmosis, Diarrhea.Herpatitis A&B, Rabies. Asthma.  Quick Ejaculation. Gallstone, Cystic Fibrosis, Esquizofrenia, Cirrhosis,  Premature Ejaculation. Herpes. Joint Pain. Stroke. Coeliac Disease, Weak Erection.  Erysipelas, Thyroid, Discharge from Penis. HPV.  Hepatitis A and B. STD. Staphylococcus + Gonorrhea + Syphilis. Heart Disease.  Pile-Hemorrhoid.rheumatism, thyroid, Autism, Penis enlargement,  Waist & Back Pain.  Male Infertility and Female Infertility. Etc. Take Action Now. contact him & Order for your Natural Herbal Medicine:  +2349010754824  and email him    drrealakhigbe@gmail.com    Note For an Appointment with (Dr.) AKHIGBE.I suffered in Cancer for a year and three months dieing in pain and full of heart break. One day I was searching through the internet and I came across a testimony herpes cure by doctor Akhigbe. So I contact him to try my luck, we talk and he send me the medicine through courier service and with instructions on how to be drinking it.To my greatest surprise drinking the herbal medicine within three weeks I got the changes and I was cure totally. I don't really know how it happen but there is power in Dr Akhigbe herbal medicine. He is a good herbalist doctor.

    ReplyDelete
  3. Hi Nick,

    hope you are well.

    I found your blog website and really liked your easy to follow approach on chow hide checkboxes and rows using VBA above.
    A friend of mine and I are currently working on a website that aims to make it easier for beginners to browse new topics (we call them islands ;)).

    We collect quality guides for these topics which link to one another in order to enhance the reading flow.

    As you seem savvy with Excel & VBA, I would love to get your feedback & talk about how connecting your guides to guides from the community can make a difference on tiplister.

    Let me know what you think.

    Best regards,

    Tassilo

    Co-founder of https://www.tiplister.com Munich, Germany. Write me hello to tassilo@tiplister.com if you are interested.

    ReplyDelete
  4. God bless Dr Ebacol for his marvelous work in my life, I was diagnosed of HERPES since 2018 and I was taking my medications, I wasn't satisfied i needed to get the HERPES out of my system,I searched out some possible cure for HERPES i saw a comment about Dr Ebacol, how he cured HERPES,DIABETES,HIV,and CANCER with his herbal medicine, I contacted him and he guided me. I asked for solutions, he started the remedy for my health, he sent me the medicine I took the medicine as prescribed by him and 14 days later i was cured from HERPES, Dr Ebacol truly you are great, do you need his help also? Why don’t you contact him through his EMAIL: drebacolherbalhome1@gmail.com call or whatsApp him on +2348159042641

    ReplyDelete
  5. FINALLY FREE FROM HERPES VIRUS. what a miracle i never believe there is cure because my doctor tested me herpes positive and she told me there is no cure, i’m very happy today that i’m having a free life without this herpes virus, i can remember some months ago when i was crying all through the night and day that i can’t get cured from this herpes, i found this herbal man called Dr sunnyna on internet when i was doing research on cure for shingle i contacted him to found out if i can get help from this herpes virus, i was so surprise when he told me that he have the herbs cure to it and he sent me the herbal in less down 5 days i was so happy when i get someone giving me hope that he can cured me i took the herbal for just 2 weeks, when i went for test after taking the herbal i found out that i am cured i was so happy and surprise, i want to use this opportunity to inform you that there is cured to herpes virus you can also contact him for his help as soon as possible so that you can get rid of this herpes once and for all you can reach him through this email: drsunnyna@gmail.com or whatsApp him on +2348077875210 You can also contact him on any disease and virus in this he all have the herbal cure to it. 1 HIV/AIDS 2 HERPES 3 CANCER 4 ALS 5 HEPATITIS 6 DIABETES 7 LOVE SPELL 8 INFECTIONS 9 HIGN BLOOD PRESSURE,..... THANKS TO DR SUNNYNA FOE THE GOOD WORK IN LIFE YOU BLESS.....  

    ReplyDelete
  6. God bless Dr Ebacol for his marvelous work in my life, I was diagnosed of HERPES since 2018 and I was taking my medications, I wasn't satisfied i needed to get the HERPES out of my system,I searched out some possible cure for HERPES i saw a comment about Dr Ebacol, how he cured HERPES,DIABETES,HIV,and CANCER with his herbal medicine, I contacted him and he guided me. I asked for solutions, he started the remedy for my health, he sent me the medicine I took the medicine as prescribed by him and 14 days later i was cured from HERPES, Dr Ebacol truly you are great, do you need his help also? Why don’t you contact him through his EMAIL:    drebacolherbalhome1@gmail.com  call or whatsApp him on +2348159042641 

    ReplyDelete

I'd love to hear from you!
-Nick