Results 1 to 6 of 6
  1. #1
    Thompyt is offline Expert
    Windows 10 Access 2016
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Font color start, length

    How can I get the correct length for the 6-character length of 5 of 5? I am using InStr(1, CharLen, "o") = 3. I can use InStr(1, CharLen, "o") = 2. "of" is 2 characters long so length is 2, I could use 4 to bracket it if needed. I have tried using If then, elseif, end and select case to change the InStr(1, CharLen, "o") to equal 3,4, or 5 and changing the start position other than the true start. if = 3, then it = 2 etc. No change.

    Code:
    Dim WkBk As Workbook, WkSht As Worksheet
    Dim RCnt As Integer, CharLen As string, l As Long, si As Long
    
     .Range("A:D").Rows(RCnt).Font.ColorIndex = 1
    
    For IPCnt = 6 To 255
          ActiveCell = WorksheetFunction.CountA(Sheets(WkSht.Name).Range("A6:A260")) - DrCnt & " of " & WorksheetFunction.CountA(Sheets(WkSht.Name).Range("A6:A260"))
          CharLen = WorksheetFunction.CountA(Sheets(WkSht.Name).Range("A6:A260")) - DrCnt & " of " & WorksheetFunction.CountA(Sheets(WkSht.Name).Range("A6:A260"))
          ActiveSheet.Range("D2:D" & RCnt).Characters(Start:=InStr(1, CharLen, "o"), Length:=2).Font.ColorIndex = 6
     Next IPCnt


    Output:
    40 of 40
    42 of 42
    15 of 15
    5 of 5
    2 of 2
    4 of 4


    255 of 255
    72 of 72

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Maybe use Split() function. If string will always have 3 parts separated by spaces, consider:
    Part1 = Split("5 of 5", " ")(0)
    Part2 = Split("5 of 5", " ")(1)
    Part3 = Split("5 of 5", " ")(2)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    misread question
    Last edited by CJ_London; 11-29-2022 at 04:26 AM. Reason: misread question

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Maybe I'm also misreading the question, but if you just want the number of characters in e.g. 5 of 5 then use the Len function

    ?Len("5 of 5")
    6

    ?Len("5 of 25")
    7
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Thompyt is offline Expert
    Windows 10 Access 2016
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    I figured out a work around. I was attempting to use len(string) as well. I do understand the split for the most part but couldn't figure a way to get the part2 to change color in vba, other than maybe posting to a cell, changing the font color, the copy and add between Part 1 and 3 call it part4 and then post to the appropriate cell. then delete the single instance of part2. That would be a lot of coding. I added a space after the "f" on of. That enabled the "of" to be a different color: below the bold color is where I added the extra space the change

    Code:
    For IPCnt = 6 To 255
         ActiveCell = WorksheetFunction.CountA(Sheets(WkSht.Name).Range("A6:A260")) - DrCnt & " of " & WorksheetFunction.CountA(Sheets(WkSht.Name).Range("A6:A260"))
             CharLen = WorksheetFunction.CountA(Sheets(WkSht.Name).Range("A6:A260")) - DrCnt & " of " & WorksheetFunction.CountA(Sheets(WkSht.Name).Range("A6:A260"))
                  ActiveSheet.Range("D2:D" & RCnt).Characters(Start:=Len(CharLen) / 2, Length:=2).Font.ColorIndex = 6
    Next IPCnt

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Ah. Now I understand why you couldn't just use Len
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA to change font color
    By data808 in forum Access
    Replies: 5
    Last Post: 10-23-2014, 11:10 AM
  2. Replies: 3
    Last Post: 07-28-2014, 03:02 PM
  3. Change font color in subform
    By rtrinidad in forum Forms
    Replies: 1
    Last Post: 11-04-2012, 01:42 PM
  4. Change as per condition font color
    By miziri in forum Programming
    Replies: 1
    Last Post: 08-20-2009, 04:23 AM
  5. Color/font change in subform
    By AndyKim in forum Forms
    Replies: 9
    Last Post: 06-24-2009, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums