?
Solved

2 very easy excel vba questions...

Posted on 2000-03-21
15
Medium Priority
?
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 80 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

752 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