Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
time selection
Hi,
I have 4 spreadsheets in my workbook called "master","1", "2", and "3". In column K of the "master" sheet, the cell format is 'dd h:mm:ss'. How do i write a macro that would go through that column....if it's less than 5 hrs then move the row (from colum A to K) to sheet "1"...if it's between 5hrs and 24 hrs then move that row to sheet "2" and if it's more than 24 hrs then move to sheet "3"... the macro will keep on doing that until there is a blank line... thanks. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
time selection
With that format how are you managing to enter zero days without it decoming
text? -- Regards, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Don Doan" wrote in message ... Hi, I have 4 spreadsheets in my workbook called "master","1", "2", and "3". In column K of the "master" sheet, the cell format is 'dd h:mm:ss'. How do i write a macro that would go through that column....if it's less than 5 hrs then move the row (from colum A to K) to sheet "1"...if it's between 5hrs and 24 hrs then move that row to sheet "2" and if it's more than 24 hrs then move to sheet "3"... the macro will keep on doing that until there is a blank line... thanks. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
time selection
Hi,
actually before column K, there are 2 other columns with the format yyyy/mm/dd hh:mm:ss... column K is the result of one column minus the other column "Sandy Mann" wrote: With that format how are you managing to enter zero days without it decoming text? -- Regards, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Don Doan" wrote in message ... Hi, I have 4 spreadsheets in my workbook called "master","1", "2", and "3". In column K of the "master" sheet, the cell format is 'dd h:mm:ss'. How do i write a macro that would go through that column....if it's less than 5 hrs then move the row (from colum A to K) to sheet "1"...if it's between 5hrs and 24 hrs then move that row to sheet "2" and if it's more than 24 hrs then move to sheet "3"... the macro will keep on doing that until there is a blank line... thanks. |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
time selection
May I please ask this question again:
Run this code and the Message Box reports the Row number that is being *Copied* (ie cell.Row) and all cells are copied as you would expect. Change the .Copy to .Cut and run the code again and the Message Box reports the Row number that is being *Pasted to* and it misses out Row 2 completely. Is this the result of poor coding technique or does it only happen in XL97? Thank you to anyone who tests this out for me. Sub Test() PasteRow = 10 For Each cell In Range(Cells(1, 7), Cells(10, 7)) Range(Cells(cell.Row, 7), Cells(cell.Row, 7)) _ .Copy Destination:= _ Sheets("Sheet2").Cells(PasteRow, 1) MsgBox cell.Row PasteRow = PasteRow + 1 Next cell End Sub -- Regards, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Sandy Mann" wrote in message ... I posted this code below last night, my apologies for the poor line wrap so I am posting it again: The code works as it is but if I replace the *Copy* with *Cut* it misses out row 4. Is this the result of poor programming skills or a problem with my version of XL (XL967)? I have tried putting messageboxes throughout the code to report the Cell.Row number and with *Copy* it indexes as expected but with *Cut* it reports the row number into which it is pasting the Cut range (2 in my case). Then it indexes *Cell* to row 5 but *only* on this first pass - all the rest of the code cuts/pastes as expected. If I change it to a For x = 3 to LastRow/ Next x loop it works as expected but can someone please tell me what I am doing wrong in the For Each loop? Sub MoveIt2() Dim LastRow As Long Dim cell As Range Dim LastRow1 As Long Dim LastRow2 As Long Dim LastRow3 As Long Dim cValue As Double Sheets("Master").Activate LastRow = Cells(Rows.Count, 11).End(xlUp).Row LastRow1 = Sheets("1").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow2 = Sheets("2").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow3 = Sheets("3").Cells(Rows.Count, 11).End(xlUp).Row + 1 For Each cell In Range(Cells(3, 11), Cells(LastRow, 11)) If cell.Value < "" Then cValue = cell.Value Select Case cValue Case Is < 5 / 24 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("1").Cells(LastRow1, 1) LastRow1 = LastRow1 + 1 GoTo GetOut Case Is <= 1 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("2").Cells(LastRow2, 1) LastRow2 = LastRow2 + 1 GoTo GetOut Case Else Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("3").Cells(LastRow3, 1) LastRow3 = LastRow3 + 1 GetOut: End Select End If Next cell End Sub -- HTH Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Sandy Mann" wrote in message ... This code assumes that the data starts in Row 3: Sub MoveIt2() Dim LastRow As Long Dim cell As Range Dim LastRow1 As Long Dim LastRow2 As Long Dim LastRow3 As Long Dim cValue As Double Sheets("Master").Activate LastRow = Cells(Rows.Count, 11).End(xlUp).Row LastRow1 = Sheets("1").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow2 = Sheets("2").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow3 = Sheets("3").Cells(Rows.Count, 11).End(xlUp).Row + 1 For Each cell In Range(Cells(3, 11), Cells(LastRow, 11)) If cell.Value < "" Then cValue = cell.Value Select Case cValue Case Is < 5 / 24 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("1").Cells(LastRow1, 1) LastRow1 = LastRow1 + 1 GoTo GetOut Case Is <= 1 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("2").Cells(LastRow2, 1) LastRow2 = LastRow2 + 1 GoTo GetOut Case Else Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("3").Cells(LastRow3, 1) LastRow3 = LastRow3 + 1 GetOut: End Select End If Next cell End Sub If you want to remove the data as well then clear the constants after the copying has been done. -- HTH Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Don Doan" wrote in message ... Hi, actually before column K, there are 2 other columns with the format yyyy/mm/dd hh:mm:ss... column K is the result of one column minus the other column "Sandy Mann" wrote: With that format how are you managing to enter zero days without it decoming text? -- Regards, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Don Doan" wrote in message ... Hi, I have 4 spreadsheets in my workbook called "master","1", "2", and "3". In column K of the "master" sheet, the cell format is 'dd h:mm:ss'. How do i write a macro that would go through that column....if it's less than 5 hrs then move the row (from colum A to K) to sheet "1"...if it's between 5hrs and 24 hrs then move that row to sheet "2" and if it's more than 24 hrs then move to sheet "3"... the macro will keep on doing that until there is a blank line... thanks. |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
time selection
I have just gone back into the place that I used to work and tried the below
code in XL2002 with the same results so it is not just my XL97. I therefore assume that it is my poor programming or part of Microsoft's grand design with For Each/Next . Cell.row seems to be changed to the Row number being pasted to when Cut is used but not when Copy is used so I assume that there is something that is not resetting the variable *cell* to 2 ( or setting *cell* to 2 before *Next cell* is encountered), and therefore it is missing out row 2. Sub Trial() Dim LastRow As Long Dim PasteRow As Long LastRow = 10 PasteRow = 10 For Each cell In Range("A1:A10") Cells(cell.Row, 1).Cut Destination:= _ Sheets("Sheet2").Cells(PasteRow, 1) Debug.Print cell.Row PasteRow = PasteRow + 1 Next cell End Sub The above code returns 1, 3, 4, 5, 6, 7, 8, 9, 10 in the immediate wondow. Changing the Cut to Copy and you get the 2 included in the immediate window. Does anyone ave any idea why? -- Reagrds, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Sandy Mann" wrote in message ... May I please ask this question again: Run this code and the Message Box reports the Row number that is being *Copied* (ie cell.Row) and all cells are copied as you would expect. Change the .Copy to .Cut and run the code again and the Message Box reports the Row number that is being *Pasted to* and it misses out Row 2 completely. Is this the result of poor coding technique or does it only happen in XL97? Thank you to anyone who tests this out for me. Sub Test() PasteRow = 10 For Each cell In Range(Cells(1, 7), Cells(10, 7)) Range(Cells(cell.Row, 7), Cells(cell.Row, 7)) _ .Copy Destination:= _ Sheets("Sheet2").Cells(PasteRow, 1) MsgBox cell.Row PasteRow = PasteRow + 1 Next cell End Sub -- Regards, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Sandy Mann" wrote in message ... I posted this code below last night, my apologies for the poor line wrap so I am posting it again: The code works as it is but if I replace the *Copy* with *Cut* it misses out row 4. Is this the result of poor programming skills or a problem with my version of XL (XL967)? I have tried putting messageboxes throughout the code to report the Cell.Row number and with *Copy* it indexes as expected but with *Cut* it reports the row number into which it is pasting the Cut range (2 in my case). Then it indexes *Cell* to row 5 but *only* on this first pass - all the rest of the code cuts/pastes as expected. If I change it to a For x = 3 to LastRow/ Next x loop it works as expected but can someone please tell me what I am doing wrong in the For Each loop? Sub MoveIt2() Dim LastRow As Long Dim cell As Range Dim LastRow1 As Long Dim LastRow2 As Long Dim LastRow3 As Long Dim cValue As Double Sheets("Master").Activate LastRow = Cells(Rows.Count, 11).End(xlUp).Row LastRow1 = Sheets("1").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow2 = Sheets("2").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow3 = Sheets("3").Cells(Rows.Count, 11).End(xlUp).Row + 1 For Each cell In Range(Cells(3, 11), Cells(LastRow, 11)) If cell.Value < "" Then cValue = cell.Value Select Case cValue Case Is < 5 / 24 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("1").Cells(LastRow1, 1) LastRow1 = LastRow1 + 1 GoTo GetOut Case Is <= 1 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("2").Cells(LastRow2, 1) LastRow2 = LastRow2 + 1 GoTo GetOut Case Else Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("3").Cells(LastRow3, 1) LastRow3 = LastRow3 + 1 GetOut: End Select End If Next cell End Sub -- HTH Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Sandy Mann" wrote in message ... This code assumes that the data starts in Row 3: Sub MoveIt2() Dim LastRow As Long Dim cell As Range Dim LastRow1 As Long Dim LastRow2 As Long Dim LastRow3 As Long Dim cValue As Double Sheets("Master").Activate LastRow = Cells(Rows.Count, 11).End(xlUp).Row LastRow1 = Sheets("1").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow2 = Sheets("2").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow3 = Sheets("3").Cells(Rows.Count, 11).End(xlUp).Row + 1 For Each cell In Range(Cells(3, 11), Cells(LastRow, 11)) If cell.Value < "" Then cValue = cell.Value Select Case cValue Case Is < 5 / 24 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("1").Cells(LastRow1, 1) LastRow1 = LastRow1 + 1 GoTo GetOut Case Is <= 1 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("2").Cells(LastRow2, 1) LastRow2 = LastRow2 + 1 GoTo GetOut Case Else Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("3").Cells(LastRow3, 1) LastRow3 = LastRow3 + 1 GetOut: End Select End If Next cell End Sub If you want to remove the data as well then clear the constants after the copying has been done. -- HTH Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Don Doan" wrote in message ... Hi, actually before column K, there are 2 other columns with the format yyyy/mm/dd hh:mm:ss... column K is the result of one column minus the other column "Sandy Mann" wrote: With that format how are you managing to enter zero days without it decoming text? -- Regards, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Don Doan" wrote in message ... Hi, I have 4 spreadsheets in my workbook called "master","1", "2", and "3". In column K of the "master" sheet, the cell format is 'dd h:mm:ss'. How do i write a macro that would go through that column....if it's less than 5 hrs then move the row (from colum A to K) to sheet "1"...if it's between 5hrs and 24 hrs then move that row to sheet "2" and if it's more than 24 hrs then move to sheet "3"... the macro will keep on doing that until there is a blank line... thanks. |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
time selection
Some times I talk absolute rubbish.
The above code returns 1, 3, 4, 5, 6, 7, 8, 9, 10 in the immediate wondow. It does of course return 10, 11, 12, 13, 14, 15, 16, 17, 18 The obvious - that it is my poor programming techniques - eventually dawned on me. I had been regarding *cell* as being a counting variable but it is really a Range object. That is why when I use Cut and I am actually moving the Cell, cell.Row changes to where *cell* is. This is similar to changing the counting variable during a loop, which is obviously a very bad thing to do. The only thing that puzzles me now is not "Why does the code miss out Row 2" but rather "How does Excel manage to deal correctly with all the other Rows!" No need to respond to these musings -- Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Sandy Mann" wrote in message ... I have just gone back into the place that I used to work and tried the below code in XL2002 with the same results so it is not just my XL97. I therefore assume that it is my poor programming or part of Microsoft's grand design with For Each/Next . Cell.row seems to be changed to the Row number being pasted to when Cut is used but not when Copy is used so I assume that there is something that is not resetting the variable *cell* to 2 ( or setting *cell* to 2 before *Next cell* is encountered), and therefore it is missing out row 2. Sub Trial() Dim LastRow As Long Dim PasteRow As Long LastRow = 10 PasteRow = 10 For Each cell In Range("A1:A10") Cells(cell.Row, 1).Cut Destination:= _ Sheets("Sheet2").Cells(PasteRow, 1) Debug.Print cell.Row PasteRow = PasteRow + 1 Next cell End Sub The above code returns 1, 3, 4, 5, 6, 7, 8, 9, 10 in the immediate wondow. Changing the Cut to Copy and you get the 2 included in the immediate window. Does anyone ave any idea why? -- Reagrds, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Sandy Mann" wrote in message ... May I please ask this question again: Run this code and the Message Box reports the Row number that is being *Copied* (ie cell.Row) and all cells are copied as you would expect. Change the .Copy to .Cut and run the code again and the Message Box reports the Row number that is being *Pasted to* and it misses out Row 2 completely. Is this the result of poor coding technique or does it only happen in XL97? Thank you to anyone who tests this out for me. Sub Test() PasteRow = 10 For Each cell In Range(Cells(1, 7), Cells(10, 7)) Range(Cells(cell.Row, 7), Cells(cell.Row, 7)) _ .Copy Destination:= _ Sheets("Sheet2").Cells(PasteRow, 1) MsgBox cell.Row PasteRow = PasteRow + 1 Next cell End Sub -- Regards, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Sandy Mann" wrote in message ... I posted this code below last night, my apologies for the poor line wrap so I am posting it again: The code works as it is but if I replace the *Copy* with *Cut* it misses out row 4. Is this the result of poor programming skills or a problem with my version of XL (XL967)? I have tried putting messageboxes throughout the code to report the Cell.Row number and with *Copy* it indexes as expected but with *Cut* it reports the row number into which it is pasting the Cut range (2 in my case). Then it indexes *Cell* to row 5 but *only* on this first pass - all the rest of the code cuts/pastes as expected. If I change it to a For x = 3 to LastRow/ Next x loop it works as expected but can someone please tell me what I am doing wrong in the For Each loop? Sub MoveIt2() Dim LastRow As Long Dim cell As Range Dim LastRow1 As Long Dim LastRow2 As Long Dim LastRow3 As Long Dim cValue As Double Sheets("Master").Activate LastRow = Cells(Rows.Count, 11).End(xlUp).Row LastRow1 = Sheets("1").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow2 = Sheets("2").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow3 = Sheets("3").Cells(Rows.Count, 11).End(xlUp).Row + 1 For Each cell In Range(Cells(3, 11), Cells(LastRow, 11)) If cell.Value < "" Then cValue = cell.Value Select Case cValue Case Is < 5 / 24 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("1").Cells(LastRow1, 1) LastRow1 = LastRow1 + 1 GoTo GetOut Case Is <= 1 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("2").Cells(LastRow2, 1) LastRow2 = LastRow2 + 1 GoTo GetOut Case Else Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("3").Cells(LastRow3, 1) LastRow3 = LastRow3 + 1 GetOut: End Select End If Next cell End Sub -- HTH Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Sandy Mann" wrote in message ... This code assumes that the data starts in Row 3: Sub MoveIt2() Dim LastRow As Long Dim cell As Range Dim LastRow1 As Long Dim LastRow2 As Long Dim LastRow3 As Long Dim cValue As Double Sheets("Master").Activate LastRow = Cells(Rows.Count, 11).End(xlUp).Row LastRow1 = Sheets("1").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow2 = Sheets("2").Cells(Rows.Count, 11).End(xlUp).Row + 1 LastRow3 = Sheets("3").Cells(Rows.Count, 11).End(xlUp).Row + 1 For Each cell In Range(Cells(3, 11), Cells(LastRow, 11)) If cell.Value < "" Then cValue = cell.Value Select Case cValue Case Is < 5 / 24 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("1").Cells(LastRow1, 1) LastRow1 = LastRow1 + 1 GoTo GetOut Case Is <= 1 Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("2").Cells(LastRow2, 1) LastRow2 = LastRow2 + 1 GoTo GetOut Case Else Range(Cells(cell.Row, 1), Cells(cell.Row, 11)).Copy Destination:= _ Sheets("3").Cells(LastRow3, 1) LastRow3 = LastRow3 + 1 GetOut: End Select End If Next cell End Sub If you want to remove the data as well then clear the constants after the copying has been done. -- HTH Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Don Doan" wrote in message ... Hi, actually before column K, there are 2 other columns with the format yyyy/mm/dd hh:mm:ss... column K is the result of one column minus the other column "Sandy Mann" wrote: With that format how are you managing to enter zero days without it decoming text? -- Regards, Sandy In Perth, the ancient capital of Scotland and the crowning place of kings Replace @mailinator.com with @tiscali.co.uk "Don Doan" wrote in message ... Hi, I have 4 spreadsheets in my workbook called "master","1", "2", and "3". In column K of the "master" sheet, the cell format is 'dd h:mm:ss'. How do i write a macro that would go through that column....if it's less than 5 hrs then move the row (from colum A to K) to sheet "1"...if it's between 5hrs and 24 hrs then move that row to sheet "2" and if it's more than 24 hrs then move to sheet "3"... the macro will keep on doing that until there is a blank line... thanks. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Moving selection right one column at a time: Is there a shortcut? | New Users to Excel | |||
Selection.sort Run-time error '1004' | Excel Programming | |||
Adding time selection to pop-up calendar | Excel Programming | |||
Moving Range Selection Right One Column at a Time | Excel Programming | |||
Moving Selection and Time based run macro | Excel Programming |