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. :)
 
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
[/QUOTE]

I changed the name to Worksheet_Change2, and it stopped giving me the ambiguous error; however, it didn't have any effects. Nothing hid or unhid. :(
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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 <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym> code. Sorry if that's a dumb question. =0/
No! You cannot do that. You can never change the named of Events Procedures. If you do, they will not run automatically.
This is a macro that run automatically when a change is made on your worksheet. Whenever you make a change, Excel will look for VBA with name "Worksheet_Change". It will never look for anything named anything else. So, if you use a different name, the code will not run automatically.

It is reading ambiguous because you cannot have two procedures in the same module with the exact same name. You just need to put all your code in a single procedure. Just copy the body of the code (the IF...THEN code) to the existing Worksheet_Change procedure, underneath the block of your code that you already have in there.
 
Last edited:
Upvote 0
No! You cannot do that. You can never change the named of Events Procedures. If you do, they will not run automatically.
This is a macro that run automatically when a change is made on your worksheet. Whenever you make a change, Excel will look for VBA with name "Worksheet_Change". It will never look for anything named anything else. So, if you use a different name, the code will not run automatically.

It is reading ambiguous because you cannot have two procedures in the same module with the exact same name. You just need to put all your code in a single procedure. Just copy the body of the code (the IF...THEN code) to the existing Worksheet_Change procedure, underneath the block of your code that you already have in there.

Sorry, I must be misunderstanding something.
I'm in Worksheet Change, and I have it pasted right under the "End Sub" of the other one you gave me. The separation line pops up automatically, so I assume that's okay. And when I alt esc it doesn't give me any bologna. But when I change something in the drop down, it errors out and reads compile error: Ambiguous name detected: Worksheet_Change

I am unsure what I'm doing different from what you stated.

Thank you for your patience with me though.
 
Upvote 0
No! You cannot do that. You can never change the named of Events Procedures. If you do, they will not run automatically.
This is a macro that run automatically when a change is made on your worksheet. Whenever you make a change, Excel will look for VBA with name "Worksheet_Change". It will never look for anything named anything else. So, if you use a different name, the code will not run automatically.

It is reading ambiguous because you cannot have two procedures in the same module with the exact same name. You just need to put all your code in a single procedure. Just copy the body of the code (the IF...THEN code) to the existing Worksheet_Change procedure, underneath the block of your code that you already have in there.


OH!
I'm so sorry. I just got it. Lol. And it's working like a charm..

My sincerest apologies. I should have seen that glaring issue.
It's totally working, and I can't thank you enough.
 
Upvote 0
It ALL needs to be in one single procedure, i.e.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   Code block 1

'   Code block 2


End Sub
Here are the important rules you need to remember, in a nutshell

1. You cannot have multiple Procedures or Functions in the same module with the same name.

2. You have no flexibility in naming Event Procedures (which is VBA code that is automatically triggered by some event happening). You must name them exactly in a certain way. You can name regular Procedures and Functions most anything you like (avoiding reserved words, etc), since you decide how and when they are run.

Here is a write-up on Event Procedures: Events In Excel VBA
 
Upvote 0
OH!
I'm so sorry. I just got it. Lol. And it's working like a charm..

My sincerest apologies. I should have seen that glaring issue.
It's totally working, and I can't thank you enough.​
Just saw your last post.
You are welcome.
 
Upvote 0
Just saw your last post.
You are welcome.

And with understanding those two, I just wrote my own for another row and value needed. :)

I'm sure it's the simplest of codes, but still. It's a baby step. Plus now I can stop annoying you!

Your signature is correct: Team a man to fish, feed him for life.
Have a good day!
 
Upvote 0

Forum statistics

Threads
1,216,200
Messages
6,129,475
Members
449,511
Latest member
OttosArmy

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