Solved

2 very easy excel vba questions...

Posted on 2000-03-21
15
204 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 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