1

データが転送されるシート内のセルに既にコンテンツがある場合、ユーザーフォームにエラーまたはプロンプトが表示されるコードが必要でした。今のところ、私が使用しているコードにはプロンプトは表示されませんが、データがセルに再度転送される場合、セルを更新しないことに成功しています。

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

前もって感謝します!

タイムスタンプ ユーザーフォーム

出力シート

4

1 に答える 1

0

コードに関しては、次のようなものを追加できると思います。

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

はい。繰り返しを避けるために、後で別の関数/サブを構築することを検討してください。

于 2016-12-16T10:40:04.607 に答える