Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Macro loop problem
Hello, I'm very new to writing macros with the Visual Basic editor, so I was
wondering if someone could help me figure out why nothing happens when I run the following macro: Sub RowDeletingMacro() x = 10 Do While Cells(x, 1).Value < "" If Cells(x, 1).Value = 0 Then Cells(x, 1).Rows(x).Select Selection.Delete Shift:=xlUp End If x = x + 1 Loop End Sub My goal with this macro is to delete all rows in my worksheet that have "0" as an entry in the first column, and to skip over all of the rows that do not have 0 as their first column entry. Any help would be greatly appreciated, thanks very much, Ben |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Macro loop problem
It looks right but it doesn't act right, because the "rows" property of a range refers to the rows within the range - not the rows in the worksheet. Since there is only one row in the range and you specify 10, it is referring to the single cell 9 rows below - cells(19, 1). To refer to the entire row of a worksheet, you need (surprise) the "EntireRow" property. Furthermore, Excel cannot keep track of the rows if they are deleted from the top down - you would think the following would work but it skips every other row... Sub RowDeletingMacro() Dim x As Long x = 10 Do While Cells(x, 1).Value < "" If Cells(x, 1).Value = 0 Then Cells(x, 1).EntireRow.Delete Shift:=xlUp End If x = x + 1 Loop End Sub '-- Row 10 gets deleted, row 11 becomes row ten and the code is looking at Row 11 - which was row 12 and so on. You have to find the last row (or specify one) and work up... For Rw = 99 to 2 Step -1 If Cells(Rw, 1).Value < "" Then If Cells(Rw, 1).Value = 0 Then Cells(Rw, 1).EntireRow.Delete End If End if Next -- Jim Cone San Francisco, USA http://www.realezsites.com/bus/primitivesoftware "Ben" wrote in message Hello, I'm very new to writing macros with the Visual Basic editor, so I was wondering if someone could help me figure out why nothing happens when I run the following macro: Sub RowDeletingMacro() x = 10 Do While Cells(x, 1).Value < "" If Cells(x, 1).Value = 0 Then Cells(x, 1).Rows(x).Select Selection.Delete Shift:=xlUp End If x = x + 1 Loop End Sub My goal with this macro is to delete all rows in my worksheet that have "0" as an entry in the first column, and to skip over all of the rows that do not have 0 as their first column entry. Any help would be greatly appreciated, thanks very much, Ben |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Macro loop problem
Try this
Sub RowDeletingMacro() Do Until ActiveCell.Value = "" If ActiveCell.Value = 0 Then ActiveCell.EntireRow.Delete Else ActiveCell.Offset(1,0).Activate End If Loop End Sub Start the activecell at the beginning of the column you want to check for 0. Or if you want to run this sub numerous times put in Range("YourStartingCell").Activate lmk "Ben" wrote: Hello, I'm very new to writing macros with the Visual Basic editor, so I was wondering if someone could help me figure out why nothing happens when I run the following macro: Sub RowDeletingMacro() x = 10 Do While Cells(x, 1).Value < "" If Cells(x, 1).Value = 0 Then Cells(x, 1).Rows(x).Select Selection.Delete Shift:=xlUp End If x = x + 1 Loop End Sub My goal with this macro is to delete all rows in my worksheet that have "0" as an entry in the first column, and to skip over all of the rows that do not have 0 as their first column entry. Any help would be greatly appreciated, thanks very much, Ben |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Macro loop problem
'Deletes all rows in the current worksheet in which the value of the 1st
column = 0 'NOTE: assumes that the 1st column is only numerical and has no blanks. Public Sub RowDeletingMacro() Dim curRow As Integer 'Which row we're examining curRow = 1 While Cells(curRow, 1) < "" 'While the current row is non-blank If Cells(curRow, 1) = 0 Then 'If this is a row that we wish to delete: Rows(curRow).Select 'Select the row Selection.Delete Shift:=x1Up 'Delete it, and shift all rows under it up Else 'If tthis is not a row that we wish to delete: curRow = curRow + 1 'Move on to a new row End If Wend End Sub "Ben" wrote: Hello, I'm very new to writing macros with the Visual Basic editor, so I was wondering if someone could help me figure out why nothing happens when I run the following macro: Sub RowDeletingMacro() x = 10 Do While Cells(x, 1).Value < "" If Cells(x, 1).Value = 0 Then Cells(x, 1).Rows(x).Select Selection.Delete Shift:=xlUp End If x = x + 1 Loop End Sub My goal with this macro is to delete all rows in my worksheet that have "0" as an entry in the first column, and to skip over all of the rows that do not have 0 as their first column entry. Any help would be greatly appreciated, thanks very much, Ben |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Macro loop problem
try this. Or, sort and delete (you can record a macro to do that too)
for i= cells(rows.count,1).end(xlup).row to 10 step-1 if cells(i,1)=0 then rows(i).delete next i -- Don Guillett SalesAid Software "Ben" wrote in message ... Hello, I'm very new to writing macros with the Visual Basic editor, so I was wondering if someone could help me figure out why nothing happens when I run the following macro: Sub RowDeletingMacro() x = 10 Do While Cells(x, 1).Value < "" If Cells(x, 1).Value = 0 Then Cells(x, 1).Rows(x).Select Selection.Delete Shift:=xlUp End If x = x + 1 Loop End Sub My goal with this macro is to delete all rows in my worksheet that have "0" as an entry in the first column, and to skip over all of the rows that do not have 0 as their first column entry. Any help would be greatly appreciated, thanks very much, Ben |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Loop Problem | Excel Programming | |||
Macro - Loop/automatization problem | Excel Programming | |||
For Loop Problem | Excel Programming | |||
Problem adding charts using Do-Loop Until loop | Excel Programming | |||
For Next loop problem | Excel Programming |