2016-08-22 16 views
0

私はユーザーフォームを作成し、コマンドボタンの1つがデータを入力できる別のユーザーフォームを起動します。このデータは、ワークシートの表に追加され、ユーザーフォームはアンロードされ、ユーザーは元のユーザーフォームに戻されます。このエラーは、データがワークシートに入力されることを意味します。このユーザーフォームは完全に単独で動作しますが、最初のユーザーフォームから起動すると、エラーが発生します。"メソッド '値'オブジェクト '範囲'の値 'が失敗し、クラッシュをExcel

Private Sub CommandButton1_Click() 

    'check all fields are filled 
    Dim nextRow As Integer 
    Dim nextCell As String 

    If Len(Trim(ComboBox1.Value)) = 0 Then 
     MsgBox "All feilds must be filled" 
     Exit Sub 
    End If 

    If Len(Trim(TextBox1.Value)) = 0 Then 
     MsgBox "All feilds must be filled" 
     Exit Sub 
    End If 

    If Len(Trim(TextBox2.Value)) = 0 Then 
     MsgBox "All feilds must be filled" 
     Exit Sub 
    End If 

    'Check if supplier ID already exists 
    Dim FindString As String 
    Dim Rng As Range 

    FindString = TextBox1.Value 

     If Trim(FindString) <> "" Then 
      With Sheet4.Range("B:B") 
       Set Rng = .Find(What:=FindString, _ 
           After:=.Cells(.Cells.Count), _ 
           LookIn:=xlValues, _ 
           LookAt:=xlWhole, _ 
           SearchOrder:=xlByRows, _ 
           SearchDirection:=xlNext, _ 
           MatchCase:=False) 
       If Not Rng Is Nothing Then 
        Application.Goto Rng, False 
        MsgBox "Sorry Bro, " & FindString & " already exists!" 
        Exit Sub 
       Else 
        FindString = TextBox2 

         If Trim(FindString) <> "" Then 
          With Sheet4.Range("D:D") 
           Set Rng = .Find(What:=FindString, _ 
               After:=.Cells(.Cells.Count), _ 
               LookIn:=xlValues, _ 
               LookAt:=xlWhole, _ 
               SearchOrder:=xlByRows, _ 
               SearchDirection:=xlNext, _ 
               MatchCase:=False) 
           If Not Rng Is Nothing Then 
            Application.Goto Rng, False 
            MsgBox "Sorry Bro, the Ordering Details you entered:" & vbNewLine & _ 
              "'" & FindString & "'" & vbNewLine & _ 
              "Already exists in our Database!" & vbNewLine & _ 
              "U wanna check ur data?" 
            Exit Sub 
           End If 
          End With 
         End If 

       End If 
      End With 
     End If 



    'enter supplier ID into sheet 
    Sheet4.Activate 
    nextRow = ActiveSheet.Range("B2", Range("B2").End(xlDown)).Count 
    nextCell = Cells(nextRow + 2, 2).Activate 

    'this is where the error occurs 
    ActiveCell.Value = TextBox1.Value 
    ActiveCell.Offset(0, 1).Value = ComboBox1.Value 
    ActiveCell.Offset(0, 2).Value = TextBox2.Value 

    Sheet2.Activate 

    Unload Me 
    End Sub 

答えて

0

個人的に私が「アクティブ化」の使用を避けているので、なぜ機能しないのか分かりません。おそらくあなたはこれがうまくいけば試してみることができます:

'Previous code that worked fine 

    nextRow = ActiveSheet.Range("B2", Range("B2").End(xlDown)).Count 

    With ActiveSheet.Cells(nextRow + 2, 2) 
     .Value = TextBox1.Value 
     .Offset(0, 1).Value = ComboBox1.Value 
     .Offset(0, 2).Value = TextBox2.Value 
    End With 
    Sheet2.Activate 

    Unload Me 
    End Sub 

希望これは仕事です! (これは私の最初の答えですので、私はフィードバックに非常に気を配っています)

関連する問題