+ Reply to Thread
Results 1 to 12 of 12

Loop for if then calculation

Hybrid View

  1. #1
    rwnelson
    Guest

    Loop for if then calculation

    I am trying to create a calendar that will automatically color and
    shade the cells according to certain conditions. I can't use
    Conditional Formatting because if I try to copy and paste items from
    the calendar, it copies the manual formatting (which I want to copy) as
    well as the conditional formatting (which I do not want).

    I created the conditions using VBA which work perfectly but it is
    rather lengthy. I currently have 126 individual If/Then functions for
    the formatting. I would like to know if there is a way to loop the
    conditions so that it can significantly shorten the code. I am new to
    the programming world and I was able to figure the following code out
    from this news group.

    Date = now()

    If Range("F7").Value < Date Then Range("F7:G13").Interior.Pattern =
    xlGray50 Else Range("F7:G13").Interior.Pattern = xlSolid
    If Range("f7").Value >= 1 And Range("F7:G13").Interior.ColorIndex = 15
    Then Range("F7:G13").Interior.ColorIndex = 37
    If Range("f7").Value = "" Then Range("F7:G13").Interior.ColorIndex = 15

    These 3 lines of code are for a single block in the calendar and there
    are a total of 42 blocks (7 across x 6 down) with the ranges of
    F7:F13, H7:H13, J7:J13, L7:L13, N7:N13, P7:Q13, R7:R13 and then going
    down the sheet to F14:F20, H14:H20, and on and on and on to a final
    R35:S41.

    Any help would be appreciated.


  2. #2
    Ardus Petus
    Guest

    Re: Loop for if then calculation

    Try the following code (untested)

    HTH
    --
    AP

    '-------------------------------------------
    Sub colors()

    Dim iRow As Long
    Dim iCol As Long

    'Boucle sur les colonnes
    For iCol = Range("F7").Column To Range("R7").Column Step 7
    'Boucle sur les lignes
    For iRow = Range("F7").Row To Range("R35").Row Step 2
    doRange Cells(iRow, iCol)
    Next iRow
    Next iCol

    End Sub

    Sub doRange(rngTopLeft As Range)
    With rngTopLeft.Resize(2, 7)
    If rngTopLeft.Value < Date Then _
    .Interior.Pattern = xlSolid
    If rngTopLeft.Value >= 1 And .Interior.ColorIndex = 15 Then _
    .Interior.ColorIndex = 37
    If rngTopLeft.Value = "" Then _
    .Interior.ColorIndex = 15
    End With
    End Sub



  3. #3
    rwnelson
    Guest

    Re: Loop for if then calculation

    Thank you for your response but this code did not work.

    The cell shading line shaded all cells. If the top left target cell
    value was not < the current date, all cells from F7:S48 were still
    shaded except for ranges F7:L8, F21:L22, and F35:L36.

    This code turned all cells except the above-mentioned ranges gray
    without regard to the date and it turned range F7:L8 colorindex 37
    while F21:L22 and F35:L36 had no color.

    The original code did not go down to the bottom of the calenar so I
    edited to following line from:

    For iRow = Range("F7").Row To Range("R35").Row Step 2

    to

    For iRow = Range("F7").Row To Range("S48").Row Step 2
    ___________

    This was the only fix I could find that helped. Granted I'm not too
    familiar with programming but looking at the code, it seemed logical
    and it looks like it should work but for some reason it didn't.


  4. #4
    Ardus Petus
    Guest

    Re: Loop for if then calculation

    Try this:

    '----------------------------------
    Sub colors()

    Dim iRow As Long
    Dim iCol As Long

    'Boucle sur les colonnes
    For iCol = Range("F7").Column To Range("R7").Column Step 7
    'Boucle sur les lignes
    For iRow = Range("F7").Row To Range("R48").Row Step 2
    doRange Cells(iRow, iCol)
    Next iRow
    Next iCol

    End Sub

    Sub doRange(rngTopLeft As Range)
    With rngTopLeft.Resize(2, 7)
    If rngTopLeft.Value < Date Then
    .Interior.Pattern = xlGray50
    Else
    .Interior.Pattern = xlSolid
    End If
    If rngTopLeft.Value >= 1 And .Interior.ColorIndex = 15 Then _
    .Interior.ColorIndex = 37
    If rngTopLeft.Value = "" Then _
    .Interior.ColorIndex = 15
    End With
    End Sub
    '------------------------------

    Cheers,
    --
    AP


    "rwnelson" <rw_nelson20@hotmail.com> a écrit dans le message de
    news:1142121424.429954.208860@p10g2000cwp.googlegroups.com...
    > Thank you for your response but this code did not work.
    >
    > The cell shading line shaded all cells. If the top left target cell
    > value was not < the current date, all cells from F7:S48 were still
    > shaded except for ranges F7:L8, F21:L22, and F35:L36.
    >
    > This code turned all cells except the above-mentioned ranges gray
    > without regard to the date and it turned range F7:L8 colorindex 37
    > while F21:L22 and F35:L36 had no color.
    >
    > The original code did not go down to the bottom of the calenar so I
    > edited to following line from:
    >
    > For iRow = Range("F7").Row To Range("R35").Row Step 2
    >
    > to
    >
    > For iRow = Range("F7").Row To Range("S48").Row Step 2
    > ___________
    >
    > This was the only fix I could find that helped. Granted I'm not too
    > familiar with programming but looking at the code, it seemed logical
    > and it looks like it should work but for some reason it didn't.
    >




  5. #5
    rwnelson
    Guest

    Re: Loop for if then calculation

    Still getting the same thing. Is there a way to display my sheet on
    this site so that you may be able to better evaluate it?


  6. #6
    rwnelson
    Guest

    Re: Loop for if then calculation

    Just as a test, I edited the doRange code just to see which cells were
    identified as rngTopLeft. In column F, beginning at F7, every other
    cell blue, instead of the top left of every block (i.e., F7, F9,
    F11.....F47). The same goes for Column M. The code I entered is as
    follows:

    With rngTopLeft
    .Interior.ColorIndex = 37
    End With

    End Sub


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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