question about hiding rows/columns and clearing worksheet

shorty123

New Member
Joined
Oct 16, 2012
Messages
6
I have some code which is trying to hide columns/rows based on some logic. After the rows/columns are hidden, the visible rows/columns are copied to Sheet2. Then I am trying to unhide the rows and columns in Sheet1.

1) In the beginning, I am trying to see if cell C3 in Sheet1 has changed. How do I modify that logic to specify that cell C3 needs to change in Sheet 1? What does this logic need to look like if I want to use r1c1 notation? I know that it is better to keep the entire script in one notation (A1 or R1C1).

2) How do I unhide the rows/columns in Sheet1?

3) how do I hide a row (what code needs to replace "MsgBox("hide the row"))?

4) how do I hide a column? (what code needs to replace "MsgBox("hide the column"))?

5) How should the intersect logic be rewritten to be in r1c1 notation? (If Not Intersect(Target, Range("C3:C3")) Is Nothing Then)

6) How do I copy Sheet 1 to Sheet 2 using r1c1 notation? My code is using A1 notation.

Here is my code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i, rowNum, colNum As Integer
Dim name As String
Dim found As Boolean
 ' when the value of cell C3 changes, some processing
 ' needs to be done
 
 ' how do you check to see if the cell which changed
 ' was in Sheet1???
 If Not Intersect(Target, Range("C3:C3")) Is Nothing Then
    colNum = 11
    ' from column 11 to the end of the table, check if
    ' the cell associated with row 5 has a name
    name = Cells(5, colNum).Value
    Do While (name <> "")
       If (name <> Target.Value) Then
          ' how do you hide the entire column in r1c1 notation?
          'Cell.EntireColumn("???").Hidden = True
           MsgBox ("hide the column")
       Else
          ' don't hide the column
          ' Cell.EntireColumn("???").Hidden = False
           MsgBox ("don't hide the column")
        End If
        colNum = colNum + 1
        name = Cells(5, colNum)
    Loop
    
    ' loop through the table starting from row 7
    rowNum = 7
    Do While (Cells(rowNum, 1) <> "")
        found = False
  
        ' loop through the table starting from column 11
        colNum = 11
        name = Cells(5, colNum)
        Do While ((name <> "") And (found <> True))
            ' in columns 11 until the end of the table
            ' check if any cells in the row contain a value
            If (Cells(rowNum, colNum).Value <> "") Then
                found = True
            End If
            name = Cells(5, colNum).Value
            colNum = colNum + 1
        Loop
        If found <> True Then
           ' hide the row for rowNum
           MsgBox ("hide the row")
        Else
           MsgBox ("don't hide the row")
        End If
        rowNum = rowNum + 1
    Loop
 End If
 
 ' copy the Sheet1 to the worksheet called "Sheet2"
 ' the hidden rows and hidden columns should not show up
 Sheets("Sheet1").Select
 Range("A1").Select
 Selection.CurrentRegion.Select
 Application.CutCopyMode = False
 Selection.Copy
 Sheets("output").Select
 
 ' unhide all rows and columns in Sheet1
 
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello, not shure what do you meen, but:
If Not Intersect(Target, Cells(1,3)) Is Nothing Then

If MsgBox("Can I?", vbOKCancel) = vbOK Then
 
Upvote 0
1) Try checking out Workbook_SheetSelectionChange. I couldn't tell exactly what you were looking for here, but this should get you in the right direction.

2) Cells.EntireRow.Hidden = False and Cells.EntireColumn.Hidden = False will unhide all rows and columns

3) You were on the right track! Cells(1, 6).EntireRow.Hidden = True you just need to identify the rows you want to hide. This hides Row 1

4) Same thing here with Cells(2, 4).EntireColumn.Hidden = True identifying the columns, this hides column 4

5) looks like Andrew covered this

6) Select the range to copy with something like this: Range(Cells(1, 1), Cells(100, 100)).Select
 
Upvote 0
Hi and welcome to the forum.

Try this. Place the code in the Sheet1 code module. It will only trigger when you make a change to Sheet1 C3

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Worksheet_Change([COLOR=darkblue]ByVal[/COLOR] Target [COLOR=darkblue]As[/COLOR] Range)
    
    [COLOR=darkblue]Dim[/COLOR] rng [COLOR=darkblue]As[/COLOR] Range, cell [COLOR=darkblue]As[/COLOR] Range
    
    [COLOR=darkblue]If[/COLOR] Target.Address(0, 0) = "C3" [COLOR=darkblue]Then[/COLOR]     [COLOR=green]'Test if cell C3 changed[/COLOR]
        [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] IsEmpty(Target) [COLOR=darkblue]Then[/COLOR]         [COLOR=green]'Test if C3 is not empty[/COLOR]
        
            Cells.EntireColumn.Hidden = [COLOR=darkblue]False[/COLOR]   [COLOR=green]'Unhide all columns[/COLOR]
            Cells.EntireRow.Hidden = [COLOR=darkblue]False[/COLOR]      [COLOR=green]'Unhide all rows[/COLOR]
            
            [COLOR=green]'Define the range of cells to check[/COLOR]
            [COLOR=darkblue]Set[/COLOR] rng = Range("K5", Cells(5, Columns.Count).End(xlToLeft))
            
            [COLOR=green]'test if the columns in row 5 have any match to C3[/COLOR]
            [COLOR=darkblue]If[/COLOR] WorksheetFunction.CountIf(rng, Target) = 0 [COLOR=darkblue]Then[/COLOR]
                MsgBox "No match for " & Target.Value & " in row 5. ", , "No Match"
            [COLOR=darkblue]Else[/COLOR]
                Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
                [COLOR=green]'Hide columns that don't match C3 or hide empty columns[/COLOR]
                [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] cell [COLOR=darkblue]In[/COLOR] rng
                    cell.EntireColumn.Hidden = (cell.Value <> Target.Value Or _
                                                Cells(Rows.Count, cell.Column).End(xlUp).Row <= 5)
                [COLOR=darkblue]Next[/COLOR] cell
                Sheets("Output").UsedRange.Clear [COLOR=green]'Clear old data from Output sheet[/COLOR]
                [COLOR=green]'Copy visible data from Sheet 1 to Output[/COLOR]
                ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Copy _
                    Destination:=Sheets("Output").Range("A1")
                    
                Cells.EntireColumn.Hidden = [COLOR=darkblue]False[/COLOR]
                Cells.EntireRow.Hidden = [COLOR=darkblue]False[/COLOR]
                Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
First of all, I would like to thank everyone on this forum who have been so helpful in making suggestions. I find Excel programming to be tricky.

Thanks for the code. I am studying the suggestions to learn the various tricks.

I added the following code to add logic to hide rows which are empty from column K and on. I was not able to code the logic correctly since it was iterating on the hidden columns (which includes column K to the right end of the data). How do I modify the below so that it only works on the visible columns? See the block of code with this comment:
' For each row from row 7 to the bottom of the table, hide the
' row if columns K to the right end of the table for that row
' are all empty"​

Code:
            Else
                Application.ScreenUpdating = False
                'Hide columns that don't match C3 or hide empty columns
                For Each cell In rng
                    cell.EntireColumn.Hidden = (cell.Value <> Target.Value Or _
                                                Cells(Rows.Count, cell.Column).End(xlUp).Row <= 5)
                Next cell
            
                ' For each row from row 7 to the bottom of the table, hide the
                ' row if columns K to the right end of the table for that row
                ' are all empty
        
                ' find the last column in row 5 which contains data
                lastCol = 1
                Do While (Cells(5, lastCol).Value <> "")
                   lastCol = lastCol + 1
                Loop
                
                curRow = 7
                Do While (Cells(curRow, 1).Value <> "")
                   ' check from column K to the right end of the
                   ' table of the given row to see if the entire
                   ' range is blank.  Col K is equivalent to column 11.
                   curCol = 11
                   emptyRange = False
                   Do While (curCol <= lastCol) And (emptyRange = True)
                      If (Cells(curRow, curCol).Value <> "") Then
                         ' at least one cell in the range for the row is not empty
                         emptyRange = False
                         curCol = curCol + 1
                      End If
                   Loop
                   If (emptyRange = True) Then
                      ' hide the row
                      Rows(curRow).EntireRow.Hidden = True
                   End If
                   curRow = curRow + 1
                Loop
 
                
                
                Sheets("Output").UsedRange.Clear 'Clear old data from Output sheet
                'Copy visible data from Sheet 1 to Output
                ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Copy _
                    Destination:=Sheets("Output").Range("A1")
                    
                Cells.EntireColumn.Hidden = False
                Cells.EntireRow.Hidden = False
                Application.ScreenUpdating = True
            End If
 
Upvote 0
I added the following code to add logic to hide rows which are empty from column K and on.

Sorry. I had missed that part.

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Worksheet_Change([COLOR=darkblue]ByVal[/COLOR] Target [COLOR=darkblue]As[/COLOR] Range)
    
    [COLOR=darkblue]Dim[/COLOR] rng [COLOR=darkblue]As[/COLOR] Range, cell [COLOR=darkblue]As[/COLOR] Range, Lastrow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], r [COLOR=darkblue]As[/COLOR] Long
    
    [COLOR=darkblue]If[/COLOR] Target.Address(0, 0) = "C3" [COLOR=darkblue]Then[/COLOR]     [COLOR=green]'Test if cell C3 changed[/COLOR]
        [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] IsEmpty(Target) [COLOR=darkblue]Then[/COLOR]         [COLOR=green]'Test if C3 is not empty[/COLOR]
        
            Cells.EntireColumn.Hidden = [COLOR=darkblue]False[/COLOR]   [COLOR=green]'Unhide all columns[/COLOR]
            Cells.EntireRow.Hidden = [COLOR=darkblue]False[/COLOR]      [COLOR=green]'Unhide all rows[/COLOR]
            
            [COLOR=green]'Define the range of cells to check[/COLOR]
            [COLOR=darkblue]Set[/COLOR] rng = Range("K5", Cells(5, Columns.Count).End(xlToLeft))
            
            [COLOR=green]'test if the columns in row 5 have any match to C3[/COLOR]
            [COLOR=darkblue]If[/COLOR] WorksheetFunction.CountIf(rng, Target) = 0 [COLOR=darkblue]Then[/COLOR]
                MsgBox "No match for " & Target.Value & " in row 5. ", , "No Match"
            [COLOR=darkblue]Else[/COLOR]
                Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
                [COLOR=green]'Hide columns that don't match C3 or hide empty columns[/COLOR]
                [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] cell [COLOR=darkblue]In[/COLOR] rng
                    cell.EntireColumn.Hidden = LCase(cell.Value) <> LCase(Target.Value) Or _
                                               Cells(Rows.Count, cell.Column).End(xlUp).Row <= 5
                [COLOR=darkblue]Next[/COLOR] cell
                
                [COLOR=green]'Hide the empty rows of the remaining visible columns[/COLOR]
                [COLOR=darkblue]Set[/COLOR] rng = rng.SpecialCells(xlCellTypeVisible) [COLOR=green]' Only visible headers in row 5[/COLOR]
                Lastrow = Cells.Find(What:="*", SearchOrder:=xlByRows, Searchdirection:=xlPrevious).Row
                [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
                [COLOR=darkblue]For[/COLOR] r = 6 [COLOR=darkblue]To[/COLOR] Lastrow
                    Rows(r).Hidden = rng.Count = rng.Offset(r - 5).SpecialCells(xlCellTypeBlanks).Count
                [COLOR=darkblue]Next[/COLOR] r
                [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
                
                [COLOR=green]'Copy visible data from Sheet1 to Output[/COLOR]
                Sheets("Output").UsedRange.Clear [COLOR=green]'Clear old data from Output sheet[/COLOR]
                ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Copy _
                    Destination:=Sheets("Output").Range("A1")
                    
                Cells.EntireColumn.Hidden = [COLOR=darkblue]False[/COLOR]
                Cells.EntireRow.Hidden = [COLOR=darkblue]False[/COLOR]
                Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
How do I need to modify the below line if the range should start in the visible column after K? If the row has content in rows A-K and no content in the visible columns after K, the row is deleted. I think that the below logic is setting the range to include columns A-K in the visible columns.

Code:
   Rows(r).Hidden = rng.Count = rng.Offset(r - 5).SpecialCells(xlCellTypeBlanks).Count

On another note, if I am looking for one good reference for coding visual basic for Excel, what would you recommend? For instance, there are many ways to code the logic for finding the last row (some of which are not robust solutions). I need to learn good techniques for manipulating data in Excel (via Visual Basic).
 
Last edited:
Upvote 0
Let me ask a different question.

After the following code:
Code:
       'Hide the empty rows of the remaining visible columns
                Set rng = rng.SpecialCells(xlCellTypeVisible) ' Only visible headers in row 5

I added
Code:
   MsgBox (rng.Count)
            rng.Offset(0, 0).Select
            MsgBox (rng.Offset(0, 0).SpecialCells(xlCellTypeBlanks).Count)

Rng.Count is returning 1 since there is only 1 visible column. After the select statement executes, I see one cell highlighted. The MsgBox dialog is returning the value "334". If the number of cells in the range is only 1, why would the count of blank cells in the range be 334?
 
Upvote 0
Let me ask a different question.

After the following code:
Code:
       'Hide the empty rows of the remaining visible columns
                Set rng = rng.SpecialCells(xlCellTypeVisible) ' Only visible headers in row 5

I added
Code:
   MsgBox (rng.Count)
            rng.Offset(0, 0).Select
            MsgBox (rng.Offset(0, 0).SpecialCells(xlCellTypeBlanks).Count)

Rng.Count is returning 1 since there is only 1 visible column. After the select statement executes, I see one cell highlighted. The MsgBox dialog is returning the value "334". If the number of cells in the range is only 1, why would the count of blank cells in the range be 334?

--> After doing some research, I see that trying to apply SpecialCells on a range of 1 cell yields bad results because it causes Excel to work with the entire worksheet instead of the range. The script is failing for the case where there is only 1 visible column (in columns K onward).
 
Upvote 0
Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] Worksheet_Change([color=darkblue]ByVal[/color] Target [color=darkblue]As[/color] Range)
    
    [color=darkblue]Dim[/color] rng [color=darkblue]As[/color] Range, cell [color=darkblue]As[/color] Range, Lastrow [color=darkblue]As[/color] [color=darkblue]Long[/color], r [color=darkblue]As[/color] Long
    
    [color=darkblue]If[/color] Target.Address(0, 0) = "C3" [color=darkblue]Then[/color]     [color=green]'Test if cell C3 changed[/color]
        [color=darkblue]If[/color] [color=darkblue]Not[/color] IsEmpty(Target) [color=darkblue]Then[/color]         [color=green]'Test if C3 is not empty[/color]
        
            Cells.EntireColumn.Hidden = [color=darkblue]False[/color]   [color=green]'Unhide all columns[/color]
            Cells.EntireRow.Hidden = [color=darkblue]False[/color]      [color=green]'Unhide all rows[/color]
            
            [color=green]'Define the range of cells to check[/color]
            [color=darkblue]Set[/color] rng = Range("K5", Cells(5, Columns.Count).End(xlToLeft))
            
            [color=green]'test if the columns in row 5 have any match to C3[/color]
            [color=darkblue]If[/color] WorksheetFunction.CountIf(rng, Target) = 0 [color=darkblue]Then[/color]
                MsgBox "No match for " & Target.Value & " in row 5. ", , "No Match"
            [color=darkblue]Else[/color]
                Application.ScreenUpdating = [color=darkblue]False[/color]
                [color=green]'Hide columns that don't match C3 or hide empty columns[/color]
                [color=darkblue]For[/color] [color=darkblue]Each[/color] cell [color=darkblue]In[/color] rng
                    cell.EntireColumn.Hidden = LCase(cell.Value) <> LCase(Target.Value) Or _
                                               Cells(Rows.Count, cell.Column).End(xlUp).Row <= 5
                [color=darkblue]Next[/color] cell
                
                [color=green]'Hide the empty rows of the remaining visible columns[/color]
                [color=darkblue]Set[/color] rng = rng.SpecialCells(xlCellTypeVisible) [color=green]' Only visible headers in row 5[/color]
                Lastrow = Cells.Find(What:="*", SearchOrder:=xlByRows, Searchdirection:=xlPrevious).Row
                [color=darkblue]If[/color] rng.Count = 1 [color=darkblue]Then[/color]
                    [color=green]'Check the one visible column for blanks[/color]
                   rng.Resize(Lastrow).AutoFilter Field:=1, Criteria1:="<>"  [color=green]'Filter for non-blanks in one column[/color]
                [color=darkblue]Else[/color]
                    [color=green]'Check multiple visible columns for blanks[/color]
                    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
                    [color=darkblue]For[/color] r = 6 [color=darkblue]To[/color] Lastrow
                        Rows(r).Hidden = rng.Count = rng.Offset(r - 5).SpecialCells(xlCellTypeBlanks).Count
                    [color=darkblue]Next[/color] r
                    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] 0
                [color=darkblue]End[/color] [color=darkblue]If[/color]
                
                [color=green]'Copy visible data from Sheet1 to Output[/color]
                Sheets("Output").UsedRange.Clear [color=green]'Clear old data from Output sheet[/color]
                ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Copy _
                    Destination:=Sheets("Output").Range("A1")
                    
                ActiveSheet.AutoFilterMode = [color=darkblue]False[/color]
                Cells.EntireColumn.Hidden = [color=darkblue]False[/color]
                Cells.EntireRow.Hidden = [color=darkblue]False[/color]
                Application.ScreenUpdating = [color=darkblue]True[/color]
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
End [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,050
Members
452,542
Latest member
Bricklin

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