VBA to put the first visible cell's value from a filter data into C2 automatically

HongRu

Board Regular
Joined
Nov 24, 2022
Messages
191
Office Version
  1. 2021
Platform
  1. Windows
Hello!
I want when I change my filter values of Data (A2:A29), I have the first visible cell's value showing in C2 automatically.
Until now, I have a helper cell E2 with formula =SUBTOTAL(3,A3:A29) in order to launch the following VBA code of sheet1.

VBA Code:
Private Sub Worksheet_Calculate()
    Range("C2") = Range("A3:A29").SpecialCells(xlCellTypeVisible).Cells(1)
End Sub

For example, if I filter Data (A2:A29) with values "005-2029-888" and "005-3228-185" only, I should have a result as "005-2029-888" in C2 cell automatically by VBA code. (as the 1st picture)
If I filter with values "005-2029-873" and "005-2029-888" only, it should be "005-2029-873" in C2 cell. (as the 2nd picture)

I think maybe have a better way to do the same thing without the helper cell.

Thanks.

Order_No_filter.xlsm
ABCDE
1ResultHelper Cell
2Order_No005-2027-44127
3005-2027-441
4005-2027-441
5005-2027-441
6005-2027-441
7005-2027-441
8005-2029-873
9005-2029-873
10005-2029-873
11005-2029-873
12005-2029-873
13005-2029-873
14005-2029-888
15005-2029-888
16005-2029-888
17005-2029-888
18005-2029-888
19005-3228-184
20005-3228-184
21005-3228-184
22005-3228-184
23005-3228-185
24005-3228-185
25005-3228-185
26005-3228-185
27005-3228-185
28005-3228-186
29005-3228-186
Sheet1
Cell Formulas
RangeFormula
E2E2=SUBTOTAL(3,A3:A29)
 

Attachments

  • 2024-09-22 165026.png
    2024-09-22 165026.png
    33.4 KB · Views: 1
  • 2024-09-22 165117.png
    2024-09-22 165117.png
    35.6 KB · Views: 1

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I still needed the Helper Cell to make the calculation automatic. I hid the Helper Cell column. This code is placed in the Sheet object.
VBA Code:
Sub Show_First()
Dim rng As Range, cell As Range
Set rng = Range("Table1") 'Change Table1 to the name of your Filtered range
For Each cell In rng
    If Not cell.Height = 0 Then
        Cells(2, 3).Value = cell.Value
        Exit Sub
    Else
        Cells(2, 3).Value = "NA"
    End If
Next cell
End Sub

Private Sub Worksheet_Calculate()
Me.Show_First
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,560
Messages
6,160,493
Members
451,653
Latest member
agata

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