はVBA

2017-09-25 3 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:=。セル(1)、Lookat: = xlPart、LookIn:= xlFormulas、_ SearchOrder:= xlByColumns、SearchDirection:= xlNext、MatchCase:= False) –

+0

@yashikavaish編集コードを試してみよう –

+0

ちょっと、うまくいきます。 ? –

0

Cells(5, Columns.Count).End(xlToLeft)Cells(Columns.Count, 5)はあなたも検索したい場合は、この

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 

を試してみてください

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

を取得するために交換してください0の代わりに空白に等しい式を持つ非空白セル使用LookIn:=xlFormulas