LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 202
Default VBA Count most current 6 months

This is going to be complicated for me to explain. After my explanation I am
pasting in previous code that you all so graciously helped me with a while
back.

In Cell R1 I have "# of sales last 6 months" In that column using similar
criteria as shown below I need to be able to count the number of cells in
rows F-Q (Jul-Jun) that have a number in it but only if it is in the most
recent 6 months (and then to color code it based on the count or tl price.
Cell A3 is the month calculation but set for the previous month so for
instance in Feb, When I open my spreadsheet I want it to automatically count
the # of cells in each row from Aug-Jan. Then in Mar it will automatically
count the cells from Sep-Feb. and count and color code the corresponding cell
in the same row in column R. I need to incorporate this into what I have
below as it is in the same worksheet. Thanks in advance for your help.



Sub ColorFormat(sh As Worksheet)
sh.Activate

Dim Rng As Range, cell As Range
Dim ncol As Integer, lrow As Long
Dim pcnt As Double, divisor As Double
Dim MyCell As Range

' Find column for current Month (add 5 to start in colum F onwards)
' Define "CurMonth" and "HdrMonths" in spreadsheet
ncol = Application.WorksheetFunction.Match(Range("A3"), Range("F3:Q3"), 0) + 5

' Find last row of data in current month column
lrow = Cells(Rows.Count, 1).End(xlUp).Row

'Range("F9:Q500").Select
'Clears the colours
'Selection.Interior.ColorIndex = xlNone

For Each MyCell In Range("F9:Q500")
Select Case MyCell.Interior.ColorIndex
'bright green
Case Is = 4
MyCell.Interior.ColorIndex = xlNone
'light green
Case Is = 35
MyCell.Interior.ColorIndex = xlNone
'light yellow
Case Is = 36
MyCell.Interior.ColorIndex = xlNone
'pink
Case Is = 7
MyCell.Interior.ColorIndex = xlNone
'red
Case Is = 3
MyCell.Interior.ColorIndex = xlNone
End Select
Next MyCell

' Set range to cells for current month (WHICH IS CELL A3) starting row 20
Set Rng = Range(Cells(20, ncol), Cells(lrow, ncol))

' Set Divisor for current month
divisor = Cells(5, ncol)

' Loop through all cells in range
For Each cell In Rng

' Check length of cell in column A
If Len(cell.Offset(0, -(ncol - 1))) = 4 Then
' Calculate perecentage
If Application.IsNumber(cell) Then ' Is this cell a number ?

' Calculate percentage
pcnt = (cell / divisor) * 100
cell.Select
' Set colorindex based on percentage
Select Case pcnt
Case Is 100
Selection.Interior.ColorIndex = 4
Case Is = 90
Selection.Interior.ColorIndex = 35
Case Is = 80
Selection.Interior.ColorIndex = 36
Case Is = 1
Selection.Interior.ColorIndex = 7
Case Else
Selection.Interior.ColorIndex = 3
End Select

' This colors the blank empty cells
Else
cell.Select
Selection.Interior.ColorIndex = 3

End If
End If
Next cell

End Sub


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
current average turnover by months kimfag Excel Worksheet Functions 1 January 29th 09 04:38 PM
In a range of months can I capture the most current month entry? Karlene Excel Discussion (Misc queries) 4 August 15th 07 05:55 PM
Automatically update charts for only the most current 12 months? AIEC33 Charts and Charting in Excel 1 February 28th 06 03:30 AM
sum current months values John Excel Programming 10 August 23rd 05 03:17 PM
sum automatically from last 12 months based on current date CDSchomaker Excel Worksheet Functions 2 December 30th 04 05:28 PM


All times are GMT +1. The time now is 08:51 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"