答案 0 :(得分:0)
您可以使用循环
排除打印空单元格Dim i As Integer
Sub arrayTest()
Dim rng As Range
Set rng = Range(Cells(1, 1), Cells(4, 2))
For i = 0 To 4
If Not IsEmpty(rng(i)) Then
Debug.Print rng(i)
End If
Next
End Sub
答案 1 :(得分:0)
在适当的位置,我不认为可以按照你想要的方式读取Excel(除非你使用像Matt演示的循环在内存中构建一个数组)。如果我理解正确,那么你想要输出1,2,2,4。
使用临时空列获取此类结果的示例代码:
Sub ArrayTest2()
Const tmpCol As Long = 2 '\\ Change this to suit. This will be your temporary column
Dim rngIn As Range, rngOut As Range
Set rngIn = Range(Cells(1, 1), Cells(4, 1))
rngIn.Copy Cells(1, tmpCol)
Set rngOut = Cells(1, tmpCol).Resize(rngIn.Count, 1)
With rngOut
.UnMerge
If .SpecialCells(xlCellTypeBlanks).Count > 0 Then .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
Debug.Print rngOut(1) & "," & rngOut(2) & "," & rngOut(3) & "," & rngOut(4)
Cells(1, tmpCol).EntireColumn.Delete
End Sub
答案 2 :(得分:0)
我自己解决了这个问题。这适用于我希望它如何工作:
Sub arrayTest2()
Dim LastRow As Long
With Worksheets(1)
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
Dim rng As Range
Set rng = Range("A1:A" & LastRow)
Dim cellA As Range
Dim arrayA() As Variant
ReDim arrayA(LastRow)
For Each cellA In rng
arrayA(cellA.Row) = cellA.MergeArea(Cells(1, 1).Value)
Debug.Print arrayA(cellA.Row)
Next cellA
End Sub