xlDown不select空白单元格

我有一个问题,特定的列在其中间空白某些行。 这是领先的代码:

Range(FieldName.Offset(1),FieldName.End(xlDown))。Select

为了不select所有的单元格,因为它在空白单元处停止,并在空白处填充单元格的XYZ。

我知道,xlup将解决这个问题,但是,如果该字段的最后一个单元格是空白的,那么它将不会更改该单元格,并转到下一个填充的单元格。 我不知道如何修改我的代码,以便它利用xlup,并避免如果列中的底部单元格为空。 我有一个名为“ABC”的列将始终填充所有的行,我可以ping通,以便作为筛选的数据的最后一行调出它,但我不知道如何做到这一点。

我的代码

Sub SelectDown() Dim FieldName As Range Dim rng As Range, res As Variant, lrow As Long Set rng = ActiveSheet.AutoFilter.Range.Rows(1) res = Application.Match("Errors", rng, 0) 'Finds the Specific Error' rng.AutoFilter Field:=res, Criteria1:="*-SHOULD BE XYZ*" 'Only Shows rows that have something that matches the filter criteria lrow = ActiveSheet.Cells(Rows.Count, res).End(xlUp).Row + 1 If ActiveSheet.Range(Cells(1, res), Cells(lrow, res)).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then Set FieldName = Range("A1:BZ1").Find("COLUMN NAME") 'If field isnt present shows message If FieldName Is Nothing Then MsgBox "Field Name was not found." End If 'Changes the Selection to XYZ if there is a change present Range(FieldName.Offset(1), FieldName.End(xlDown)).Select Selection.FormulaR1C1 = "XYZ" 'Changes the Color of the fields changed to Yellow With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 65535 End With Else End If End Sub 

你可以使用这个代码。
使用Set FieldName = Range("A1:BZ1").Find("COLUMN NAME")来find列号(提供它不是NOTHING)并提供它作为可选的列号。

 Public Function LastCell(wrkSht As Worksheet, Optional Col As Long = 0) As Range Dim lLastCol As Long, lLastRow As Long On Error Resume Next With wrkSht If Col = 0 Then lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row Else lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column lLastRow = .Columns(Col).Find("*", , , , xlByColumns, xlPrevious).Row End If If lLastCol = 0 Then lLastCol = 1 If lLastRow = 0 Then lLastRow = 1 Set LastCell = wrkSht.Cells(lLastRow, lLastCol) End With On Error GoTo 0 End Function