VBA用户表单问题select错误的工作表

我想要实现的是创build一个用户窗体,我在Sheet1中调用,但从VBA用户窗体的结果应该去表2行而不打开它。 但是,我无法做到这一点,当我保存logging表2popup窗口。 任何想法如何解决它? 非常感谢。

Private Sub CommandButton1_Click() Dim emptyRow As Long 'Make Sheet2 active Sheets("Sheet2").Activate 'Determine emptyRow emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1 'Transfer information Cells(emptyRow, 1).Value = TextBox1.Value Cells(emptyRow, 2).Value = TextBox2.Value If OptionButton1.Value = True Then Cells(emptyRow, 3).Value = OptionButton1.Caption End If If OptionButton2.Value = True Then Cells(emptyRow, 3).Value = OptionButton2.Caption End If If OptionButton3.Value = True Then Cells(emptyRow, 3).Value = OptionButton3.Caption End If If CheckBox1.Value = True Then Cells(emptyRow, 4).Value = CheckBox1.Caption If CheckBox2.Value = True Then Cells(emptyRow, 4).Value = Cells(emptyRow, 4).Value & " " & CheckBox2.Caption If CheckBox3.Value = True Then Cells(emptyRow, 4).Value = Cells(emptyRow, 4).Value & " " & CheckBox3.Caption If CheckBox4.Value = True Then Cells(emptyRow, 4).Value = Cells(emptyRow, 4).Value & " " & CheckBox4.Caption If CheckBox5.Value = True Then Cells(emptyRow, 4).Value = Cells(emptyRow, 4).Value & " " & CheckBox5.Caption Cells(emptyRow, 4).Value = Cells(emptyRow, 4).Value & " " & TextBox3.Value If CheckBox6.Value = True Then Cells(emptyRow, 5).Value = CheckBox6.Caption If CheckBox7.Value = True Then Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & " " & CheckBox7.Caption If CheckBox8.Value = True Then Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & " " & CheckBox8.Caption 'ewwrewerwerwe If OptionButton7.Value = True Then Cells(emptyRow, 6).Value = OptionButton7.Caption End If If OptionButton8.Value = True Then Cells(emptyRow, 6).Value = OptionButton8.Caption End If If OptionButton9.Value = True Then Cells(emptyRow, 6).Value = OptionButton9.Caption End If 'werwewewewe If OptionButton10.Value = True Then Cells(emptyRow, 7).Value = OptionButton10.Caption End If If OptionButton11.Value = True Then Cells(emptyRow, 7).Value = OptionButton11.Caption End If If OptionButton12.Value = True Then Cells(emptyRow, 7).Value = OptionButton12.Caption End If 'ewrweewwerew If OptionButton13.Value = True Then Cells(emptyRow, 8).Value = OptionButton13.Caption End If If OptionButton14.Value = True Then Cells(emptyRow, 8).Value = OptionButton14.Caption End If If OptionButton15.Value = True Then Cells(emptyRow, 8).Value = OptionButton15.Caption End If If OptionButton16.Value = True Then Cells(emptyRow, 8).Value = OptionButton16.Caption End If If OptionButton17.Value = True Then Cells(emptyRow, 8).Value = OptionButton17.Caption End If 'werwerwerwe If OptionButton18.Value = True Then Cells(emptyRow, 9).Value = OptionButton18.Caption End If If OptionButton19.Value = True Then Cells(emptyRow, 9).Value = OptionButton19.Caption End If If OptionButton20.Value = True Then Cells(emptyRow, 9).Value = OptionButton20.Caption End If If OptionButton21.Value = True Then Cells(emptyRow, 9).Value = OptionButton21.Caption End If If OptionButton22.Value = True Then Cells(emptyRow, 9).Value = OptionButton22.Caption End If End Sub 

而不是激活工作表使用一个With语句,然后用.cells引用 – 而在与(例如在结束之前),您可以引用表单和正常表单function只有一个“。 首先例如.range(“A1”)。value将得到sheet2 A1的值

 Private Sub CommandButton1_Click() Dim emptyRow As Long 'Make Sheet2 active With Sheets("Sheet2") 'Determine emptyRow emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1 'Transfer information .Cells(emptyRow, 1).Value = TextBox1.Value .Cells(emptyRow, 2).Value = TextBox2.Value If OptionButton1.Value = True Then .Cells(emptyRow, 3).Value = OptionButton1.Caption End If If OptionButton2.Value = True Then .Cells(emptyRow, 3).Value = OptionButton2.Caption End If If OptionButton3.Value = True Then .Cells(emptyRow, 3).Value = OptionButton3.Caption End If If CheckBox1.Value = True Then .Cells(emptyRow, 4).Value = CheckBox1.Caption If CheckBox2.Value = True Then .Cells(emptyRow, 4).Value = .Cells(emptyRow, 4).Value & " " & CheckBox2.Caption If CheckBox3.Value = True Then .Cells(emptyRow, 4).Value = .Cells(emptyRow, 4).Value & " " & CheckBox3.Caption If CheckBox4.Value = True Then .Cells(emptyRow, 4).Value = .Cells(emptyRow, 4).Value & " " & CheckBox4.Caption If CheckBox5.Value = True Then .Cells(emptyRow, 4).Value = .Cells(emptyRow, 4).Value & " " & CheckBox5.Caption .Cells(emptyRow, 4).Value = .Cells(emptyRow, 4).Value & " " & TextBox3.Value If CheckBox6.Value = True Then .Cells(emptyRow, 5).Value = CheckBox6.Caption If CheckBox7.Value = True Then .Cells(emptyRow, 5).Value = .Cells(emptyRow, 5).Value & " " & CheckBox7.Caption If CheckBox8.Value = True Then .Cells(emptyRow, 5).Value = .Cells(emptyRow, 5).Value & " " & CheckBox8.Caption 'ewwrewerwerwe If OptionButton7.Value = True Then .Cells(emptyRow, 6).Value = OptionButton7.Caption End If If OptionButton8.Value = True Then .Cells(emptyRow, 6).Value = OptionButton8.Caption End If If OptionButton9.Value = True Then .Cells(emptyRow, 6).Value = OptionButton9.Caption End If 'werwewewewe If OptionButton10.Value = True Then .Cells(emptyRow, 7).Value = OptionButton10.Caption End If If OptionButton11.Value = True Then .Cells(emptyRow, 7).Value = OptionButton11.Caption End If If OptionButton12.Value = True Then .Cells(emptyRow, 7).Value = OptionButton12.Caption End If 'ewrweewwerew If OptionButton13.Value = True Then .Cells(emptyRow, 8).Value = OptionButton13.Caption End If If OptionButton14.Value = True Then .Cells(emptyRow, 8).Value = OptionButton14.Caption End If If OptionButton15.Value = True Then .Cells(emptyRow, 8).Value = OptionButton15.Caption End If If OptionButton16.Value = True Then .Cells(emptyRow, 8).Value = OptionButton16.Caption End If If OptionButton17.Value = True Then .Cells(emptyRow, 8).Value = OptionButton17.Caption End If 'werwerwerwe If OptionButton18.Value = True Then .Cells(emptyRow, 9).Value = OptionButton18.Caption End If If OptionButton19.Value = True Then .Cells(emptyRow, 9).Value = OptionButton19.Caption End If If OptionButton20.Value = True Then .Cells(emptyRow, 9).Value = OptionButton20.Caption End If If OptionButton21.Value = True Then .Cells(emptyRow, 9).Value = OptionButton21.Caption End If If OptionButton22.Value = True Then .Cells(emptyRow, 9).Value = OptionButton22.Caption End If End With End Sub 

另一个select是closures屏幕更新,然后select原始表单,然后再打开,但以上更高效