![]() |
Clear Contants in a Range Based on a Value
I need to clear constants in a range based on the value of a cell.
The values are 'yes' or 'no' (it is restricted by validation) and stored in the first row (C1:AR1). If the value is 'no' all constants in the corresponding column should be cleared from row 9 onward. The number of rows is variable. A 'yes' value will not require any action. I am using Excel 2002. Thanks |
Clear Contants in a Range Based on a Value
If this isn't what you are looking for, let me know so I can revise it. Sub test1() Dim i As Integer i = 3 Do While Cells(1, i).Value = "Yes" i = i + 1 If Cells(1, i).Value = "No" Then Cells(1, i).Select Columns(i).Delete End If Loop End Sub -- dok112 ------------------------------------------------------------------------ dok112's Profile: http://www.excelforum.com/member.php...o&userid=10581 View this thread: http://www.excelforum.com/showthread...hreadid=532484 |
Clear Contants in a Range Based on a Value
dok112, thank you for the quick reponse. The solution seems to only work
for one column at a time when running the macro i.e., I had to run the macro once for each column to delete. Should a range of columns be specified? Also, I am trying to preserve the first 8 rows of data and all formulas in the column (no matter where they are). Rather than deleteing the whole column, can the SpecialCells function be used to only delete constants in the columns from row 9 down. My (ab)users are permitted to add rows and formulas if necessary. Thanks "dok112" wrote in message ... If this isn't what you are looking for, let me know so I can revise it. Sub test1() Dim i As Integer i = 3 Do While Cells(1, i).Value = "Yes" i = i + 1 If Cells(1, i).Value = "No" Then Cells(1, i).Select Columns(i).Delete End If Loop End Sub -- dok112 ------------------------------------------------------------------------ dok112's Profile: http://www.excelforum.com/member.php...o&userid=10581 View this thread: http://www.excelforum.com/showthread...hreadid=532484 |
Clear Contants in a Range Based on a Value
Sorry about the delay in not getting back to you. Here is what I came up with. Let me know if this is what you want it to do... Sub test() Dim i As Integer i = 3 Do While Cells(1, i).Value 0 If Cells(1, i).Value = "Yes" Then i = i + 1 Else: Dim cellA As Variant Dim cellB As Variant [cellA] = Cells(8, i).Address [cellB] = Cells(65536, i).Address Cells(1, i).Select Range("" & [cellA] & ":" & [cellB] & "").Delete Shift:=xlUp i = i + 1 End If Loop End Sub -- dok112 ------------------------------------------------------------------------ dok112's Profile: http://www.excelforum.com/member.php...o&userid=10581 View this thread: http://www.excelforum.com/showthread...hreadid=532484 |
Clear Contants in a Range Based on a Value
That worked great. Thank you. I tweaked it a bit to clear constants as I
had formulas dependant on the cleared cells: 'to clear data only not formulas Range("" & [cellA] & ":" & [cellB] & "").Select On Error Resume Next 'to handle no constants in range -- Selection.SpecialCells(xlConstants).ClearContents Regards, Jeff "dok112" wrote in message ... Sorry about the delay in not getting back to you. Here is what I came up with. Let me know if this is what you want it to do... Sub test() Dim i As Integer i = 3 Do While Cells(1, i).Value 0 If Cells(1, i).Value = "Yes" Then i = i + 1 Else: Dim cellA As Variant Dim cellB As Variant [cellA] = Cells(8, i).Address [cellB] = Cells(65536, i).Address Cells(1, i).Select Range("" & [cellA] & ":" & [cellB] & "").Delete Shift:=xlUp i = i + 1 End If Loop End Sub |
All times are GMT +1. The time now is 06:56 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com