获取数组中的一个范围的非emptry单元格

我正在尝试获取一个范围内的所有非空单元格。

'get keywords - only non-empty values Dim sheetDataSource As String Dim LR As Long Dim keywords As Variant Dim strUrl As String ' Set sheetDataSource = "DataSource" LR = Range("A" & Rows.Count).End(xlUp).Row Sheets("Settings").Range("A2:A" & LR).SpecialCells(xlCellTypeConstants, 23).Select 

目前,我没有得到任何回报。 另外,我想把它们放到一个数组中,用for循环来运行它们。

任何build议我做错了什么?

感谢您的回复!

我会build议使用Intersect获取所有非空单元格:

 With Sheets("Settings").Range("A2:A" & LR) Application.Intersect(.SpecialCells(xlCellTypeConstants), .SpecialCells(xlCellTypeVisible)).Select End With 

从这里你可以遍历这个范围,而不是存储为一个数组(除非有一个特定的原因,你需要它是一个数组)。 虽然我也build议不要使用.Select方法,但相反,设置所需的范围等于一个variables。

 Dim NonEmpties As Range With Sheets("Settings").Range("A2:A" & LR) Set NonEmpties = Application.Intersect(.SpecialCells(xlCellTypeConstants), .SpecialCells(xlCellTypeVisible)) End With 

我喜欢丹的回答,但为了以防万一你想做一些范围的实验:

 selectedRange = Application.Selection For Each cell In selectedRange If cell <> "" Then Debug.Print cell Next 

突出显示工作簿中的范围,然后翻转到VB编辑器并运行代码。 它应该打印除了空白单元格之外的所有选定区域内的所有内容。