从AB3开始批量处理23行,查找每行前4个Double类型有效值的技术需求
Solution for Extracting First 4 Valid Double Values per Row
Here's a complete VBA implementation that does exactly what you need—processing 23 rows starting at AB3, capturing the first 4 valid Double values and their cell positions for each row:
Sub ExtractFirstFourDoubles() Dim ws As Worksheet Dim startRow As Long, endRow As Long Dim currentRow As Long, currentCol As Long Dim values As Collection Dim positions As Collection Dim lastCol As Long ' Set your target worksheet (change to your sheet name if needed) Set ws = ThisWorkbook.Sheets("YourSheetName") startRow = 3 ' Start at row 3 endRow = startRow + 23 - 1 ' 23 rows total (ends at row 25) ' Loop through each target row For currentRow = startRow To endRow ' Initialize new collections for each row to avoid data overlap Set values = New Collection Set positions = New Collection ' Get the last used column in the current row to optimize the loop lastCol = ws.Cells(currentRow, ws.Columns.Count).End(xlToLeft).Column ' Check columns starting from AB (column index 28) to last used column For currentCol = 28 To lastCol ' Stop checking once we have 4 valid values If values.Count >= 4 Then Exit For Dim cellVal As Variant cellVal = ws.Cells(currentRow, currentCol).Value ' Validate: not an error value, and data type is Double If Not IsError(cellVal) And VarType(cellVal) = vbDouble Then values.Add cellVal ' Store the value positions.Add ws.Cells(currentRow, currentCol).Address ' Store cell address ' Optional: Store as Range object instead of address: ' positions.Add ws.Cells(currentRow, currentCol) End If Next currentCol ' Convert collections to arrays (if you need array storage for later use) Dim valueArr() As Double Dim posArr() As String ' Change to Range if storing Range objects ReDim valueArr(1 To values.Count) ReDim posArr(1 To positions.Count) For i = 1 To values.Count valueArr(i) = values(i) posArr(i) = positions(i) Next i ' -------------------------- ' Replace this section with your own logic to use the arrays ' Example: Print results to Immediate Window (Ctrl+G to view) Debug.Print "Row " & currentRow & ":" If values.Count > 0 Then For i = 1 To values.Count Debug.Print " Value: " & valueArr(i) & " | Position: " & posArr(i) Next i Else Debug.Print " No valid Double values found" End If Debug.Print "------------------------" ' -------------------------- Next currentRow End Sub
Key Details:
- Row Range: Processes rows 3 to 25 (23 rows total starting at AB3).
- Column Check: Starts at column AB (index 28) and goes to the last used column in each row—this avoids unnecessary checks on empty columns.
- Validation: Uses
VarType(cellVal) = vbDoubleto ensure we only capture Double-type values, andNot IsError(cellVal)skips cells with error values like#N/Aor#VALUE!. - Dynamic Storage: Uses collections to build the list of valid values/positions dynamically, then converts to arrays if you need array-based storage for later processing.
- Early Exit: Stops checking columns in a row once we've found 4 valid values to save processing time.
Customization Tips:
- Change Worksheet: Replace
"YourSheetName"with the actual name of your worksheet. - Check Entire Row: If you want to check all columns in the row (not just from AB onwards), change
currentCol = 28tocurrentCol = 1. - Store Positions as Ranges: If you need to reference the cells directly later, modify the positions collection to store Range objects instead of addresses (see the commented line in the code).
内容的提问来源于stack exchange,提问作者ep2020
相关产品推荐
相关产品推荐

