2016-12-16 7 views
1

シート内のセル、つまりデータが転送され、既にコンテンツがある場合はエラーまたはプロンプトが表示されるコードが必要です。現時点では、私が使用しているコードはプロンプトを表示しませんが、データを再度転送する場合はセルを更新しません。VBAプログラミング。シート内のフィールドに既にコンテンツがある場合、エラーまたはプロンプトが表示されます

Private Sub CancelButton_Click() 
Unload Me 
End Sub 

Private Sub ClearButton_Click() 
Call UserForm_Initialize 
End Sub 

Private Sub OKButton_Click() 
Dim emptyRow As Long 

'Make Sheet1 active 
Sheet1.Activate 

'Determine emptyRow 
Dim rFound As Range: Set rFound = Range("B:B").Find(BarcodeTextBox.Value, , , xlWhole) 
If rFound Is Nothing Then 
    emptyRow = Range("B" & Rows.Count).End(xlUp).Row + 1 
Else 
    emptyRow = rFound.Row 
End If 

'Transfer information 

If TimeOptionButton1.Value = True Then 
    Cells(emptyRow, 5).Value = "Yes" 
End If 
If TimeOptionButton2.Value = True Then 
    Cells(emptyRow, 7).Value = "Yes" 
End If 
If BreakOptionButton1.Value = True Then 
    Cells(emptyRow, 9).Value = "Yes" 
End If 
If BreakOptionButton2.Value = True Then 
    Cells(emptyRow, 11).Value = "Yes" 
End If 
If BreakOptionButton3.Value = True Then 
    Cells(emptyRow, 14).Value = "Yes" 
End If 
If BreakOptionButton4.Value = True Then 
    Cells(emptyRow, 16).Value = "Yes" 
End If 

Cells(emptyRow, 2).Value = BarcodeTextBox.Value 

Set ws = ActiveSheet 
Me.TextBox1 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$T$2:$E$977"), "IN") 
Me.TextBox2 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$U$2:$U$977"), "LF") 
Me.TextBox3 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$U$2:$U$977"), "READYMAN") 
Me.TextBox4 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK") 
Me.TextBox5 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$U$2:$U$977"), "VISITOR") 
End Sub 

Private Sub UserForm_Initialize() 
'Set Time In as default 
TimeOptionButton1.Value = True 

'Empty BarcodeTextBox 
BarcodeTextBox.Value = "" 

Set ws = ActiveSheet 
Me.TextBox1 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$T$2:$E$977"), "IN") 
Me.TextBox2 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$U$2:$U$977"), "LF") 
Me.TextBox3 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$U$2:$U$977"), "READYMAN") 
Me.TextBox4 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK") 
Me.TextBox5 = Application.WorksheetFunction. _ 
CountIf(ws.Range("$U$2:$U$977"), "VISITOR") 
End Sub 

ありがとうございます!あなたのコードについては

Output Sheet

答えて

0

Time Stamp Userformは、私はあなたがこのような何かを追加することだと思います。各はいに関して

If TimeOptionButton1.Value = True Then 
    if len(cells(emptyRow,5)) = 0 then 
     MsgBox "Write Error Message here" 
    else 
     Cells(emptyRow, 5).Value = "Yes"   
    end if 
End If 

。反復を避けるために、後で別の機能/サブを構築することを検討することもできます。

+0

ご意見ありがとうございます:)しかし、コードはコンパイルされません。 TimeOptionButton1.Value = Trueの場合 [len(cells(emptyRow、5)= 0の場合) - この部分に問題があります。これらのテキストは、エンコード後に赤色になります。 MsgBox「ここにエラーメッセージを書き込む」 else 細胞(emptyRow、5).Valueの=「はい」 終了 END IF は申し訳ありませんが、私は本当に何の専門家でない場合。 –

+1

私はそれを得た!ちょうど行方不明閉じ括弧があります。どうもありがとう! –

+0

カッコに追加答え: – Vityata

関連する問題