VBA script doesn't work as expected

Bonbi456

New Member
Joined
Feb 8, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi people! The script that I've written reads the info on a column and if the value is zero, it highlights the value in red and copies the whole row to a new worksheet. The script works, but for some reason, it is also copying many other rows that do not fit the criteria. Anyone have a clue why it does that?
Any help is appreciated, here's the code:

VBA Code:
Sub CheckForZeros()

Dim ws As Worksheet
Dim lastRow As Long
Dim dRange As Range
Dim z0Ws As Worksheet
Dim z0Errors As Range
Dim i As Long

Set ws = ThisWorkbook.Sheets("Upper Beaver_collar") 

lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row 

Set dRange = ws.Range("D2:D" & lastRow) 

Set z0Ws = ThisWorkbook.Sheets.Add 
z0Ws.Name = "Z_0" 

Set z0Errors = z0Ws.Range("A1")

For i = 1 To lastRow - 1
    If dRange(i + 1, 1) = 0 Then 
        dRange(i + 1, 1).Interior.Color = RGB(255, 0, 0) 
        Dim errorRow As Range
        Set errorRow = ws.Rows(i + 1)
        errorRow.Copy z0Errors
        Set z0Errors = z0Errors.Offset(1, 0)
    End If
Next i

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Have you tried changing this line:
VBA Code:
If dRange(i + 1, 1) = 0 Then
to this:
VBA Code:
If dRange(i + 1, 1).Text = 0 Then
 
Upvote 0
Have you tried changing this line:
VBA Code:
If dRange(i + 1, 1) = 0 Then
to this:
VBA Code:
If dRange(i + 1, 1).Text = 0 Then
Just tried it, it didn't change anything though.

Thanks anyways:)
 
Upvote 0
Sounds like it might be a data issue.
Can you show us a sample of your data, including some of the rows that aren't working properly?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Sounds like it might be a data issue.
Can you show us a sample of your data, including some of the rows that aren't working properly?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
Sorry but I don't think I can share this data sadly. However I did solve my problem by working around it. Thank you for taking a look!
 
Upvote 0
Sorry but I don't think I can share this data sadly. However I did solve my problem by working around it. Thank you for taking a look!
We usually tell people if they have sensitive data, to "dummy it up" first before sharing it.
Out of curiousity, how did you fix it?
 
Upvote 0
We usually tell people if they have sensitive data, to "dummy it up" first before sharing it.
Out of curiousity, how did you fix it?
I got the script to simply color the cells that have a value of zero, then it marks every row that has color (because there's more than just the z=0 thing) with an x. Then every row that has an x is copied to an already existing worksheet instead of creating one. In retrospect some of my problems may have been because it was a csv file instead of an xlsm one. But hey, it works.
 
Upvote 0
But hey, it works.
Good to hear you got the solution.
If you would like to post the solution then it is perfectly fine to mark your post as the solution. Otherwise, please do not mark a post that doesn't contain a solution. You explained the solution for the original question that actually contains a macro, so the answer with the explained code would be helpful for future readers.
 
Upvote 0

Forum statistics

Threads
1,223,950
Messages
6,175,582
Members
452,653
Latest member
craigje92

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