how to handle if value NOT found VBA

punnipah

Board Regular
Joined
Nov 3, 2021
Messages
134
Office Version
  1. 2019
Platform
  1. Windows
Hi...

i have to this VBA Code about Filter #N/A but i have a problem.When Data NOT found "#N/A" not working
-If Have "#N/A" Working
-If no Have "#N/A" not Working

i want to when no Have "#N/A" continue working


VBA Code:
Sub Add_Subtotals()
 
 LastRow = Range("O" & Rows.Count).End(xlUp).Row - 1
Range("A1:W" & LastRow).AutoFilter Field:=23, Criteria1:="#N/A"
ActiveSheet.Range("A2:W" & LastRow).SpecialCells(xlCellTypeVisible).Copy
 
Range("A" & LastRow + 3).PasteSpecial xlPasteValues
Application.CutCopyMode = False
'Remove AutoFilter
ActiveSheet.AutoFilterMode = False
End Sub





1683791847737.png
 
Last edited by a moderator:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Simple approach using Goto error trap

VBA Code:
Sub Add_Subtotals()

LastRow = Range("O" & Rows.Count).End(xlUp).Row - 1
On Error goto NoNA
Range("A1:W" & LastRow).AutoFilter Field:=23, Criteria1:="#N/A"
ActiveSheet.Range("A2:W" & LastRow).SpecialCells(xlCellTypeVisible).Copy
On Error GoTo 0
Range("A" & LastRow + 3).PasteSpecial xlPasteValues
NoNA:
Application.CutCopyMode = False
'Remove AutoFilter
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 1
Simple approach using Goto error trap

VBA Code:
Sub Add_Subtotals()

LastRow = Range("O" & Rows.Count).End(xlUp).Row - 1
On Error goto NoNA
Range("A1:W" & LastRow).AutoFilter Field:=23, Criteria1:="#N/A"
ActiveSheet.Range("A2:W" & LastRow).SpecialCells(xlCellTypeVisible).Copy
On Error GoTo 0
Range("A" & LastRow + 3).PasteSpecial xlPasteValues
NoNA:
Application.CutCopyMode = False
'Remove AutoFilter
ActiveSheet.AutoFilterMode = False
End Sub
Thank you Very Much ^^
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

Rather than using an error to decide (there could be multiple reasons for a code line to error), I would use a more positive approach

Rich (BB code):
Sub Add_Subtotals()
 
  lastrow = Range("O" & Rows.Count).End(xlUp).Row - 1
  Range("A1:W" & lastrow).AutoFilter Field:=23, Criteria1:="#N/A"
  If Range("A1:A" & lastrow).SpecialCells(xlCellTypeVisible).Count > 1 Then
    ActiveSheet.Range("A2:W" & lastrow).SpecialCells(xlCellTypeVisible).Copy
     
    Range("A" & lastrow + 3).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
  End If
  'Remove AutoFilter
  ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 1
Solution
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

Rather than using an error to decide (there could be multiple reasons for a code line to error), I would use a more positive approach

Rich (BB code):
Sub Add_Subtotals()
 
  lastrow = Range("O" & Rows.Count).End(xlUp).Row - 1
  Range("A1:W" & lastrow).AutoFilter Field:=23, Criteria1:="#N/A"
  If Range("A1:A" & lastrow).SpecialCells(xlCellTypeVisible).Count > 1 Then
    ActiveSheet.Range("A2:W" & lastrow).SpecialCells(xlCellTypeVisible).Copy
    
    Range("A" & lastrow + 3).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
  End If
  'Remove AutoFilter
  ActiveSheet.AutoFilterMode = False
End Sub
Thank You So Much
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
@Peter_SSs

I so sorry , i have 1 more question.

IF my Data NoHave N/A i want to Value : Sheets("ReportAll").Range("A14").Value = "Data Not Found"

How to try Working





Thank you for Advance.


Sub Add_Subtotals()

lastrow = Range("O" & Rows.Count).End(xlUp).Row - 1
Range("A1:W" & lastrow).AutoFilter Field:=23, Criteria1:="#N/A"
If Range("A1:A" & lastrow).SpecialCells(xlCellTypeVisible).Count > 1 Then
ActiveSheet.Range("A2:W" & lastrow).SpecialCells(xlCellTypeVisible).Copy

Range("A" & lastrow + 3).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End If
'Remove AutoFilter
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 0
Please post your code again, remembering my previous request:
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details.
.. explain again just what are wanting done now.
 
Upvote 0

Forum statistics

Threads
1,215,183
Messages
6,123,522
Members
449,103
Latest member
Michele317

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