【发布时间】:2022-12-02 02:48:57
【问题描述】:
Below is the code I have. I am looking to loop through the blank cells in the range to return the values. I want to then add these values to a list and print them.
Sub NDJList()
Dim List() As Variant
Dim Alert, Today As Date
Dim Days, Due As Integer
Dim rng, Cell As Range
Dim x As Long
On Error Resume Next
ActiveSheet.ShowAllData
'Determine the data to store
Set rng = Range(Range("C4"), Range("C" & Rows.Count).End(xlUp))
With rng
.AutoFilter 7, ""
Set blanks = .Offset(1, 0).SpecialCells(xlCellTypeVisible)
'Resize Array prior to loading data
ReDim List(blanks.Rows.Count)
'Loop through each cell in range and store value in Array
For Each Cell In blanks
Alert = Cell.Offset(0, 4)
Today = Format(Now(), "dd-mmm-yy")
Days = Alert - Today
'Due = Days * (-1)
List(x) = Array(Cell.Offset(0, 3).Value)
x = x + 1
Next Cell
'Print values to Immediate Window
For x = LBound(List) To UBound(List)
Debug.Print List(x)
Next x
End With
End Sub
All it does is return a blank value in the Immediate window
【问题讨论】:
-
I don't know that this solves the problem, but it might. Drop the
ARRAY()function from theList(x) = Array(Cell.Offset(0, 3).Value)line. You are wanting to retrieve a value from a cell and add it to theListarray. It doesn't make sense to wrap the value returned from the cell inArray()for this. -
FYI
Dim rng, Cell As Rangedeclaresrngas Variant - every variable needs a type, otherwise it's a Variant. -
.AutoFilter 7, ""- but the range is only one column? Suggest you removeOn Error Resume Nextand see whether you get any errors. -
I suspect that @TimWilliams suggestion to drop
ON ERROR RESUME NEXTmay be illuminating for that problem. There may be an index-out-of-bound error being thrown as you iterate the blank range and assign values to elements of the array. And that potential error may stem from yourredim, lack of initialization of yourxvariable (x=0before iterating would be appropriate), and the fact that arrays are zero-based. That's a bit of a guess though. -
Can you try explainingin wordswhat you try accomplishing? What do you expect from
.AutoFilter 7, ""filtering in column "C:C" range? Do you want filtering the whole sheet on its column "G:G"? Are the headers on the fourth row? Then, in a discontinuous rangeblanks.Rows.Countreturns only the rowsof the first range area. From which column do you want returning in the mentioned array? From "F:F" and "G:G"? And, as stated above,On Error Resume Nextonly not let you seeing the raised errors... If you do not know how usingActiveSheet.ShowAllData,On Error GoTo 0is nec after