VBA - Extract data by matching value - error

JetSetDrive

New Member
Joined
Jul 26, 2019
Messages
14
Can anyone assist me with figuring out why excel is not pulling over the rows that match the search criteria? The message box is showing no values found even though there are matching values. Is it because the value is set to "0"?

Dim lastrow As Long
Dim c As Long
Dim s As Variant
Dim ans As String
ans = ActiveSheet.Name
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "No MinMax"
c = 12
s = "0"
lastrow = Sheets(ans).Cells(Rows.Count, c).End(xlUp).Row
With Sheets(ans).Cells(1, c).Resize(lastrow)
.AutoFilter 1, s
counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
If counter > 1 Then
.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("No MinMax").Rows(2)
Sheets(ans).Rows(1).Copy Sheets("No MinMax").Rows(1)
Else
MsgBox "No values found"
End If
.AutoFilter
End With
application.ScreenUpdating = True
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You've set ans to the old activesheet, so it's looking in whatever the activesheet was BEFORE you added the new sheet.
Try:
Code:
Dim lastrow As Long
Dim c As Long
Dim s As Variant
Dim ans As String

Sheets.Add(After:=Sheets(Sheets.Count)).Name = "No MinMax"
ans = ActiveSheet.Name
c = 12
s = "0"
lastrow = Sheets(ans).Cells(Rows.Count, c).End(xlUp).Row
With Sheets(ans).Cells(1, c).Resize(lastrow)
.AutoFilter 1, s
counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
If counter > 1 Then
.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("No MinMax").Rows(2)
Sheets(ans).Rows(1).Copy Sheets("No MinMax").Rows(1)
Else
MsgBox "No values found"
End If
.AutoFilter
End With
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Disregard previous post - looks like you DID want to search the previously active sheet...
 
Upvote 0
@JetSetDrive
You're code works for me, although you could simplify the copy/paste like
Code:
If counter > 1 Then
   .SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("No MinMax").Range("A1")
Else
   MsgBox "No values found"
End If
Make sure that you actually have values that are 0 rather than 0.0000001.
Also the cell format needs to show as 0 rather than 0.00
 
Upvote 0
Works for me, too (with a couple of "0"s in the previously activesheet, Column L).
Could it be that you wanted to count cells <> 0, rather than =0?
 
Last edited:
Upvote 0
Thank you everyone, it turns out the previous macro did not remove the filters. I corrected this and it resolved the error
 
Upvote 0
Good find!
Thanks for closing out your post.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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