VBA code to automatically hide blank rows but need to unhide when needed to enter details.

DeepuSultan

New Member
Joined
Feb 18, 2021
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi Team,

I am building an excel template where there are 250 blank rows (as of now) and I need help in automatically hiding the blank rows so that my team working on it can see the last row where the totals are. I don't want everytime to scroll to the last row for the totals. For example if they need to fill only the first 10 rows then want the remaining blank rows to get automatically hidden and just show up the last row of totals after feeling up the first 10 rows. The point to note here is that I have formulas in some of the columns to calculate discounts and sums, what I meant to say that the whole rows will not be blank. Some of the cells in the rows will have formulas.

Also I want if H1>Sheet1!E3 then hide the following columns l, o, r, u and if H1>Sheet1!E4 then hide the following columns j, m, p, s.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I you put this code in the worksheet activate adn worksheet change evetns it should hopefuylly do what you want:
VBA Code:
Private Sub Worksheet_Activate()
Range(Cells(1, 1), Cells(100000, 1)).EntireRow.Hidden = False
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range("A1:A" & lastrow)
For i = 1 To lastrow
 If inarr(i, 1) = "" Then Exit For
Next i
Range(Cells(i + 1, 1), Cells(lastrow - 1, 1)).EntireRow.Hidden = True
    

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Range(Cells(1, 1), Cells(100000, 1)).EntireRow.Hidden = False
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range("A1:A" & lastrow)
For i = 1 To lastrow
 If inarr(i, 1) = "" Then Exit For
Next i
Range(Cells(i + 1, 1), Cells(lastrow - 1, 1)).EntireRow.Hidden = True
    

End Sub
 
Upvote 0
T
I you put this code in the worksheet activate adn worksheet change evetns it should hopefuylly do what you want:
VBA Code:
Private Sub Worksheet_Activate()
Range(Cells(1, 1), Cells(100000, 1)).EntireRow.Hidden = False
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range("A1:A" & lastrow)
For i = 1 To lastrow
 If inarr(i, 1) = "" Then Exit For
Next i
Range(Cells(i + 1, 1), Cells(lastrow - 1, 1)).EntireRow.Hidden = True
   

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Range(Cells(1, 1), Cells(100000, 1)).EntireRow.Hidden = False
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range("A1:A" & lastrow)
For i = 1 To lastrow
 If inarr(i, 1) = "" Then Exit For
Next i
Range(Cells(i + 1, 1), Cells(lastrow - 1, 1)).EntireRow.Hidden = True
   

End Sub
This is not working. I don't know why
 
Upvote 0
You didn't give any specific details about your worksheet so I made a number of assumptions ( which is also why i didn't answer the second question)
What I did was on sheet 1 of a blank workbook : I put some text in the cells A1 to A10, I then put text in A250 ( the last row)
I then put the two bits of code in the code for that worksheet
I then selected sheet 2 and then back to sheet 1: rows 1 to 11 appear and then all rows are hidden until row 250.
I then typed into A11 and when I hit enter row 12 appears
I sugggest you do exactly the same on a blank workbook and see if that works.
 
Upvote 0
You didn't give any specific details about your worksheet so I made a number of assumptions ( which is also why i didn't answer the second question)
What I did was on sheet 1 of a blank workbook : I put some text in the cells A1 to A10, I then put text in A250 ( the last row)
I then put the two bits of code in the code for that worksheet
I then selected sheet 2 and then back to sheet 1: rows 1 to 11 appear and then all rows are hidden until row 250.
I then typed into A11 and when I hit enter row 12 appears
I sugggest you do exactly the same on a blank workbook and see if that works.
This is the template,

Pricing and Margin file template-1.xlsm
ABCDEFGHIJKLMNOPQRS
1Average Days755.851095
2LicenseOriginal SKUQuantityExpiry DateCo-term DateCo-Term DaysWeighted Avg DaysLP 1YrLP 3YrLP 5YrPrice with 1Y LPPrice with 3Y LPPrice with 5Y LPPer unit cost for 1YPer unit cost for 3YPer unit cost for 5Y Margin Cost for 1Y LP Margin Cost for 3Y LP Margin Cost for 5Y LP
3ABCDFgbGHJJKABC123307/10/20243/31/20279952985012332466369933611.9267223.84100835.7591.70212.94258.987499.305804.804235.92
4DEFGjglKLLJHGFDFDEF123510/26/20243/31/202788744351233246636994993.939987.8614981.7991.70212.94258.981114.22862.46629.36
5GHIGHGJJJUIKKdGHI123112/16/20273/31/202744484109521913287484.00968.441452.8891.70212.93258.97121.6094.1268.68
6        
7       
8       
9        
10       
11       
12        
13       
14       
15        
16       
17       
18        
19       
20       
21        
22       
23       
24        
25       
26       
27        
28       
29       
30        
31       
32       
33        
34       
35       
36        
37       
38       
39        
40       
41       
42        
43       
44       
45        
46       
47       
48        
49       
50       
514634769Total Value78,180.14
52Discount66.75%Customer Price25,994.90
53Margin$25,994.90
54Margin%100.00%
Pricing+Margin Template
Cell Formulas
RangeFormula
F1F1=ROUND(G51/C51,2)
G1G1=IF(AND(F1>Sheet1!E4,F1<Sheet1!D4),1825,IF(AND(F1>Sheet1!D3,F1<Sheet1!E4),1095,IF(AND(F1>Sheet1!E3,F1<Sheet1!D3),1095,365)))
Q3:Q50Q3=IF(OR(C3="",N3=""),"",(C3*N3*$F3/365))
R3:R50R3=IF(OR(C3="",O3=""),"",(C3*O3*$F3/1095))
S3:S50S3=IF(OR(C3="",P3=""),"",(C3*P3*$F3/1825))
M3:M6M3=IF(OR(C3="",I3=""),"",(C3*J3*$F3/$G$1))
M9,M48,M45,M42,M39,M36,M33,M30,M27,M24,M21,M18,M15,M12M9=IF(OR(C9="",I9=""),"",(C9*I9*$F9/$G$1))
F3:F50F3=IF(OR(D3="",E3=""),"",E3-D3+1)
K3:K50K3=IF(OR(C3="",H3=""),"",(C3*H3*$F3/$G$1))
C51,L51,G51C51=SUM(C3:C50)
G3:G50G3=IF(OR(C3="",F3=""),"",C3*F3)
L3:L50L3=IF(OR(C3="",I3=""),"",(C3*I3*$F3/$G$1))
L52L52=L51*(1-G52)
L53L53=L52-R51
L54L54=L53/L52
 
Upvote 0
I could not copy 250 rows for limitations with Xl2bb, hence have includedonly 48 rows. But in actual my file would have 250 rows to fill i.e from row# 3-252. There are two headers Row #1 and 2 and four rows #253-256 which have totals at the bottom. There is also a reference from another sheet named "Sheet1" from the same workbook in G1.

What I need is-

1. when I fill up Row#3 (or any no of rows) and go to another sheet (come out of that working sheet), the remaining blank rows should get hidden automatically, leaving only blank row open (prbably the next blank row) so that I could start entering details again. This would keep repeating untill row# 252 is filled up.

2. what I also need is that if the value in F1>Sheet1!E3 then hide the following columns L, O, R, U but keep other columns visible. Again if the value in F1>Sheet1!E4 then hide the following columns J, M, P, S and keep other columns visible.

Could you help me please
 
Upvote 0
Here is the solution to question 1 which is just a couple of modificatiosn to my code to take account of your worksheet:
VBA Code:
Private Sub Worksheet_Activate()
Range(Cells(1, 1), Cells(100000, 1)).EntireRow.Hidden = False
lastrow = Cells(Rows.Count, "I").End(xlUp).Row   ' changed to I
inarr = Range("A1:A" & lastrow)
For i = 2 To lastrow - 4                        ' change to 2 to lastrow -4
 If inarr(i, 1) = "" Then Exit For
Next i
Range(Cells(i + 1, 1), Cells(lastrow - 4, 1)).EntireRow.Hidden = True  ' changed to lastrow -4
    

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Range(Cells(1, 1), Cells(100000, 1)).EntireRow.Hidden = False
lastrow = Cells(Rows.Count, "I").End(xlUp).Row
inarr = Range("A1:A" & lastrow)
For i = 2 To lastrow - 4
 If inarr(i, 1) = "" Then Exit For
Next i
Range(Cells(i + 1, 1), Cells(lastrow - 4, 1)).EntireRow.Hidden = True
    

End Sub
 
Upvote 0
2. what I also need is that if the value in F1>Sheet1!E3 then hide the following columns L, O, R, U but keep other columns visible. Again if the value in F1>Sheet1!E4 then hide the following columns J, M, P, S and keep other columns visible.
What happens if the value if F1 is greater that Sheet1!E3 and greater than Sheet1!E4??
also does that value in F1 change because of a data entry by the user or will it change due to a calculation, i.e I am asking what is going to trigger showing and hiding the columns
 
Upvote 0

Forum statistics

Threads
1,216,126
Messages
6,129,004
Members
449,480
Latest member
yesitisasport

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