.SpecialCells(xlCellTypeVisible) not working in function?

larsa

New Member
Joined
Nov 28, 2012
Messages
3
Hello,
I have a filtered area where I want to repeat/show the first visible cell in another cell above (so that I can always refer to this destination).

I've tried to make a function for doeing this, but it does not work... it only return the first cell disregarding it beeing visible or not. I've done checked the same in a procedure, and then it returns only the visible cell.

Here's my code:
Function nextVisibleCell(rng As Range) As Variant
' Application.Volatile
nextVisibleCell = Range(rng.Offset(1, 0), rng.End(xlDown)).SpecialCells(xlCellTypeVisible).Cells(1)
End Function

I want it to always run whenever the filter is changed.

any suggestions?
regards,
Lars A.

-------------------
This is how it looks like, I'd like to return/show val2 since col 1 is filtered with val2, but my function returns val1. There can be more than one row with val1, val2, val3 etc, but I like to return/show the first apperances.

[TABLE="width: 146"]
<tbody>[TR]
[TD]val2[/TD]
[TD][/TD]
[TD]val1[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]head1[/TD]
[TD][/TD]
[TD]head1[/TD]
[/TR]
[TR]
[TD]val2[/TD]
[TD][/TD]
[TD]val1[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]val2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]val3[/TD]
[/TR]
</tbody>[/TABLE]
 

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.
Hi,
You can't use specialcells in a udf I'm afraid - you'll have to loop through the cells and check if the row is visible.
 
Upvote 0
Thank you very much for the quick reply!

As you may understand I'm not a experienced VBA user, so:
Is there any way that I can know what is working where???

regards,
Lars A.
 
Upvote 0
You would need a loop like this:
Code:
Function nextVisibleCell(rng As Range) As Variant
   Dim rngCell                As Range
   Application.Volatile True
   nextVisibleCell = "No visible cells"
   For Each rngCell In Range(rng.Offset(1, 0), rng.End(xlDown))
      If rngCell.EntireRow.Hidden = False Then
         nextVisibleCell = rngCell.Value
         Exit For
      End If
   Next rngCell
End Function
 
Upvote 0

Forum statistics

Threads
1,226,312
Messages
6,190,218
Members
453,598
Latest member
Excelnewbieneedtolearn

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