Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add visible cells of a range to array

Tags:

range

excel

vba

I am trying to get the values of the visible cells of a range into an array.

My code makes the array carry the values until the first non visible cell then stops.

Public Function ListeMaschinen() As Variant

Dim Auswahl As Range

With Sheets("qry_TechnischesDatenblatt")
Set Auswahl = .Range(.Range("A2:B2"), .Range("A2:B2").End(xlDown)).SpecialCells(xlCellTypeVisible)
End With

ListeMaschinen = Auswahl

End Function

If I select the range it shows all the cells I want marked.

Auswahl.Select
like image 830
LeeONight Avatar asked Apr 21 '16 08:04

LeeONight


People also ask

How do you assign a range of cells to an array in VBA?

Steps to Add a Range into an Array in VBA First, you need to declare a dynamic array using the variant data type. Next, you need to declare one more variable to store the count of the cells from the range and use that counter for the loop as well. After that, assign the range where you have value to the array.

How do I link only visible cells in Excel?

Select the range of cells in your worksheet. 2. Press Alt+; (hold down the Alt key and then press the semicolon key). On a Mac the shortcut is Cmd+Shift+Z. Excel will exclude all the hidden data from your selection and then you can copy (Ctrl+C) and paste (Ctrl+V) only the visible cells.


1 Answers

Here I have added the range cells to an array.

Sub examp()
Dim rng As Range, cll As Range, i As Integer, a(100) As Variant
Set rng = Range(Range("A2:B2"), Range("A2:B2").End(xlDown)).SpecialCells(xlCellTypeVisible)
i = 0
For Each cll In rng
a(i) = cll.Value
i = i + 1
Next
End Sub
like image 57
Sumit Jain Avatar answered Sep 19 '22 11:09

Sumit Jain