如何检索任何动态行的第一个非空白单元格和值在VBA

问题描述:

Set rngFound = Rows(5).Find("*", Cells(Columns.Count, 5), xlFormulas, , xlByColumns, xlNext) 

上面的代码给出了类型不匹配错误13.如何检索任何动态行的第一个非空白单元格和值在VBA

请指教一下我做错了,或提出任何其他方法也是这样做的。

+0

你能分享你的其他相关代码吗?如何定义'rngFound',哪个工作表是你想要使用'Find'等等 –

+0

rngfound被定义为varriant或range,如果我用列替换行然后同一行代码工作,但我无法找到一行的非空值。 –

尝试下面的代码,解释代码的注释里:

Dim rngFound As Range 
Dim LastCol As Long 
Dim nonBlankCell As Variant 

With Worksheets("Sheet1") ' <-- modify to your sheet's name 
    ' -- get last column in a specific row by using the Find function -- 
    Set rngFound = .Rows(5).Find(What:="*", Lookat:=xlPart, LookIn:=xlFormulas, _ 
        SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) 

    If Not rngFound Is Nothing Then ' Find was successful 
     LastCol = rngFound.Column ' get last Column with data 
     nonBlankCell = rngFound.Value 
    End If 

End With 
+0

感谢您的帮助,但此代码仍显示类型不匹配错误13,问题是在行Set rngFound = .Rows(5).Find(What:=“*”,After:=。Cells(1),Lookat: = xlPart,LookIn:= xlFormulas,_ SearchOrder:= xlByColumns,SearchDirection:= xlNext,MatchCase:= False) –

+0

@yashikavaish尝试编辑代码 –

+0

嘿,它的工作原理谢谢你能告诉我在上面的代码中有什么问题? –

更换Cells(Columns.Count, 5)Cells(5, Columns.Count).End(xlToLeft)得到

Set rngFound = Rows(5).Find("*", Cells(5, Columns.Count).End(xlToLeft), xlFormulas, , xlByColumns, xlNext) 

试试这个

Sub Find_Cell() 
    Dim rngFound As Range 
    Set rngFound = ThisWorkbook.Sheets("Sheet1").Rows(5).Find(What:="*", LookIn:=xlValues) 

    If rngFound Is Nothing Then 
     MsgBox "All cells are blank." 
    Else 
     MsgBox "First Cell Address: " & rngFound.Address 
     MsgBox "First Cell Value: " & rngFound.Value 
    End If 
End Sub 

如果你也想找到非空白单元格的公式等于空白,而不是LookIn:=xlValues使用LookIn:=xlFormulas