例如,如果需要使用现有数据在电子表格中更改样本类型,则有一个列表框用于选择样本类型。我没有列表框的代码,但对属性进行了一些更改,以确定列表的来源,并将其保留为单选项。
有一个命令按钮,后面有代码,用于将用户输入的所有列表框和文本框值发送到活动工作表。
Private Sub cmdSendPIDdata_Click()
Cells.AutoFilter
'Advance to last row
Range("B1").End(xlDown).Offset(1, 0).Select
'Constants and user data
Range("B" & (ActiveCell.Row)).Value = "'" & SamplePointID
Range("J" & (ActiveCell.Row)).Value = "'PID MEASUREMENT"
Range("N" & (ActiveCell.Row)).Value = "'PPB"
Range("F" & (ActiveCell.Row)).Value = "'" & dtpSampleDate.Value
Range("G" & (ActiveCell.Row)).Value = "'" & txtTime.Value
Range("H" & (ActiveCell.Row)).Value = "'" & lbxSampleType.Value
Range("K" & (ActiveCell.Row)).Value = "'" & txtConcentration.Value
Range("AK" & (ActiveCell.Row)).Value = "'" & lbxSampleLocation.Value
Range("AN" & (ActiveCell.Row)).Value = "'" & lbxSampledBy.Value
Unload Me
frmPIDDataEntry.Show
结束子
如果在列表框中未选择任何内容,则会清除相应范围中的现有值。
您可以添加一个IF语句来检查列表框的值,如果您看到它是=none,则退出。这个问题有点含糊,所以我的回答也是。
Private Sub cmdSendPIDdata_Click()
' Add something like this.
If ListBox.value = vbnullstring then Exit Sub
Cells.AutoFilter
'Advance to last row
Range("B1").End(xlDown).Offset(1, 0).Select
'Constants and user data
Range("B" & (ActiveCell.Row)).Value = "'" & SamplePointID
Range("J" & (ActiveCell.Row)).Value = "'PID MEASUREMENT"
Range("N" & (ActiveCell.Row)).Value = "'PPB"
Range("F" & (ActiveCell.Row)).Value = "'" & dtpSampleDate.Value
Range("G" & (ActiveCell.Row)).Value = "'" & txtTime.Value
Range("H" & (ActiveCell.Row)).Value = "'" & lbxSampleType.Value
Range("K" & (ActiveCell.Row)).Value = "'" & txtConcentration.Value
Range("AK" & (ActiveCell.Row)).Value = "'" & lbxSampleLocation.Value
Range("AN" & (ActiveCell.Row)).Value = "'" & lbxSampledBy.Value
Unload Me
frmPIDDataEntry.Show
End Sub
也许在@twenger的帖子中添加一些简单的东西如下所示。您必须将其添加到每个具有ListBox条目信息的范围中,这对程序员来说可能有点庞大,但对我来说很有效。希望这能给你一些想法或让你朝着正确的方向前进。
myRow = ActiveCell.Row
result1 = ListBox1.Value
If result1 = vbnullstring Then
Range("B" & myRow).Value = SamplePointID
Else
Range("B" & myRow).Value = result1 & " " & SamplePointID
End If