Solved

2 very easy excel vba questions...

Posted on 2000-03-21
15
200 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA Excel: Copy Data from one workbook to another v6 csv extension 15 77
vb6 programatically bring window to front 8 63
Hide vba in gp 7 80
How to Add / Edit Windows Menu 4 50
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…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

914 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

20 Experts available now in Live!

Get 1:1 Help Now