Hide cells based on value

Farley945

New Member
Joined
Feb 12, 2021
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
Hi All,

I have looked on similar post but couldn't find the answer to my query

on the below code, instead of deleting the entire row, I want the row to hide. Can anyone help with this?

Thank you in advance

VBA Code:
Sub commandbutton1_click()
Dim x As Integer
Dim y As Integer
Dim i As Integer
Dim shSource As Worksheet
Dim shTarget1 As Worksheet
Dim shTarget2 As Worksheet


Set shSource = ThisWorkbook.Sheets("Phase Bad Debt Schedule Compan")
Set shTarget1 = ThisWorkbook.Sheets("Paid")


If shTarget1.Cells(7, 32).Value = "" Then
x = 5
Else
x = shTarget1.Cells(7, 32).CurrentRegion.Rows.Count + 5
End If



i = 7


Do Until shSource.Cells(i, 32) = ""
    If shSource.Cells(i, 32).Value = "Paid" Then
    shSource.Rows(i).Copy
    shTarget1.Cells(i, 1).PasteSpecial Paste:=xlPasteValues
    shSource.Rows(i).Delete
    x = x + 1
    GoTo Line1
    
    End If
i = i + 1


Line1: Loop


End Sub
 

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.
Hi & welcome to MrExcel.
Change
VBA Code:
shSource.Rows(i).Delete
to
VBA Code:
shSource.Rows(i).Hidden=True
 
Upvote 0
Solution
Hi & welcome to MrExcel.
Change
VBA Code:
shSource.Rows(i).Delete
to
VBA Code:
shSource.Rows(i).Hidden=True
Hi Fluff,

I tried that before but it didn't work.

The screen just flashes, after 2 minutes only the first row was hidden
 
Upvote 0
Also instead of copy and paste, I would ideally need to cut and paste if possible please
 
Upvote 0
With the change I suggested also get rid of these two lines
VBA Code:
    x = x + 1
    GoTo Line1
 
Upvote 0
With the change I suggested also get rid of these two lines
VBA Code:
    x = x + 1
    GoTo Line1

Thank you, that perfectly, I don't suppose you saw my other message regarding; changing the copy and paste to cut and paste?
 
Upvote 0
How about
VBA Code:
Do Until shSource.Cells(i, 2) = ""
    If shSource.Cells(i, 2).Value = "No" Then
    shSource.Rows(i).Copy
    shTarget1.Cells(i, 1).PasteSpecial xlPasteValues
    shSource.Rows(i).Clear
    shSource.Rows(i).Hidden = True
    End If
 
Upvote 0
Hi Fluff,

That works perfectly, thank you.

My only issue is now, the button only works once, whereas I want it to work every time it's pressed as the figures I use are updated every other day.

See code below:

VBA Code:
Sub commandbutton1_click()
Dim x As Integer
Dim y As Integer
Dim i As Integer
Dim shSource As Worksheet
Dim shTarget1 As Worksheet
Dim shTarget2 As Worksheet


Set shSource = ThisWorkbook.Sheets("Phase Bad Debt Schedule Compan")
Set shTarget1 = ThisWorkbook.Sheets("Paid")


If shTarget1.Cells(7, 32).Value = "" Then
x = 5
Else
x = shTarget1.Cells(7, 32).CurrentRegion.Rows.Count + 5
End If



i = 7


Do Until shSource.Cells(i, 32) = ""
    If shSource.Cells(i, 32).Value = "Paid" Then
    shSource.Rows(i).Copy
    shTarget1.Cells(i, 1).PasteSpecial Paste:=xlPasteValues
    shSource.Rows(i).Clear
    shSource.Rows(i).Hidden = True
       
    End If
i = i + 1


Line1: Loop


End Sub
 
Upvote 0
What do you mean it only works once?
 
Upvote 0
After I have clicked the button it works but when I up date my data, it won't work after that
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,825
Members
449,190
Latest member
rscraig11

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