+ Reply to Thread
Results 1 to 11 of 11

Imported VBA code not working....

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Question Imported VBA code not working....

    Hello all...

    Last year, member rylo, helped me with some VBA code for one of my projects. If you are reading this post, rylo, you might recall the VBA code that you helped me with...and maybe you can help me once more. Otherwise, maybe some other members can help me.

    The VBA code below was provided by rylo last year. The code was designed to cycle through a list of teams...and FILL the background of each cell in the list with color...depending on which named range a match was found in.

    Sub SetNiteColor()
    'AUTHOR:   rylo
    'DATE: 2 Sep 2007
    'REFERENCE: http://excelforum.com/showthread.php?t=612291
      
      'set default color for text
      Range("D5:R36").Font.ColorIndex = xlAutomatic
      'array of the night games defined names
      nitearr = Array("THURS_GAMES", "FRI_SAT_GAMES", "SUN_NITE_GAMES", "MON_NITE_GAMES")
      'cycle through all the relevant columns
      For coll = 4 To 18
        'determine color
        colorind = ""
      'set a reference for the date being processed
        Set findit = Sheets("sheet2").Range("E54:F112").Find(what:=Cells(4, coll), LookIn:=xlFormulas)
        If Not findit Is Nothing Then 'a match is found
        'cycle through the night arrays, and determine the relevant colorindex
          For i = LBound(nitearr) To UBound(nitearr)
            If Not Intersect(findit, Range(nitearr(i))) Is Nothing Then
          
              Select Case nitearr(i)
                Case "THURS_GAMES", "FRI_SAT_GAMES"
                  colorind = 7
                Case "SUN_NITE_GAMES"
                  colorind = 3
                Case "MON_NITE_GAMES"
                  colorind = 5
              End Select
            
            End If
          Next i
        End If
        coloff = 1 'reference offset to get the team names for the date
        'used to determine the column to search for the team.  As there are 2 date columns, but
        'only one team column due to the merge, have to determine the relevant column
        If Not findit Is Nothing Then
          If coll Mod 2 = 0 Then
            teamcoll = coll
          Else
            teamcoll = coll - 1
          End If
        'find the team for the date, and set the font color
        Do
          Set findteam = Cells(5, teamcoll).Resize(32, 2).Find(what:=findit.Offset(0, coloff).Value, LookIn:=xlValues)
          If Not findteam Is Nothing Then
            findteam.Font.ColorIndex = colorind
          End If
          coloff = coloff + 2
        Loop Until IsEmpty(findit.Offset(0, coloff))
        End If
      Next coll
    End Sub
    This year I put together a slightly different spreadsheet and made the following changes to the VBA to conform to my new spreadsheet:

    Sub SetNiteColor()
    'AUTHOR:   rylo
    'DATE: 2 Sep 2007
    'REFERENCE: http://excelforum.com/showthread.php?t=612291
      
      'set default color for text
      Range("$E$5:$AR$36").Font.ColorIndex = xlAutomatic
      'array of the night games defined names
      nitearr = Array("ByeWeekTeams", "Th_Fr_Sa_Games", "MondayGames", "SundayGames")
      'cycle through all the relevant columns
      For coll = 5 To 43
        'determine color
        colorind = ""
      'set a reference for the date being processed
        Set findit = Sheets("sheet2").Range("F8:F73").Find(what:=Cells(5, coll), LookIn:=xlValues)
        If Not findit Is Nothing Then 'a match is found
        'cycle through the night arrays, and determine the relevant colorindex
          For i = LBound(nitearr) To UBound(nitearr)
            If Not Intersect(findit, Range(nitearr(i))) Is Nothing Then
          
              Select Case nitearr(i)
                Case "Th_Fr_Sa_Games"
                  colorind = 9
                Case "ByeWeekTeams"
                  colorind = 7
                Case "MondayGames"
                  colorind = 5
                Case "SundayGames"
                  colorind = 3
              End Select
            
            End If
          Next i
        End If
        coloff = 1 'reference offset to get the team names for the date
        'used to determine the column to search for the team.  As there are 2 date columns, but
        'only one team column due to the merge, have to determine the relevant column
        If Not findit Is Nothing Then
          If coll Mod 2 = 0 Then
            teamcoll = coll
          Else
            teamcoll = coll - 1
          End If
        'find the team for the date, and set the font color
        Do
          Set findteam = Cells(6, teamcoll).Resize(32, 2).Find(what:=findit.Offset(0, coloff).Value, LookIn:=xlValues)
          If Not findteam Is Nothing Then
            findteam.Font.ColorIndex = colorind
          End If
          coloff = coloff + 2
        Loop Until IsEmpty(findit.Offset(0, coloff))
        End If
      Next coll
    End Sub
    When I run the code, I get a Runtime error -' 1004': Method 'range' of object '_global' failed. (This error occurs on the VBA code line that is enlarged in the second version of the code, above. I tried to change the font color...but my color palette wouldn't open).

    Three questions....

    1. What part of the code did I change incorrectly? ...Or... Did I overlook any needed change(s)?

    2. How can I format the font color along with the background color using this VBA?

    3. The range $E$5:$AR$36 on sheet "MasterCopy" has been given a name.
    Can't I replace the RANGE ($E$5:$AR$36) with the RANGE NAME ("Selections_MasterCopy")?

    Thank you for any help you can provide....

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    The original code has two columns (E54:F112), the new version only one (F8:F73), might that be an issue?
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Talking

    The original code has two columns (E54:F112), the new version only one (F8:F73), might that be an issue?
    The referenced columns contain dates.

    In the older version, the dates appeared in merged cells.
    The dates in the new version occupy individual cells.

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Husker

    Vaguely remember this. Can you attach an example workbook of your current structure with the new code.

    rylo

  5. #5
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167
    Can you attach an example workbook of your current structure with the new code.
    You bet I can.....!!!

    But...don't have the time at the moment. (dammit...!!!)

    Will get it to you as soon as I can. (Might be tomorrow....don't give up on me....)

  6. #6
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167
    Here is the truncated spreadsheet:
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Took me a while!!!

    Change the line
    Set findit = Sheets("sheet2").Range("F8:F73").Find(what:=Cells(5, coll), LookIn:=xlValues)
    to
    Set findit = Sheets("sheet2").Range("F8:F73").Find(what:=Format(Cells(4, coll), Sheets("sheet2").Range("F8").NumberFormat), LookIn:=xlValues)
    You will also have to get rid of the days in sheet2!G19:G27 and make them teams.

    When you have done that, if you can't get it to work, then put up the revised workbook and I'll have another look.

    rylo

  8. #8
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167
    Thanks, rylo....

    I made the changes to the VBA code as suggested.

    As far as the DAYS were concerned, I simply eliminated them, as I don't
    really need them. Then I moved all of the teams one column to the left.

    When I press the RANDOMIZE button on Sheet2...I get a Run-time error.

    Run-time error '1004':
    Unable to set the ColorIndex property of the Font Class


    This error occurs AFTER your code changes...near the end of the subroutine;

        'find the team for the date, and set the font color
        Do
          Set findteam = Cells(6, teamcoll).Resize(32, 2).Find(what:=findit.Offset(0, coloff).Value, LookIn:=xlValues)
          If Not findteam Is Nothing Then
            findteam.Font.ColorIndex = colorind
          End If
          coloff = coloff + 2
        Loop Until IsEmpty(findit.Offset(0, coloff))
        End If
      Next coll
    End Sub
    Also, is there a simple way to add code to set the FONT for each CASE above??

    I've attached the updated sample for your inspection.
    Thanks, so much, for your help...
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Instead of actioning a range that has teams, you are going from column 5 to column 43 and this range included blank columns (21-23). It is having trouble establishing a value for colorind in the case statement.

    Perhaps enhance the case statement to have a default colorind of say xlnone. See how that goes.


    rylo

  10. #10
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167
    I'm going to try that......

    Or....Maybe is should just execute the procedure TWICE....once for each half of the range...........????????

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. code to stop working being shown
    By tryer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-15-2008, 01:32 PM
  2. Run code but carry on working
    By stoney1977 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2008, 12:58 AM
  3. code not working
    By SRussell in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-16-2008, 08:11 AM
  4. Replies: 7
    Last Post: 03-03-2008, 11:48 AM
  5. Unlinking Imported Data
    By Donquick in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-15-2007, 09:29 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1