Loop w/ Rows().Delete not working...

MistakesWereMade

Board Regular
Joined
May 22, 2019
Messages
103
I have a 'for loop' that is supposed to compare a cell in the master workbook with a set value, and then delete a row in the new workbook based on whether the condition is met or not. Currently, my loop runs and every row in the new workbook is deleted...

m is equal to 3 and the initial condition of Cell(n, 15).Value is 2.

If I set the if loops to "... <> m -1", the loop works fine for some reason and deletes every row that doesn't contain 2 in the new workbook. BUT, I need it to work with "...<> m" because m is a changing vector.

VBA Code:
For n = ThisWb.Sheets(1).Cells(Rows.Count, 12).End(xlUp).Row To 2 Step -1
        
    If ThisWb.Sheets(1).Cells(n, 15).Value <> m Then
        wbTemp.Sheets(1).Rows(n).Delete
    End If
            
Next n
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Seems odd that you check a row in the master book & then delete a row in another book. :unsure:
That said, do you have any cells in col O of the Master that are exactly 3?
 
Upvote 0
Yes, only 2s and 3s. And they are DEFINITELY exist.

I'm making a program to filter through thousands of data points and then store in a new workbook.

I just changed my code as shown below... It proves my m is equal to 3 since EVERY row in the new workbook was deleted. So I'm assuming my if statement condition is throwing me off somehow... :(

VBA Code:
For n = ThisWb.Sheets(1).Cells(Rows.Count, 12).End(xlUp).Row To 2 Step -1
    If m = 3 Then
        wbTemp.Sheets(1).Rows(n).Delete
    End If           
Next n
 
Upvote 0
That doesn't explain why you check col O in the master & then delete rows from another sheet, rather than checking the rows you are deleting.
Are the values in col O text or numbers?
Try filtering the Master for 3 in col O, are any rows visible?
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,773
Members
449,336
Latest member
p17tootie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top