I am trying to insert a function in my VBA code to only fill a cell when another cell has data in it

-2

This is my code. It fills the Date when I have this =Updating_Date(B5) typed in C5. What it is supposed to do is fill the Date ONLY when data is present in B5, and remove the Date when No data is present. Currently it displays Date no matter what. I need to write in a condition to make it ONLY display Date when data is present in Column B.

Below is what I have typed into a module, thanks to another user, with =Updating_Date(B5) entered in C5.

Function Updating_Date(dependent_cell as Range) As Date Updating_Date = Date End Function

Again, this displays the Date no matter if data is in column B or not.

Thanks.

Kyle Hinkebein

Posted 2015-03-24T19:56:08.807

Reputation: 33

Question was closed 2015-03-25T13:33:38.250

Have you tried googling for "vba condition"? Because this site is not a free macro writing service. – TheUser1024 – 2015-03-24T20:06:14.467

If my question was inappropriate I apologize and will delete it. I was looking for assistance or suggestions to what I currently had. I didn't realize asking for help writing that condition was breaking any rules.. – Kyle Hinkebein – 2015-03-24T20:23:35.990

This site is fully about helping you write that condition, but you should try to get a bit further maybe. If you try and run into an issue, this is the place to go and ask for help. But it is up to you to do the trying. ;-) However you already got an answer, but i think your question does not show enough effort. No worries though, no offense meant! You would have learnt and benefited more trying a bit harder i believe (and you already had the search word you needed: "condition"). – TheUser1024 – 2015-03-24T20:25:15.850

I appreciate the advice!! I'm honestly in a bit of crunch to get this working otherwise I probably would had even a little more. Generally I would have more time to experiment and get a better grasp as I would prefer to learn rather than ask for handouts.. When your not used to formulas or coding it's really tricky trying to jump into it.. – Kyle Hinkebein – 2015-03-24T20:39:44.727

Answers

1

I may not understand your example clearly, but it looks like you could just use one of these cell formulas:

=IF(ISBLANK(B5),"",TODAY())
=IF(ISBLANK(B5),"",NOW())
=IF(ISBLANK(B5),"",Updating_Date(B5))

If you want to do the check in VBA, here's an option:

Function Updating_Date(dependent_cell As Range)
    If LenB(dependent_cell.Value) > 0 Then Updating_Date = Date Else Updating_Date = ""
End Function

Not that the function can not return a date or else it'll return 0 instead of blank. I dropped that and added an If condition that returns blank if the dependent_cell is blank.

Engineer Toast

Posted 2015-03-24T19:56:08.807

Reputation: 3 019

This seemed to work perfectly, just altering the formula to =IF(ISBLANK(B5),"",Updating_Date(B5)). Thank you very much. I am 100% new to using formulas and especially vba.. Thanks again.. – Kyle Hinkebein – 2015-03-24T20:24:48.517