Help!! Inserting Rows in Excel using VBA

000

Board Regular
Joined
Jun 3, 2011
Messages
59
I am creating a spreadsheet that will end up having atleast 200 rows when completed. I have my spreadsheet broken up into different sections and each row has a cell that will either display Yes or No.

My VBA code is a bunch of If and ElseIf statements that will either display the entire row when the cell reads "Yes" or it will hide the entire row when the cell displays "No".

My concern is if I forgot to add something in a section near the top of the spreadsheet, I would have to insert an entire row. All of the cells called out in each line of VBA code would then be one off.

Is there any way of still being able to look at whether each cell says yes or no and hiding that row accordingly? I just do not want to have to insert a row and then have to go and change all the cell values in each line of code below where I made the insert.

Thanks.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I am creating a spreadsheet that will end up having atleast 200 rows when completed. I have my spreadsheet broken up into different sections and each row has a cell that will either display Yes or No.

My VBA code is a bunch of If and ElseIf statements that will either display the entire row when the cell reads "Yes" or it will hide the entire row when the cell displays "No".

My concern is if I forgot to add something in a section near the top of the spreadsheet, I would have to insert an entire row. All of the cells called out in each line of VBA code would then be one off.

Is there any way of still being able to look at whether each cell says yes or no and hiding that row accordingly? I just do not want to have to insert a row and then have to go and change all the cell values in each line of code below where I made the insert.

Thanks.

Welcome to the forums!

Why not instead just use a filter?
 
Upvote 0
This is my second time using vba. I am not very good at programming. How exactly would I use a filter?
 
Upvote 0
Filtering is a non-vba solution. Select your range of data, then go to Data>Filter>Autofilter. This will give you drop-boxes on all of your headers. Choose the header you want to filter the "No"s out of, and un-tick "No". This will hide all rows with a "No" in that column.
 
Upvote 0
As it is right now I have a command button that I use to update the spreadsheet. I do not have column headers because the cells that display the "yes or no" are not all in the same column so I do not think filtering is going to work.

Is there some way to loop through each row and tell the program to look through each cell in the row to see if there is a NO somewhere and then hide that entire row?
 
Upvote 0
Try this code out. If a "No" is found in a row, it will hide it. If a "No" is not found, it will unhide the row. Note that Column A is used to determine what the last row is, and row 1 is used to determine what the last column is.

Code:
Public Sub HideNo()
Dim i   As Long, _
    j   As Long, _
    LR  As Long, _
    LC  As Long
 
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LR
    Application.StatusBar = "Currently checking row " & i & " of " & LR
    For j = 1 To LC
        If Cells(i, j).Value = "No" Then
            Rows(i).Hidden = True
            Exit For
        End If
        If j = LC Then
            Rows(i).Hidden = False
        End If
    Next j
Next i
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .StatusBar = False
End With
End Sub
 
Upvote 0
It doesnt seem to be working. Some of the cells that have the yes or no start in column b,c,and d. Would this make a difference?
 
Last edited:
Upvote 0
Nevermind....it is working now that I changed the No in the code to the way it is formatted in the spreadsheet. Thank you so much!
 
Upvote 0
It would not.

Do you have data in column A that goes down the the bottom of your table, and data in row 1 that goes out to the right side of your table?

I just tested it on my end and it works fine.
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

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