New to VBA - Simple code needed

Grammarjunkie

Board Regular
Joined
Mar 22, 2016
Messages
86
I didn't really have a need for VBA until now, but I do have a lot of experience writing formulas in Excel. If someone could help write a VBA code for me to simply copy and paste, I'd really appreciate it.

Essentially, I have a date in D5 and a date in E11. If the dates match, I need row 25 to be hidden. (Or I can hide it and it can just stay hidden.)
If they DON'T match, I need row 25 to unhide automatically.

And that's it!

I presume that I can write whatever formulas I want in the cells in row 25 and they will be there throughout the automatic hiding and unhiding.
I also presume this code would be pasted in General_Worksheet.

I understand that it might be obnoxious to want someone to just write the code for me, but I've been trying to do it myself for a good hour, and don't seem to be close. So I'd really appreciate it.

Thanks in advance. :)
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Welcome to the Board!

How are the dates in cells D5 and E11 entered and changed?
Is it done manually, or are they the result of some formulas?
 
Upvote 0
If they are entered in manually, you should be able to use a Worksheet_Change event procedure.
Just go to the sheet you want to apply this to, right-click on the sheet tab name at the bottom of the sheet, select "View Code", and paste this code in the resulting VBA Editor window.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim myRange As Range
    
'   Set range to check
    Set myRange = Range("D5,E11")
    
'   See if value in range above was updated
    If Not Intersect(Target, myRange) Is Nothing Then
        If Range("D5") <> "" Then
            Rows("25").Hidden = (Range("D5") = Range("E11"))
        End If
    End If
              
End Sub
 
Upvote 0
If they are entered in manually, you should be able to use a Worksheet_Change event procedure.
Just go to the sheet you want to apply this to, right-click on the sheet tab name at the bottom of the sheet, select "View Code", and paste this code in the resulting VBA Editor window.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim myRange As Range
    
'   Set range to check
    Set myRange = Range("D5,E11")
    
'   See if value in range above was updated
    If Not Intersect(Target, myRange) Is Nothing Then
        If Range("D5") <> "" Then
            Rows("25").Hidden = (Range("D5") = Range("E11"))
        End If
    End If
              
End Sub


You gave me EXACTLY what I asked for, and I'm so appreciative!
But if I may nitpick and ask for a tiny, aesthetic change... Is there much more to the code if I want it to stay hidden until both dates are filled? (They are filled in manually, btw.)
As of right now, it unhides the row with one date filled in. I'd PREFER if it stayed hidden (since in general these two dates are always going to be the same).

I hope that made sense, and I hope that isn't too much trouble. :)
 
Upvote 0
We can simplify it to the following. What this will essentially do is ONLY unhide row 25 when both D5 and E11 are both populated, and they are equal to each other:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If (Range("D5") <> "") And (Range("E11") <> "") And (Range("D5") = Range("E11")) Then
        Rows("25").Hidden = False
    Else
        Rows("25").Hidden = True
    End If
    
End Sub
 
Upvote 0
We can simplify it to the following. What this will essentially do is ONLY unhide row 25 when both D5 and E11 are both populated, and they are equal to each other:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If (Range("D5") <> "") And (Range("E11") <> "") And (Range("D5") = Range("E11")) Then
        Rows("25").Hidden = False
    Else
        Rows("25").Hidden = True
    End If
    
End Sub


Haha, you actually switched them! I wanted it hidden when equal, and revealed when they're different. But that's okay because I read it over and over until my brain comprehended what was going on, and I fixed it myself. So you forced me to learn something!
Probably purposefully ;)

Thank you so much for your help, Joe. You're the man.
 
Upvote 0
Haha, you actually switched them! I wanted it hidden when equal, and revealed when they're different. But that's okay because I read it over and over until my brain comprehended what was going on, and I fixed it myself. So you forced me to learn something!
Probably purposefully
Yeah, that's it, I would just testing you.;)
Good job!
 
Upvote 0
Yeah, that's it, I would just testing you.;)
Good job!

Would you mind teaching me one more thing? I feel like with these two things combined, I would be able to figure out other things.

But say I wanted to hide Rows 31 and 32 if F30 has "N" or nothing selected from the drop down, and unhide them if "Y" is selected from the dropdown.
What would that look like?
I tried playing with the code you gave me, but I doubt it's as simple as my mind tried to do. Haha.
Instead of the greater than, less than signs, I just smacked an equal in there with "Y" in quotes after. But that isn't working. :)
 
Upvote 0
Assuming that F30 can only ever be "Y","N", or nothing, try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Range("F30") = "Y" Then
        Rows("31:32").Hidden = False
    Else
        Rows("31:32").Hidden = True
    End If
    
End Sub
 
Upvote 0
Assuming that F30 can only ever be "Y","N", or nothing, try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Range("F30") = "Y" Then
        Rows("31:32").Hidden = False
    Else
        Rows("31:32").Hidden = True
    End If
    
End Sub


Should I change the name to Worksheet_Change2 or something? It reads that it's ambiguous, I think because the name is the same as my first VBA code. Sorry if that's a dumb question. =0/
 
Upvote 0

Forum statistics

Threads
1,215,248
Messages
6,123,866
Members
449,129
Latest member
krishnamadison

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