2014-10-02 123 views
0

我正试图推出一份问卷调查问卷。用户可以回答单选按钮,从是,有的,可能和不可以。这份调查问卷包含在名为经理的工作表中。缩短我的代码

如果他们选择“是”或“否”,我想将问题复制到标有“是”和“否”的表格中的单独表格中,以选择“是”还是“否”。这张表被称为Follow Up。

我有200多个问题,所以你可以理解为什么我想缩短代码。

我的第二个问题是,如果他们将他们的想法从no改为yes,我想从no列中删除复制的问题。当他们改变主意时,问题就会被复制到两者中。

以下是我的代码已经有:

Private Sub OptionButton1_Click() 

If OptionButton1.Value = True Then 'This is yes 
    Worksheets("Manager").Range("B3").Copy _ 'Within B3 is the person's name 
    Destination:=Worksheets("Follow Up").Range("B3") 'In this B3 I need the person's name 
End If 

If OptionButton1.Value = True Then 
    Worksheets("Manager").Range("B9").Copy _ 'B9 holds the question 
    Destination:=Worksheets("Follow Up").Range("B6") 'B6 is where I want it to go 
End If 
End Sub 


Private Sub OptionButton4_Click() 

If OptionButton4.Value = True Then 'This is for no 
    Worksheets("Manager").Range("B3").Copy _ 
    Destination:=Worksheets("Follow Up").Range("B3") 
End If 

If OptionButton4.Value = True Then 
    Worksheets("Manager").Range("B9").Copy _ 
    Destination:=Worksheets("Follow Up").Range("C6") 
End If 

End Sub 
+1

如果此代码适合您,但您想要改进它,那么此代码可能更适合于[代码审阅](http://codereview.stackexchange.com/)。 – skrrgwasme 2014-10-02 14:05:07

+1

如果某人在某个问题上改变了主意,该怎么办?你的“否”程序需要撤销你的“是”,反之亦然。 – 2014-10-02 15:49:31

+0

这就是我困在蒂姆身上的那一点 – Biffy261 2014-10-03 16:52:38

回答

0

这里有一个方法,但它很难提供一个非常有用的答案不知道你的另一问题是如何构成的。

下面的代码仍然依赖于对给定问题的范围(B3,B6,C6等)进行硬编码:但是如果您的问题都是相同的结构,那么应该可以再添加一个参数到TheAnswer所以它知道它应该操作哪些单元格。

Private Sub OptionButton1_Click() 
    TheAnswer OptionButton1.Value = True 'Yes-->True 
End Sub 

Private Sub OptionButton4_Click() 
    TheAnswer Not OptionButton4.Value = True 'No --> False 
End Sub 

Private Sub TheAnswer(IsYes As Boolean) 

    Dim shtM As Worksheet, shtF As Worksheet 
    Set shtM = Worksheets("Manager") 
    Set shtF = Worksheets("Follow Up") 

    shtF.Range("B3").Value = shtM.Range("B3").Value 
    shtF.Range("B6:C6").ClearContents 
    shtF.Cells(6, IIf(IsYes, 2, 3)).Value = shtM.Range("B9").Value 

End Sub