2017-09-25 41 views
1
Set rngFound = Rows(5).Find("*", Cells(Columns.Count, 5), xlFormulas, , xlByColumns, xlNext) 

上面的代碼給出了類型不匹配錯誤13.如何檢索任何動態行的第一個非空白單元格和值在VBA

請指教一下我做錯了,或提出任何其他方法也是這樣做的。

+0

你能分享你的其他相關代碼嗎?如何定義'rngFound',哪個工作表是你想要使用'Find'等等 –

+0

rngfound被定義爲varriant或range,如果我用列替換行然後同一行代碼工作,但我無法找到一行的非空值。 –

回答

1

嘗試下面的代碼,解釋代碼的註釋裏:

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

嘿,它的工作原理謝謝你能告訴我在上面的代碼中有什麼問題? –

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

相關問題