0

我有一个 Excel 表格,它可以工作并将数据保存到一个工作表中。我喜欢能够放置一个单选按钮,并基于该选择,我喜欢将其保存到不同的工作表中。示例:如果选择单选按钮 1 保存到工作表 1 如果选择单选按钮 2 保存到工作表 2 如果选择单选按钮 3 保存到工作表 3

用于保存在不同工作表上的相同表格。

我正在使用的代码。

Private Sub btn_append_Click()

Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Assessment")

'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'check for a part number
If Trim(Me.txt_roomNumber.Value) = "" Then
  Me.txt_roomNumber.SetFocus
  MsgBox "Please enter a Room Number"
  Exit Sub
End If

'copy the data to the database
'use protect and unprotect lines,
'     with your password
'     if worksheet is protected
With ws
'  .Unprotect Password:="password"
  .Cells(iRow, 1).Value = Me.txt_roomNumber.Value
  .Cells(iRow, 2).Value = Me.txt_roomName.Value
  .Cells(iRow, 3).Value = Me.txt_department.Value
  .Cells(iRow, 4).Value = Me.txt_contact.Value
  .Cells(iRow, 5).Value = Me.txt_altContact.Value
  .Cells(iRow, 6).Value = Me.cbx_devices.Value
  .Cells(iRow, 7).Value = Me.cbx_wallWow.Value
  .Cells(iRow, 8).Value = Me.txt_existingHostName.Value
  .Cells(iRow, 10).Value = Me.cbx_relocate.Value
  If Me.ckb_powerbar.Value = True Then Cells(iRow, 11).Value = "Y"
  If Me.ckb_dataJackPull.Value = True Then Cells(iRow, 12).Value = "P"
  .Cells(iRow, 13).Value = Me.txt_existingDataJack.Value
  If Me.ckb_hydroPull.Value = True Then Cells(iRow, 14).Value = "P" Else Cells(iRow, 14).Value = "A"
  .Cells(iRow, 19).Value = Me.txt_cablePullDesc.Value
  .Cells(iRow, 20).Value = Me.txt_hydroPullDesc.Value
  .Cells(iRow, 21).Value = Me.Txt_otherDesc.Value
'  .Protect Password:="password"
End With

'clear the data
'Me.txt_roomNumber.Value = ""
'Me.txt_roomName.Value = ""
'Me.txt_department.Value = ""
'Me.txt_contact.Value = ""
'Me.txt_altContact.Value = ""
Me.cbx_relocate.Value = ""
Me.txt_existingHostName = ""
Me.txt_altContact.SetFocus

End Sub

Private Sub btn_close_Click()
 Unload Me
End Sub

Private Sub ComboBox1_Change()

End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
 If CloseMode = vbFormControlMenu Then
    Cancel = True
    MsgBox "Please use the Close Form button!"
  End If
End Sub
4

1 回答 1

0

首先要做的是将单选按钮(实际上OptionButton)添加到表单并命名它们。

然后检查是否选择了其中之一,使用代码,例如:

If optSheet1 Then
    'it is checked
ElseIf optSheet2 Then
    'the second one is checked
'etc
End If
于 2013-07-03T18:07:28.817 回答