Consulting

Results 1 to 15 of 15

Thread: Sleeper: Change color of variable

  1. #1
    VBAX Regular
    Joined
    Aug 2005
    Posts
    56
    Location

    Sleeper: Change color of variable

    Wassup...

    hey, anyone know how i can change the color of "scanned" and "thisbook" in this code to eh... lets say blue?


    ActiveCell = Date & ", " & Time & ", " & "One- of BC:[ " & scanned & " ] " & "- Book ID: " & thisbook & ", Deducted from Inventory"

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi russkie, what do you mean exactly? In the cell you want to change the color of it? Can you post what your variables are? What should the data in the cell look like?

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    The best way to find out how to do this would be to record a macro when you do it manually.
    	ActiveCell.FormulaR1C1 = _
    		"01/09/05, 17:31:48, One- of BC:[scanned ] - Book ID: thisbook , Deducted from Inventory"
    	With ActiveCell.Characters(Start:=1, Length:=32).Font
    		.Name = "Arial"
    		.FontStyle = "Regular"
    		.Size = 8
    		.Strikethrough = False
    		.Superscript = False
    		.Subscript = False
    		.OutlineFont = False
    		.Shadow = False
    		.Underline = xlUnderlineStyleNone
    		.ColorIndex = xlAutomatic
    	End With
    	With ActiveCell.Characters(Start:=33, Length:=7).Font
    		.Name = "Arial"
    		.FontStyle = "Regular"
    		.Size = 8
    		.Strikethrough = False
    		.Superscript = False
    		.Subscript = False
    		.OutlineFont = False
    		.Shadow = False
    		.Underline = xlUnderlineStyleNone
    		.ColorIndex = 5
    	End With
    	With ActiveCell.Characters(Start:=40, Length:=14).Font
    		.Name = "Arial"
    		.FontStyle = "Regular"
    		.Size = 8
    		.Strikethrough = False
    		.Superscript = False
    		.Subscript = False
    		.OutlineFont = False
    		.Shadow = False
    		.Underline = xlUnderlineStyleNone
    		.ColorIndex = xlAutomatic
    	End With
    	With ActiveCell.Characters(Start:=54, Length:=8).Font
    		.Name = "Arial"
    		.FontStyle = "Regular"
    		.Size = 8
    		.Strikethrough = False
    		.Superscript = False
    		.Subscript = False
    		.OutlineFont = False
    		.Shadow = False
    		.Underline = xlUnderlineStyleNone
    		.ColorIndex = 5
    	End With
    	With ActiveCell.Characters(Start:=62, Length:=26).Font
    		.Name = "Arial"
    		.FontStyle = "Regular"
    		.Size = 8
    		.Strikethrough = False
    		.Superscript = False
    		.Subscript = False
    		.OutlineFont = False
    		.Shadow = False
    		.Underline = xlUnderlineStyleNone
    		.ColorIndex = xlAutomatic
    	End With

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    You need to take into account the length of the varibles Norie. Plus the length of the date/time.

  5. #5
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by russkie
    Wassup...

    hey, anyone know how i can change the color of "scanned" and "thisbook" in this code to eh... lets say blue?


    ActiveCell = Date & ", " & Time & ", " & "One- of BC:[ " & scanned & " ] " & "- Book ID: " & thisbook & ", Deducted from Inventory"
    there may be more streamlined ways to do this, but I have found that managing the cell's content as characters works quite well. If you turn on the macro recorder and manually edit (font, color, etc) a few words in the cell, you will get something like this:

    Range("E25").Select
        ActiveCell.FormulaR1C1 = "now is the time"
        With ActiveCell.Characters(Start:=1, Length:=3).Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = 3
        End With
        With ActiveCell.Characters(Start:=4, Length:=8).Font
            .Name = "Arial"
            .FontStyle = "Regular"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
        End With
        With ActiveCell.Characters(Start:=12, Length:=4).Font
            .Name = "Arial"
            .FontStyle = "Italic"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = 7
        End With
    As with most macro recorder generated code, you can strip out 80% of the code
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I suggest you put Date & Time into a seperate variable, then your other text into their own variables. Makes it easier to determine lengths, which is what you'll need here.

  7. #7
    VBAX Regular
    Joined
    Aug 2005
    Posts
    56
    Location
    nah, it cant work recording a macro where you change it manually. see the "scanned" and "thisbook" variables change size, so selecting a "position" in the cell constantly varies, the length will change with each book. Those are the barcode and the name of the books im recording into a logbook everytime they are added or subtracted from the inventory. I just want the barcode and the name of the book to stand out so it will be easier to read the Log.

  8. #8
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    russkie

    I know the recorded macro won't work, but what it does do is give you the basic idea of how to do what you want.

    You can adapt the code generated to do exactly what you want.

    Quote Originally Posted by firefytr
    You need to take into account the length of the varibles Norie. Plus the length of the date/time.
    I know, and the length of the text.

    I'm just trying to give the OP a nudge in the right direction.

  9. #9
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    why don't you get the position of "scanned" and "thisbook" with the function Instr

  10. #10
    VBAX Regular
    Joined
    Aug 2005
    Posts
    56
    Location
    but even if i have the position of those 2 variables, i need to tell it where to stop the color change. and to tell it where to stop according to their lengths is tough becuase they change.

    see, i tried to just add a "change color" code before and after the variables but that doesnt work, it wont take code in the middle of defining whats inside of the cell.

    i just thought there was a simpler way to just change the color of whats in those variables BEFORE i put them into the cell...?

  11. #11
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Try this.
    str1 = "scanned"
    str2 = "thisbook"
    ActiveCell = Date & ", " & Time & ", One- of BC:[" & str1 & _
    			" ] - Book ID:" & str2 & " , Deducted from Inventory"
    		
    str3 = ActiveCell.Text
    With ActiveCell.Characters(Start:=InStr(str3, str1), Length:=Len(str1)).Font
    		.Name = "Arial"
    		.FontStyle = "Regular"
    		.Size = 8
    		.Strikethrough = False
    		.Superscript = False
    		.Subscript = False
    		.OutlineFont = False
    		.Shadow = False
    		.Underline = xlUnderlineStyleNone
    		.ColorIndex = 5
    End With
    With ActiveCell.Characters(Start:=InStr(str3, str2), Length:=Len(str2)).Font
    		.Name = "Arial"
    		.FontStyle = "Regular"
    		.Size = 8
    		.Strikethrough = False
    		.Superscript = False
    		.Subscript = False
    		.OutlineFont = False
    		.Shadow = False
    		.Underline = xlUnderlineStyleNone
    		.ColorIndex = 5
    End With

  12. #12
    VBAX Regular
    Joined
    Aug 2005
    Posts
    56
    Location
    poyfect!

    thanks alot man.

  13. #13
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    A very good possibility ALe. Here is an example of what I would do ...

    Sub testfoo()
    Dim scanned As String, thisbook As String
        Dim dt As String
        Dim dtLen As Long
        Dim sScan As Long, eScan As Long, sThis As Long, eThis As Long
        Dim txt1 As String, txt2 As String, txt3 As String
    scanned = "scanned"
        thisbook = "thisbook"
    dt = Date & ", " & Time & ", " & "One- of BC:[ "
        txt1 = " ] - Book ID: "
        txt2 = ", Deducted from Inventory"
        txt3 = dt & scanned & txt1 & thisbook & txt2
    sScan = Len(dt) + 1
        eScan = sScan + Len(scanned)
        sThis = Len(dt) + Len(scanned) + Len(txt1) + 1
        eThis = sThis + Len(thisbook)
    ActiveCell = txt3
        ActiveCell.Characters(sScan, eScan - sScan).Font.ColorIndex = 3
        ActiveCell.Characters(sThis, eThis - sThis).Font.ColorIndex = 3
    End Sub

  14. #14
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Wow, well done firefytr. That is what I was thinking. The fact is that I think, you do.
    Thanks for the code, maybe it'll be used.
    Actually I was also thinking about other methods but I couldn't find any else.

  15. #15
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Sub test()
    Dim str1 As String
    Dim str2 As String
    Dim str3 As String
        str1 = "scanned"
        str2 = "thisbook"
        ActiveCell = Date & ", " & Time & ", One- of BC:[" & str1 & _
                    " ] - Book ID:" & str2 & " , Deducted from Inventory"
    str3 = ActiveCell.Text
        ActiveCell.Characters(Start:=InStr(str3, str1), Length:=Len(str1)).Font.ColorIndex = 5
    ActiveCell.Characters(Start:=InStr(str3, str2), Length:=Len(str2)).Font.ColorIndex = 5
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •