Code not working

ExcelNovice17

New Member
Joined
Apr 11, 2019
Messages
7
Hi,

I am new to VBA coding and i am still in the learning process. Can anybody help me why the codes in red below are not working. I have different sheets in the excel and i have to hide 2 specific row in one sheet when an option is being selected:

Sub bar()
Dim sht
Dim str As String
sht = "NEW"
str = "-"
If sht.Range("I19").Value = str Then
sht.Rows("35:36").Hidden = True
Else: sht.Rows("35:36").Hidden = False
End If
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Because to use it like sht.Range vba expects sht to be a sheet not a string.

Code:
Dim sht As Worksheet
Set sht = Sheets("NEW")
 
Upvote 0
Try
Code:
Sub bar()
   Dim sht
   Dim str As String
   sht = "NEW"
   str = "-"
   With Sheets(sht)
      If .Range("I19").Value = str Then
         .Rows("35:36").Hidden = True
      Else
         .Rows("35:36").Hidden = False
      End If
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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