A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Am 23.08.2010 15:52, schrieb CMW_001:
Thanks Andreas, that partially works.
I am glad to hear this. :-)
I get a type mismatch error if I just enter the ranges as follows:
StartDate = Range("D2:D20")
EndDate = Range("E2:E20")
This is logical, I explain it once briefly.
When you make an assignment of one cell to one variable, then the contents of the cell is written into this variable.
The variable Startdate has the type "Date", so if we say
StartDate = Range("A1")
the value of cell A1 will be written into the variable and thus becomes a date.
If you specify more than one cell like
StartDate = Range("A1:A2")
you get a RTE 13 because the range returns an array of values and startdate can hold only one value.
Apologies but I'm not familiar with the Intersect function. Does this recognise when there is an intersection at row level or do I need a loop of some kind?
You are right, we need a loop through the dates.
Intersect is a very powerful tool, try around a little bit with this macro, make some selections in a sheet, with not-related areas too and look what the MsgBox shows.
Sub Example_Intersect()
Dim R As Range, C As Range
Dim Msg As String
If Not TypeOf Selection Is Range Then
MsgBox "You did not select a range"
Else
Set R = Intersect(Selection, Rows(10))
Set C = Intersect(Selection, Columns("F"))
If R Is Nothing Then
Msg = Msg & "Your selection intersects not Row 10"
Else
Msg = Msg & "Your selection intersects Row 10 at " & R.Address(0, 0)
End If
Msg = Msg & vbCrLf
If C Is Nothing Then
Msg = Msg & "and not Column F"
Else
Msg = Msg & "and intersects Column F at " & C.Address(0, 0)
End If
MsgBox Msg
End If
End Sub
I think after a few tries, you'll understand what Intersect can do.
Back to your original question, the loop is very easy to program, but the many different colors are quite expensive... well not really, but I'm just lazy. ;-)
But we can do it with a nice trick very easy by simply reads the color of a cell. :-)
I simply take the cell containing the start date, if you don't like this feature or if you want a different cell and if you need help, just ask again.
Andreas.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim R As Range, C As Range
Dim FirstDate As Date, LastDate As Date
Dim StartDate As Date, EndDate As Date
Dim ClearLine As Boolean
'If nothing of our interest is changed we're done
Set Target = Intersect(Target, Range("F6:AQ20,D6:D20,E6:E20"))
If Target Is Nothing Then Exit Sub
'Don't flicker with the screen
Application.ScreenUpdating = False
'Walk trough the first column
For Each R In Range("F6:F20")
'Get the dates from this row
FirstDate = R
LastDate = Range("AQ" & R.Row)
'Create the ganttline for each task
ClearLine = True
'Walk trough the dates
For Each C In Range("D6:D20")
'Get the start date from column D
StartDate = C
'Get the end date from column E same row as in Column D
EndDate = C.Offset(0, 1)
'Create the ganttline
CreateGanttLine Range(R, Range("AQ" & R.Row)), _
StartDate, EndDate, FirstDate, LastDate, C.Interior.ColorIndex, ClearLine
'Overwrite the colored cells next time
ClearLine = False
Next
Next
'Show the result
Application.ScreenUpdating = True
End Sub