2017-03-08 17 views
1

使用されているセル全体で「Example」という単語を検索しています。しかし、コードは私にランタイムエラー1004 "アプリケーション定義またはオブジェクト定義のエラー"を与えます。私はラインに何か問題があることを知っていますRange(Cells(1, 1), Cells(lastrow, lastcolumn)) それは何でしょうか?セルと範囲で変数を使用するExcel VBA

ありがとうございます。ここ はコードです:

Dim lastrow as Long 
Dim lastcolumn as Long 
Dim sclnr as Range 
dim aws as WorkSheet 

Set aws = Thisworkbook.Sheets("Sheet1") 

'Using UsedRange 
lastrow = aws.UsedRange.Rows(aws.UsedRange.Rows.Count).Row 
lastcolumn = aws.UsedRange.Columns(aws.UsedRange.Columns.Count).Column 
'UsedRange works fine no problem with finding last row and column 

Set sclnr = aws.Range(Cells(1, 1), Cells(lastrow, lastcolumn)).Find("Example") 
'the word Example exits in one of the cells 
+0

はあなたに感謝します。 – MertTheGreat

+0

ようこそ、SOコミュニティがどのように動作するかを確認するには、[ツアー](クリックしてください)を取ることをお待ちください:答えとupvotesを受け入れる。 – R3uK

+0

を参照してください。 .Cellsで定義されている場合は必要な範囲です。](http://stackoverflow.com/questions/36368220/is-the-in-range-necessary-when-defined-by-cells) – Jeeped

答えて

3

はこれを試してみてください。マクロが実行されているときにawsがアクティブなシートでない場合は、すべての範囲/セル参照を修飾する必要があります。はい、それは動作します

Dim lastrow as Long 
Dim lastcolumn as Long 
Dim sclnr as Range 
dim aws as WorkSheet 

Set aws = Thisworkbook.Sheets("Sheet1") 

'Using UsedRange 
lastrow = aws.UsedRange.Rows(aws.UsedRange.Rows.Count).Row 
lastcolumn = aws.UsedRange.Columns(aws.UsedRange.Columns.Count).Column 
'UsedRange works fine no problem with finding last row and column 

Set sclnr = aws.Range(aws.Cells(1, 1), aws.Cells(lastrow, lastcolumn)).Find("Example") 
+0

それはうまく動作します、私は十分にあなたに感謝することはできません。それは一日中私を盗んだ。 – MertTheGreat

+1

私の喜び。それは非常に一般的な問題です。 – SJR

+0

答えを受け入れる時間が必要です。 – ManishChristian

0
Sub test_MertTheGreat() 
Dim FirstAddress As String, LookForString As String 
LookForString = "Example" 

Dim LastRow As Long 
Dim LastColumn As Long 
Dim SclnR As Range 
Dim awS As Worksheet 

Set awS = ThisWorkbook.Sheets("Sheet1") 

'Using UsedRange 
With awS.UsedRange 
LastRow = .Rows(.Rows.Count).Row 
LastColumn = .Columns(.Columns.Count).Column 
End With 'awS.UsedRange 

Set SclnR = awS.Range(Cells(1, 1), Cells(LastRow, LastColumn)).Find("Example") 
'the word Example exits in one of the cells 

With awS.Range(awS.Cells(1, 1), awS.Cells(LastRow, LastColumn)) 
    .Cells(1, 1).Activate 
    'First, define properly the Find method 
    Set SclnR = .Find(What:=LookForString, _ 
       After:=ActiveCell, _ 
       LookIn:=xlValues, _ 
       LookAt:=xlWhole, _ 
       SearchOrder:=xlByColumns, _ 
       SearchDirection:=xlNext, _ 
       MatchCase:=False, _ 
       SearchFormat:=False) 

    'If there is a result, keep looking with FindNext method 
    If Not SclnR Is Nothing Then 
     FirstAddress = SclnR.Address 
     Do 
      '''---+++--- Your actions here ---+++--- 
      Set SclnR = .FindNext(SclnR) 
     'Look until you find again the first result 
     Loop While Not SclnR Is Nothing And SclnR.Address <> FirstAddress 
    End If 
End With 

End Sub 
関連する問題