Solved

2 very easy excel vba questions...

Posted on 2000-03-21
15
199 Views
Last Modified: 2010-05-02
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.

0
Comment
Question by:crimson30
  • 8
  • 4
  • 3
15 Comments
 
LVL 2

Expert Comment

by:ggilman
ID: 2641311
   Range("A1:Z40").Select
    With Selection.Interior
        .ColorIndex = 46    'Your color here
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
    End With
0
 
LVL 2

Expert Comment

by:ggilman
ID: 2641314
And for the time/date

    Cells(1, 1) = "=Now()"
0
 
LVL 2

Expert Comment

by:ggilman
ID: 2641330
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
 
LVL 17

Expert Comment

by:calacuccia
ID: 2641339
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
 
LVL 17

Expert Comment

by:calacuccia
ID: 2641352
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
 

Author Comment

by:crimson30
ID: 2641552
What do I use instead of ActiveSheet if I have a sheet in mind? I tried Worksheets(Nameoftheworksheet) and it doesn't work...
0
 

Author Comment

by:crimson30
ID: 2641573
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Expert Comment

by:ggilman
ID: 2641601
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
 
LVL 2

Expert Comment

by:ggilman
ID: 2641613
If you want the time on a specific page too:

Sheets("Sheet2").Range("A1").Cells = "=Now()"
0
 
LVL 2

Expert Comment

by:ggilman
ID: 2641644
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
 

Author Comment

by:crimson30
ID: 2641696
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
 
LVL 2

Expert Comment

by:ggilman
ID: 2641756
No begin. Just an end if:

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

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

0
 
LVL 2

Accepted Solution

by:
ggilman earned 20 total points
ID: 2641779
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
 

Author Comment

by:crimson30
ID: 2641862
Thank very, very much!

oh and...how do I give Calacuccia points?
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 2642519
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now