2 very easy excel vba questions...

Usually I figure out vba stuff with help, but since it's missing and i don't have admin privelages, here goes:

Want to make a little 'macro' hooked on the change sheet procedure to change the color of the cell text (of a bunch of cells from A1 to Z40) if the value is #N/A... (which would probably entail changing it back to it's original color, too...but I can figure that out from an answer to the above)

While I'm at it, might as well ask about a macro for a time date stamp. Something along the lines of changing a cell to the current time and date which I'd hook to the beforesave action.

crimson30Asked:
Who is Participating?
 
ggilmanConnect With a Mentor Commented:
BTW, you need to modify the if statement a little too:

If ActiveSheet.Name = "Somesheet" Or ActiveSheet.Name = "Another Sheet" Then

        ...Do Stuff...

End If
0
 
ggilmanCommented:
   Range("A1:Z40").Select
    With Selection.Interior
        .ColorIndex = 46    'Your color here
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
    End With
0
 
ggilmanCommented:
And for the time/date

    Cells(1, 1) = "=Now()"
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
ggilmanCommented:
Really the easiest way to figure out things like that is just to do it: Goto Tools->Macro->Record new macro. Now, manually perform the action (change color, font, etc...). Go back to Tools->Macro->StopRecording. Now, look at the code it generated by going to Tools->Macro->Visual Basic Editor. It will record each step of the macro you performed into a module (by default Module1). Just copy the code from there, place it in the event you want for the worksheet/workbook and simply modify it to be more generic (rather than being tied to a specific cell, etc...)

Hope this has been some help.
0
 
calacucciaCommented:
Hi Crimson,

This Sub will do the color change (text color) to color index 3 (RED).
If the cell does not contain #N/A value, the text color is set to black (color index 0)

Sub ErrorTrappingWithColors()
For Each c In ActiveSheet.Range("$A$1:$Z$40").Cells
If WorksheetFunction.IsNA(c) Then
c.Font.ColorIndex = 3
Else
c.Font.ColorIndex = 0
End If
Next c

Question 2:

Sub DateStamp()
ActiveSheet.Range("$B$2") = Date & " " & Time
End Sub

sets the cell B2 to the current date & time, separated by one space.

Hope this helps you

Calacuccia
0
 
calacucciaCommented:
GGilman,

You're right about recording macro's, that helps a lot of syntax questions out of the way.

You seem to have forgotten about the #N/A condition though.

Calacuccia
0
 
crimson30Author Commented:
What do I use instead of ActiveSheet if I have a sheet in mind? I tried Worksheets(Nameoftheworksheet) and it doesn't work...
0
 
crimson30Author Commented:
Got a

runtime error 438: Object doesn't support this property or method for:

For Each c In ActiveSheet.Range("$A$1:$Z$40").c
0
 
ggilmanCommented:
Withdrawing my answer since apparently I was thinking "cell color" and not "cell text color. However, as to your specific sheet:

For Each c In Sheets("Sheet2").Range("$A$1:$Z$40").Cells
0
 
ggilmanCommented:
If you want the time on a specific page too:

Sheets("Sheet2").Range("A1").Cells = "=Now()"
0
 
ggilmanCommented:
Actually the way you use Now() should be based on what you want it to do. If you want it to update at certain times: (from help) "The NOW function changes only when the worksheet is calculated or when a macro that contains the function is run. It is not updated continuously.", use it as above. If you just want to capture the time when the macro is run, use this

Sheets("Sheet2").Range("A1").Cells = Now()
0
 
crimson30Author Commented:
One last question (I think)...

So it's not doing it on unnecessary sheets, what's the vb equivalent of Begin End; or { }

So I can put:

If ActiveSheet.Name="Somesheet" or "Another Sheet" then do

(Begin)

The code snippet, which works great, by the way...

(End)

?
0
 
ggilmanCommented:
No begin. Just an end if:

If ActiveSheet.Name="Somesheet" or "Another Sheet" then

do stuff here
....
....
....
end If

0
 
crimson30Author Commented:
Thank very, very much!

oh and...how do I give Calacuccia points?
0
 
calacucciaCommented:
Seems like I missed a big part...

Crimson, if you want to give points to me (thanks), just open a new question, entitled, "For Calacuccia" and assign the point you want to assign. Then I will step in, and you can accept my answer.

Cheers

Calacuccia
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.