Hide Rows with VBA

niravptl

New Member
Joined
Mar 12, 2018
Messages
10
Hello,

So we have wrote a code that does so many things. But there is a last step we would like it to do. The step is that the data that is being pasted in nextRow=200 and down needs to be hidden. So basically after everything is pasted in the new sheets I would like cells 200 down to probably 600 to be hidden in the new sheets being created.

This is my current code I have

Code:
Sub CopyandPaste()
Application.ScreenUpdating = False
Sheets("Template").Visible = True
Application.ScreenUpdating = False
Sheets("MasterSheet").Visible = True
Dim lastRow As Long
Dim thisRow As Long
Dim nextRow As Long
Dim sheetCount As Long
Dim selectedCells
Dim newSheet As Worksheet
Dim pt As PivotTable
On Error Resume Next
Application.ScreenUpdating = False
lastRow = Sheets("MasterSheet").Cells(Sheets("MasterSheet").Rows.Count, "A").End(xlUp).Row
selectedCells = Application.Selection.Value
Set pt = ActiveSheet.PivotTables("PivotTable1")
    pt.RefreshTable
        
For sheetCount = 1 To UBound(selectedCells, 1)
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    Set newSheet = Sheets(Sheets.Count)
    newSheet.Name = selectedCells(sheetCount, 1)
    nextRow = 200
    For thisRow = 2 To lastRow
        If Sheets("MasterSheet").Cells(thisRow, "A").Value = selectedCells(sheetCount, 1) Then
            Sheets("MasterSheet").Cells(thisRow, "A").EntireRow.Copy Destination:=newSheet.Cells(nextRow, "A")
            nextRow = nextRow + 1
        End If
    Next thisRow
Next sheetCount
Sheets("MasterSheet").Activate
Range("A1").Select
Sheets("MasterSheet").Visible = False
Application.ScreenUpdating = True
Sheets("Template").Visible = False
Application.ScreenUpdating = True
End Sub



I would like to say thank you in advance!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
newSheet.Rows("200:600").Hidden = True

put that after the loop

if it might be something else than 600 then calculate it and save it to a variable...

newSheet.Rows("200:" & lastRow).Hidden = True
 
Last edited:
Upvote 0
Hey thanks for helping me but I don't know if I put it at the wrong place but it isn't working. @cerfani


This is what I have...


Code:
Sub CopyandPaste()
Application.ScreenUpdating = False
Sheets("Template").Visible = True
Application.ScreenUpdating = False
Sheets("MasterSheet").Visible = True
Dim lastRow As Long
Dim thisRow As Long
Dim nextRow As Long
Dim alsoRow As Long
Dim sheetCount As Long
Dim selectedCells
Dim newSheet As Worksheet
Dim pt As PivotTable
On Error Resume Next
Application.ScreenUpdating = False
lastRow = Sheets("MasterSheet").Cells(Sheets("MasterSheet").Rows.Count, "A").End(xlUp).Row
selectedCells = Application.Selection.Value
Set pt = ActiveSheet.PivotTables("PivotTable1")
    pt.RefreshTable
        
For sheetCount = 1 To UBound(selectedCells, 1)
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    Set newSheet = Sheets(Sheets.Count)
    newSheet.Name = selectedCells(sheetCount, 1)
    nextRow = 200
    For thisRow = 2 To lastRow
        If Sheets("MasterSheet").Cells(thisRow, "A").Value = selectedCells(sheetCount, 1) Then
            Sheets("MasterSheet").Cells(thisRow, "A").EntireRow.Copy Destination:=newSheet.Cells(nextRow, "A")
            nextRow = nextRow + 1
        End If
    Next thisRow
Next sheetCount
newSheet.Rows("200:600").Hidden = True
Sheets("MasterSheet").Activate
Range("A1").Select
Sheets("MasterSheet").Visible = False
Application.ScreenUpdating = True
Sheets("Template").Visible = False
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Without determining the bottom row, you could assume row 600 as last row.
Insert before "Next sheetCount"
Code:
newSheet.Rows("200:600").EntireRow.Hidden = True
 
Upvote 0
I actually meant for you top place after the nested loop. The first loop is looping sheets and you need to hide rows for each sheet so it needed to be inside the sheet loop and after the row loop... but anyways you got it done regardless. Good job.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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