Link to home
Start Free TrialLog in
Avatar of canesbr
canesbr

asked on

Detect VBA Font Colors

VBA statements (default colors) are black, comments are green, errors are red, keywords are blue, etc.
Is it possible to read a VBA code line and examine each character and determine what font color it is? How?
Regards
Brian
Avatar of Wilder1626
Wilder1626
Flag of Canada image

Hi Brian
is it possible to elaborate on this, give use an example of what you are trying to do?
Avatar of canesbr
canesbr

ASKER

Want to detect comments, errors,  key words etc in VBA statements by checking font colors.
Regards
Brian
what would happen if you have 100 comments, errors,  key words etc? do you want to see the result in a another cell or in a message?

ex: Cell "A1" have red color text and in Cell "A2" you will have "Error on Cell A1" message.
This could be a sample of something you can probably do.

But you need to specify if the cells can contain multiple different kind of green, blue or red colors. If that's the case, it will be harder to specify each ColorIndex but still feasible.

In the excel macro file sample in attachment you will see what it does based on VbRed, VbGreen and VbBlue color.

Normally the best would be to always use the same 3 colors. That would simplify your code at the end.

  User generated image
Macro sample:
Private Sub CommandButton1_Click()
 Dim I As Long
    For I = Cells(65536, 1).End(xlUp).Row To 1 Step -1

            If Cells(I, 1).Font.Color = vbRed Then Cells(I, 2) = "Error in cell A" & I
            If Cells(I, 1).Font.Color = vbGreen Then Cells(I, 2) = "Comment in cell A" & I
            If Cells(I, 1).Font.Color = vbBlue Then Cells(I, 2) = "Key word in cell A" & I
            

    Next I

End Sub

Open in new window


Here is a macro sample:
Validate_text_color.xlsm
Avatar of canesbr

ASKER

Looking for colors in VBA text, not in worksheet cells. Unless I missed where you transferred the colored code to cells retaining the colors?
Regards
Brian
how do you get your VBA statements?
Popup message?
In a TextBox inside a UserForm?
In a RichTextBox inside a UserForm?

Else?
Avatar of canesbr

ASKER

VBA statements are in User written Visual Basic Script Code in Subroutines and Functions within Modules within VB Projects in the VBE (Visual Basic Editor)
Regards
Brian
SOLUTION
Avatar of Wilder1626
Wilder1626
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of canesbr

ASKER

Ok, thanks
Regards
Brian
Canesbr, I think you need to be a bit less vague.

You want to detect the colour of the VBA Line.
It sounds like you think that's your solution, but there is little reason to want to do that.

Are you trying to get alerts for error'd code?
Are you trying to count the amount of comments?

What are you hoping to get out of this?

Interacting with VBA is far beyond simple code. it requires plug-ins to interact with.
Have a look at MZ Tools.
http://www.mztools.com/

I've used it for a while providing additional functionality for VBA.
Avatar of canesbr

ASKER

I believe that I have been clear.
Why I want to do this is relevant only if you want to offer alternative approaches. Which I don't want.
Is it possible to programmatically determine the colored text that the eye can see?

Kind regards
Brian
Sure it's possible. Not in excel. Or even VBA.
You'll need to develop an addin for Office VBA.
It would be alot of work. Needing probably a month to get it working properly. With someone who has experience developing VBA addins.

By not saying why, you limit what options we can offer you.
Avatar of canesbr

ASKER

I just wanted an academic solution.
A month? Yikes!


Regards
Brian
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of canesbr

ASKER

Ok then
Bye
Regards
Brian