VBA Coding Help - Incorporate Text Boxes based on results

surkdidat

Well-known Member
Joined
Oct 1, 2011
Messages
582
Office Version
  1. 365
Hi, yet again!

I have used the "Record Macro" function, to record some very basic VBA Coding.

What I would like, after the filter has run, is to display a text box based on two scenarios

1. If only "blanks" to display a text box, and say "There are no results found. Please press OK to reset the search." This will then remove the filter, and then return to cell A3

2. If there are any results, display a text box and say "There are x results" (x being the number of rows with a result).

As a secondary question, are there any good online (Free or very cheap) places to start learning how to physically code in VBA at all please?

Thanks in advance!

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.AutoFilter
    Selection.AutoFilter
  
    ActiveSheet.Range("$A$2:$E$3480").AutoFilter Field:=5, Criteria1:="<>"
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this:
VBA Code:
Sub Macro1()
  With Range("A2:E" & Range("A" & Rows.Count).End(3).Row)
    .AutoFilter Field:=5, Criteria1:="<>"
    If Range("A" & Rows.Count).End(3).Row = 2 Then
      MsgBox "There are no results found"
      ActiveSheet.ShowAllData
      Range("A3").Select
    Else
      MsgBox "There are " & .Columns(1).SpecialCells(xlCellTypeVisible).Count - 1 & " results "
    End If
  End With
End Sub

---
The best place to learn VBA is this forum. ;)
---
 
Last edited:
Upvote 0
Solution
Lovely - appreciate your help :)

Try this:
VBA Code:
Sub Macro1()
  With Range("A2:E" & Range("A" & Rows.Count).End(3).Row)
    .AutoFilter Field:=5, Criteria1:="<>"
    If Range("A" & Rows.Count).End(3).Row = 2 Then
      MsgBox "There are no results found"
      ActiveSheet.ShowAllData
      Range("A3").Select
    Else
      MsgBox "There are " & .Columns(1).SpecialCells(xlCellTypeVisible).Count - 1 & " results "
    End If
  End With
End Sub

---
The best place to learn VBA is this forum. ;)
---
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,717
Members
449,050
Latest member
MiguekHeka

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