2017-03-18 8 views
0

このコードを使用して、1つのシートで何かを検索し、別のシートに移動しようとしています。以下は私のコードです。私はこのエラーを取得しておいてください。Excelマクロの実行時エラー1004:アプリケーション定義のエラーまたはオブジェクト定義のエラー

 Run-time error '1004': 
     Application-defined or object-defined error 

コード:

Private Sub CommandButton1_Click() 
    Dim ws As Worksheet, myCounter 
    Dim erow, myValue As Long 

    For Each ws In Sheets 

    If ws.Range("C3").Value > 6 Then 

    myCounter = 1 
    ws.Select 
    ws.Range("C3").Select 

    myValue = ws.Range("C3").Value 

    Worksheets("Report").Select 

    erow = ActiveSheet.Cells(Rows.Count, 1).End(x1Up).Offset(1, 0).Row 

    ActiveSheet.Cells(erow, 1) = myValue 

    nextValue = MsgBox("Value found in " & ws.Name & Chr(10) & "Continue?",  vbInformation + vbYesvbNo, ws.Name & " C3 = " & ws.Range("C3").Value) 

    Select Case nextValue 
    Case Is = vbYes 
    Case Is = vbNo 
    Exit Sub 
    End Select 
    End If 
    Next ws 

    If myCounter = 0 Then 
     MsgBox "None of the sheets contains a " & Chr(10) & "value greater than 6 in cell C3 ", vbInformation, "Not Found" 
    End If 

End Sub 

は、なぜ私はこのエラーを取得していますか?

 **Run-time error '1004': 
     Application-defined or object-defined error** 
+0

いいタイトルを書いてください。 –

+1

エラーが発生したとき、どの行が強調表示されていますか? – tigeravatar

+0

コードにスペルミスがあります。 '.End(x1Up)'は '.End(xlUp)'でなければなりません。 –

答えて

1

あなたがxlUpを持っている必要がありx1Upを持っています。小文字のLの代わりに1に注意してください。また、MsgBoxオプションはvbYesvbNoではなくvbYesNoです。

Option Explicit 

Private Sub CommandButton1_Click() 
    Dim w As Long, bFound As Boolean, nextValue As Variant 
    Dim erow, myValue As Long 

    For w = 1 To Worksheets.Count 
     With Worksheets(w) 
      'don't look on the Report worksheet 
      If .Name <> Worksheets("Report").Name Then 
       'use isnumeric to avoid confusing text and numbers 
       If IsNumeric(.Range("C3").Value2) Then 
        If .Range("C3").Value2 > 6 Then 
         Worksheets("Report").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = .Range("C3").Value 
         nextValue = MsgBox("Value found in " & .Name & Chr(10) & "Continue?", vbInformation + vbYesNo, .Name & " C3 = " & .Range("C3").Value) 
         bFound = True 
         Select Case nextValue 
          Case vbYes 
           'do nothing; continue 
          Case vbNo 
           'just exit the worksheet loop 
           Exit For 
         End Select 
        End If 
       End If 
      End If 
     End With 
    Next w 

    If Not bFound Then 
     MsgBox "None of the sheets contains a " & Chr(10) & "value greater than 6 in cell C3 ", vbInformation, "Not Found" 
    End If 

End Sub 

上記は私のコードです。タイプミスを避けるためにOption Explicitを使用することに慣れてください。

関連する問題