![]() |
Tidy VBA Code
Hi all, I am not very experienced in writing vba. I have got the following code, put together by recording macros mostly, and editing afterwards. Is there a simpler way to construct the below code. Range("Q2").Select Do While Not IsEmpty(ActiveCell.Offset(0, -1)) ActiveCell.FormulaR1C1 = _ "=IF(OR('Pallet'!RC[-1]<ShipmentDate_StartValue,'Pallet'!RC[-1]ShipmentDate_EndValue),""Del"",""Keep"")" ActiveCell.Offset(1, 0).Select Loop Range("Q2").Select Do While Not IsEmpty(ActiveCell.Offset(0, -1)) Do While ActiveCell = "Del" ActiveCell.Offset(0, -16).Range("A1:Q1").Select Selection.Delete Shift:=xlUp ActiveCell.Offset(0, 16).Select Loop ActiveCell.Offset(1, 0).Select Loop ActiveSheet.Range("Q:Q").ClearContents Data resides in column a to p formula above check the data, populates column q, and acts on it depending on value. Cannot delete the whole row as data is on further columns (unable to change this) Thanks in advance edul |
Tidy VBA Code
On Dec 6, 4:56*pm, vom wrote:
Hi all, I am not very experienced in writing vba. I have got the following code, put together by recording macros mostly, and editing afterwards. Is there a simpler way to construct the below code. * * * * Range("Q2").Select * * * * Do While Not IsEmpty(ActiveCell.Offset(0, -1)) * * * * ActiveCell.FormulaR1C1 = _ "=IF(OR('Pallet'!RC[-1]<ShipmentDate_StartValue,'Pallet'!RC[-1]ShipmentDat*e_EndValue),""Del"",""Keep"")" * * * * ActiveCell.Offset(1, 0).Select * * * * *Loop * * * * Range("Q2").Select * * Do While Not IsEmpty(ActiveCell.Offset(0, -1)) * * * * * * Do While ActiveCell = "Del" * * * * * * ActiveCell.Offset(0, -16).Range("A1:Q1").Select * * * * * * Selection.Delete Shift:=xlUp * * * * * * ActiveCell.Offset(0, 16).Select * * * * * * Loop * * ActiveCell.Offset(1, 0).Select * * Loop * * * * ActiveSheet.Range("Q:Q").ClearContents Data resides in column a to p formula above check the data, populates column q, and acts on it depending on value. Cannot delete the whole row as data is on further columns (unable to change this) Thanks in advance edul As coded, you are paying a very tiny penalty in code execution time. You don't need to Select in the loops. You can just loop over the ranges. |
Tidy VBA Code
I would not use the apply formula and then delete due to formula.
something like Sub foreachC() For Each c In Cells(2, "q").Resize(Cells(Rows.Count, "Q").End(xlUp).Row) ' not quite sure what you want here. If c.offset(,-1)<ShipmentDate_StartValue or _ c.offset(,-1)shipmentDate_EndValue) then rows(c.row).delete Next c End Sub '========== On Dec 6, 3:56*pm, vom wrote: Hi all, I am not very experienced in writing vba. I have got the following code, put together by recording macros mostly, and editing afterwards. Is there a simpler way to construct the below code. * * * * Range("Q2").Select * * * * Do While Not IsEmpty(ActiveCell.Offset(0, -1)) * * * * ActiveCell.FormulaR1C1 = _ "=IF(OR('Pallet'!RC[-1]<ShipmentDate_StartValue,'Pallet'!RC[-1]ShipmentDat e_EndValue),""Del"",""Keep"")" * * * * ActiveCell.Offset(1, 0).Select * * * * *Loop * * * * Range("Q2").Select * Do While Not IsEmpty(ActiveCell.Offset(0, -1)) * * * * * * Do While ActiveCell = "Del" * * * * * * ActiveCell.Offset(0, -16).Range("A1:Q1").Select * * * * * * Selection.Delete Shift:=xlUp * * * * * * ActiveCell.Offset(0, 16).Select * * * * * * Loop * * ActiveCell.Offset(1, 0).Select * * Loop * * * * ActiveSheet.Range("Q:Q").ClearContents Data resides in column a to p formula above check the data, populates column q, and acts on it depending on value. Cannot delete the whole row as data is on further columns (unable to change this) Thanks in advance edul |
Tidy VBA Code
Thanks Don,
' not quite sure what you want here. If c.offset(,-1)<ShipmentDate_StartValue or _ c.offset(,-1)shipmentDate_EndValue) then rows(c.row).delete Didn't explain very clearly here, the start date and end date are named values with date & time format. Column P has date & time showing, and the formula deletes the dates not inbetween these 2 values (deletes the whole table row A to P - shiftup) I got your formula working by adapting it to column P Sub foreachC() Sheets("Dollies - Shipment").Activate For Each c In Cells(2, "p").Resize(Cells(Rows.Count, "P").End(xlUp).Row) If c.Offset(, 0) < ShipmentDate_StartValue Or c.Offset(, 0) shipmentDate_EndValue Then Rows(c.Row).Delete Next c The problem is I can only delete columns A to Q, as there is data in R to T, and your formula deletes the whole row. Can this be changed On Wed, 7 Dec 2011 06:02:39 -0800 (PST), Don Guillett wrote: I would not use the apply formula and then delete due to formula. something like Sub foreachC() For Each c In Cells(2, "q").Resize(Cells(Rows.Count, "Q").End(xlUp).Row) ' not quite sure what you want here. If c.offset(,-1)<ShipmentDate_StartValue or _ c.offset(,-1)shipmentDate_EndValue) then rows(c.row).delete Next c End Sub '========== On Dec 6, 3:56*pm, vom wrote: Hi all, I am not very experienced in writing vba. I have got the following code, put together by recording macros mostly, and editing afterwards. Is there a simpler way to construct the below code. * * * * Range("Q2").Select * * * * Do While Not IsEmpty(ActiveCell.Offset(0, -1)) * * * * ActiveCell.FormulaR1C1 = _ "=IF(OR('Pallet'!RC[-1]<ShipmentDate_StartValue,'Pallet'!RC[-1]ShipmentDat e_EndValue),""Del"",""Keep"")" * * * * ActiveCell.Offset(1, 0).Select * * * * *Loop * * * * Range("Q2").Select * Do While Not IsEmpty(ActiveCell.Offset(0, -1)) * * * * * * Do While ActiveCell = "Del" * * * * * * ActiveCell.Offset(0, -16).Range("A1:Q1").Select * * * * * * Selection.Delete Shift:=xlUp * * * * * * ActiveCell.Offset(0, 16).Select * * * * * * Loop * * ActiveCell.Offset(1, 0).Select * * Loop * * * * ActiveSheet.Range("Q:Q").ClearContents Data resides in column a to p formula above check the data, populates column q, and acts on it depending on value. Cannot delete the whole row as data is on further columns (unable to change this) Thanks in advance edul |
Tidy VBA Code
On Dec 7, 12:44*pm, vom wrote:
Thanks Don, ' not quite sure what you want here. If c.offset(,-1)<ShipmentDate_StartValue or _ * c.offset(,-1)shipmentDate_EndValue) then rows(c.row).delete Didn't explain very clearly here, *the start date and end date are named values with date & time format. Column P has date & time showing, and the formula deletes the dates not inbetween these 2 values (deletes the whole table row A to P - shiftup) I got your formula working by adapting it to column P Sub foreachC() * * Sheets("Dollies - Shipment").Activate For Each c In Cells(2, "p").Resize(Cells(Rows.Count, "P").End(xlUp).Row) If c.Offset(, 0) < ShipmentDate_StartValue Or c.Offset(, 0) shipmentDate_EndValue Then Rows(c.Row).Delete Next c The problem is I can only delete columns A to Q, as there is data in R to T, and your formula deletes the whole row. *Can this be changed On Wed, 7 Dec 2011 06:02:39 -0800 (PST), Don Guillett wrote: I would not use the apply formula and then delete due to formula. something like Sub foreachC() For Each c In Cells(2, "q").Resize(Cells(Rows.Count, "Q").End(xlUp).Row) ' not quite sure what you want here. If c.offset(,-1)<ShipmentDate_StartValue or _ * c.offset(,-1)shipmentDate_EndValue) then rows(c.row).delete Next c End Sub '========== On Dec 6, 3:56 pm, vom wrote: Hi all, I am not very experienced in writing vba. I have got the following code, put together by recording macros mostly, and editing afterwards. Is there a simpler way to construct the below code. Range("Q2").Select Do While Not IsEmpty(ActiveCell.Offset(0, -1)) ActiveCell.FormulaR1C1 = _ "=IF(OR('Pallet'!RC[-1]<ShipmentDate_StartValue,'Pallet'!RC[-1]ShipmentDat e_EndValue),""Del"",""Keep"")" ActiveCell.Offset(1, 0).Select Loop Range("Q2").Select Do While Not IsEmpty(ActiveCell.Offset(0, -1)) Do While ActiveCell = "Del" ActiveCell.Offset(0, -16).Range("A1:Q1").Select Selection.Delete Shift:=xlUp ActiveCell.Offset(0, 16).Select Loop ActiveCell.Offset(1, 0).Select Loop ActiveSheet.Range("Q:Q").ClearContents Data resides in column a to p formula above check the data, populates column q, and acts on it depending on value. Cannot delete the whole row as data is on further columns (unable to change this) Thanks in advance edul *'Then Rows(c.Row).Delete Then range(c.row,"a"),cells(c.row,"q")).Delete |
Tidy VBA Code
On Dec 7, 1:19*pm, Don Guillett wrote:
On Dec 7, 12:44*pm, vom wrote: Thanks Don, ' not quite sure what you want here. If c.offset(,-1)<ShipmentDate_StartValue or _ * c.offset(,-1)shipmentDate_EndValue) then rows(c.row).delete Didn't explain very clearly here, *the start date and end date are named values with date & time format. Column P has date & time showing, and the formula deletes the dates not inbetween these 2 values (deletes the whole table row A to P - shiftup) I got your formula working by adapting it to column P Sub foreachC() * * Sheets("Dollies - Shipment").Activate For Each c In Cells(2, "p").Resize(Cells(Rows.Count, "P").End(xlUp).Row) If c.Offset(, 0) < ShipmentDate_StartValue Or c.Offset(, 0) shipmentDate_EndValue Then Rows(c.Row).Delete Next c The problem is I can only delete columns A to Q, as there is data in R to T, and your formula deletes the whole row. *Can this be changed On Wed, 7 Dec 2011 06:02:39 -0800 (PST), Don Guillett wrote: I would not use the apply formula and then delete due to formula. something like Sub foreachC() For Each c In Cells(2, "q").Resize(Cells(Rows.Count, "Q").End(xlUp).Row) ' not quite sure what you want here. If c.offset(,-1)<ShipmentDate_StartValue or _ * c.offset(,-1)shipmentDate_EndValue) then rows(c.row).delete Next c End Sub '========== On Dec 6, 3:56 pm, vom wrote: Hi all, I am not very experienced in writing vba. I have got the following code, put together by recording macros mostly, and editing afterwards. Is there a simpler way to construct the below code. Range("Q2").Select Do While Not IsEmpty(ActiveCell.Offset(0, -1)) ActiveCell.FormulaR1C1 = _ "=IF(OR('Pallet'!RC[-1]<ShipmentDate_StartValue,'Pallet'!RC[-1]ShipmentDat e_EndValue),""Del"",""Keep"")" ActiveCell.Offset(1, 0).Select Loop Range("Q2").Select Do While Not IsEmpty(ActiveCell.Offset(0, -1)) Do While ActiveCell = "Del" ActiveCell.Offset(0, -16).Range("A1:Q1").Select Selection.Delete Shift:=xlUp ActiveCell.Offset(0, 16).Select Loop ActiveCell.Offset(1, 0).Select Loop ActiveSheet.Range("Q:Q").ClearContents Data resides in column a to p formula above check the data, populates column q, and acts on it depending on value. Cannot delete the whole row as data is on further columns (unable to change this) Thanks in advance edul *'Then Rows(c.Row).Delete *Then range(c.row,"a"),cells(c.row,"q")).Delete oops. Use Then range(cells(c.row,"a"),cells(c.row,"q")).Delete |
Tidy VBA Code
On Wed, 7 Dec 2011 11:43:45 -0800 (PST), Don Guillett
wrote: On Dec 7, 1:19*pm, Don Guillett wrote: On Dec 7, 12:44*pm, vom wrote: Thanks Don, ' not quite sure what you want here. If c.offset(,-1)<ShipmentDate_StartValue or _ * c.offset(,-1)shipmentDate_EndValue) then rows(c.row).delete Didn't explain very clearly here, *the start date and end date are named values with date & time format. Column P has date & time showing, and the formula deletes the dates not inbetween these 2 values (deletes the whole table row A to P - shiftup) I got your formula working by adapting it to column P Sub foreachC() * * Sheets("Dollies - Shipment").Activate For Each c In Cells(2, "p").Resize(Cells(Rows.Count, "P").End(xlUp).Row) If c.Offset(, 0) < ShipmentDate_StartValue Or c.Offset(, 0) shipmentDate_EndValue Then Rows(c.Row).Delete Next c The problem is I can only delete columns A to Q, as there is data in R to T, and your formula deletes the whole row. *Can this be changed On Wed, 7 Dec 2011 06:02:39 -0800 (PST), Don Guillett wrote: I would not use the apply formula and then delete due to formula. something like Sub foreachC() For Each c In Cells(2, "q").Resize(Cells(Rows.Count, "Q").End(xlUp).Row) ' not quite sure what you want here. If c.offset(,-1)<ShipmentDate_StartValue or _ * c.offset(,-1)shipmentDate_EndValue) then rows(c.row).delete Next c End Sub '========== On Dec 6, 3:56 pm, vom wrote: Hi all, I am not very experienced in writing vba. I have got the following code, put together by recording macros mostly, and editing afterwards. Is there a simpler way to construct the below code. Range("Q2").Select Do While Not IsEmpty(ActiveCell.Offset(0, -1)) ActiveCell.FormulaR1C1 = _ "=IF(OR('Pallet'!RC[-1]<ShipmentDate_StartValue,'Pallet'!RC[-1]ShipmentDat e_EndValue),""Del"",""Keep"")" ActiveCell.Offset(1, 0).Select Loop Range("Q2").Select Do While Not IsEmpty(ActiveCell.Offset(0, -1)) Do While ActiveCell = "Del" ActiveCell.Offset(0, -16).Range("A1:Q1").Select Selection.Delete Shift:=xlUp ActiveCell.Offset(0, 16).Select Loop ActiveCell.Offset(1, 0).Select Loop ActiveSheet.Range("Q:Q").ClearContents Data resides in column a to p formula above check the data, populates column q, and acts on it depending on value. Cannot delete the whole row as data is on further columns (unable to change this) Thanks in advance edul *'Then Rows(c.Row).Delete *Then range(c.row,"a"),cells(c.row,"q")).Delete oops. Use Then range(cells(c.row,"a"),cells(c.row,"q")).Delete Thanks Don That deleted the columns ok. Am just trying to fit it in with using the endtime & starttime. It's not working as I expected. The formula doesn't stop when it reaches a valid date 'For Each c In Cells(2, "P").Resize(Cells(Rows.Count, "P").End(xlUp).Row) 'If c < ShipmentDate_StartValue Or c ShipmentDate_EndValue _ 'Then Range(Cells(c.Row, "a"), Cells(c.Row, "q")).Delete 'Next c ie. When c< shipmentDate_EndValue it still deletes the columns. I will try to sort it out myself, and shout on here if I require help. Thanks again. |
Tidy VBA Code
On Tue, 6 Dec 2011 16:27:29 -0800 (PST), James Ravenswood
wrote: On Dec 6, 4:56*pm, vom wrote: Hi all, I am not very experienced in writing vba. I have got the following code, put together by recording macros mostly, and editing afterwards. Is there a simpler way to construct the below code. * * * * Range("Q2").Select * * * * Do While Not IsEmpty(ActiveCell.Offset(0, -1)) * * * * ActiveCell.FormulaR1C1 = _ "=IF(OR('Pallet'!RC[-1]<ShipmentDate_StartValue,'Pallet'!RC[-1]ShipmentDat*e_EndValue),""Del"",""Keep"")" * * * * ActiveCell.Offset(1, 0).Select * * * * *Loop * * * * Range("Q2").Select * * Do While Not IsEmpty(ActiveCell.Offset(0, -1)) * * * * * * Do While ActiveCell = "Del" * * * * * * ActiveCell.Offset(0, -16).Range("A1:Q1").Select * * * * * * Selection.Delete Shift:=xlUp * * * * * * ActiveCell.Offset(0, 16).Select * * * * * * Loop * * ActiveCell.Offset(1, 0).Select * * Loop * * * * ActiveSheet.Range("Q:Q").ClearContents Data resides in column a to p formula above check the data, populates column q, and acts on it depending on value. Cannot delete the whole row as data is on further columns (unable to change this) Thanks in advance edul As coded, you are paying a very tiny penalty in code execution time. You don't need to Select in the loops. You can just loop over the ranges. Thanks for that James. I am running the live code as done, but am trying to learn vba a bit better now. |
All times are GMT +1. The time now is 09:48 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com