When you create an array from a selection in excel it ignores the fact that some rows are hidden.
For example if you have data in columns A, B, and C and column B is hidden, the array still has 3 columns and 4 rows.
Is there a way to either
Some code to illustrate what I want dim v as variant Range("A2:C4").Select v = selection
the selection is now 3 columns wide by 4 rows deep. I just want the two columns in the selection which are not hidden.
The only two ways I can think of to do that are (1) drop something from the original selection (2) not select the hidden columns in the first place.
maybe I can select a non-contiguous range and then turn that into an array?
You can accomplish this by
Assume on Sheet2 you have data in A1:C4 with column B hidden.
The following code will result in V being a 2D array (1 to 4, 1 to 2) containing the data from only columns A and C.
Option Explicit
Sub arrUnHidden()
Dim R As Range
Dim V As Variant
Dim wsTemp As Worksheet
Set R = Worksheets("Sheet2").Range("A1", Cells(Rows.Count, "C").End(xlUp))
Worksheets.Add
ActiveSheet.Name = "Temp"
Set wsTemp = Worksheets("Temp")
R.SpecialCells(xlCellTypeVisible).Copy wsTemp.Cells(1, 1)
V = wsTemp.UsedRange
Application.DisplayAlerts = False 'suppress message when deleting a sheet
Worksheets("temp").Delete
Application.DisplayAlerts = True
End Sub
Several points:
0: Do you mean actual arrays or do you mean named ranges? Visual Basic has arrays, excel has ranges. For the purposes of this answer, I will assume that you mean named ranges, as you cannot delete items from array without resizing the array.
1: There are ways to delete rows and columns from excel, or move ranges to not include certain elements. They are too numerous to mention here, but ThisRange.Columns(1).Delete or .Offset are both options.
2: Instead of changing what the range contains, I would use a for each loop that takes into account the cell properties.
As the comment suggested, more information would enable a greator tailoring of this answer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.