在VBA excel公式中单击并取消单击X框

我正在创build一个调查,希望对最终用户来说很简单。 我创build了一个按照李克特量表(Likert scale)操作的文档,其范围从不同意到6分(非答复字段)。 我在第3行到第152行上运行的问题和select在每行的单元格C:H中。 目前,我已经知道接受者可以在哪里点击一个单元格,并在框中产生一个X,表示他们的select。 我也拥有它,以便他们只能单击行中的一个选项,如果他们select另一个选项,它将删除第一个X并将它放置在他们单击的新单元格中。

这是我想要的。 现在,如果他们把一个X放在一个单元格中,然后点击同一个X,我的代码就会通过并把X取出来,并用同样的X代替它。我希望它用一个单词replace被单击的X,所以它可以打开和closures点击。 我想保留它,以便如果他们在同一行中select另一个单元格,原始单元格中的X消失,然后X单击单元格中popup。 我只希望他们能够为每个问题在每一行中select一个单元格。 对不起,我只是想清楚。 这是我的代码目前。

谢谢你尽你所能的帮助!

Sub Worksheet_SelectionChange(ByVal Target As Range) Dim rInt As Range Dim rCell As Range For rw = 3 To 152 Set rInt = Intersect(Target, Range("C" & rw & ":H" & rw)) If Not rInt Is Nothing Then If Application.WorksheetFunction.CountA(Range("C" & rw & ":H" & rw)) > 0 Then Range("C" & rw & ":H" & rw).Value = "" End If For Each rCell In rInt If rCell.Value = "" Then rCell.Value = "X" End If Next End If Set rInt = Nothing Set rCell = Nothing Cancel = True Next End Sub 

尝试下面的代码。 注意我删除了你的循环; 当我们可以精确定位用户select的行并将焦点放在该行上时,没有理由循环遍历每一行。 我还改变了设置rInt的方式,用.Findreplace了CountA函数,并且用testing包装了整个过程,以查看用户是否已经在我们预定义的范围内select了一个单元格(所以当其他单元格不会不必要地运行代码被选中)。

 Option Explicit Sub Worksheet_SelectionChange(ByVal Target As Range) 'Only run the code if the user selected a cell in our defined range: If Not Intersect(Target, Me.Range("C3:H152")) Is Nothing Then 'Declare variables Dim rInt As Range Dim rCell As Range Dim rw As Long Dim xLoc As Range Set rInt = Me.Range(Me.Cells(Target.Row, "C"), Me.Cells(Target.Row, "H")) If Not rInt Is Nothing Then 'Look for a response in our answer range Set xLoc = rInt.Find("x") If Not xLoc Is Nothing Then 'If there was a response and the response was in the same column _ 'we selected, wipe the response and exit the sub. If Target.Column = xLoc.Column Then rInt.Value = vbNullString Exit Sub 'Else, wipe the previous response and add the new response Else rInt.Value = vbNullString Target.Value = "x" End If 'If there were no previous responses... Else: Target.Value = "x" End If End If End If End Sub 

所有你需要的是单选button,链接到一个单元格,然后编辑点击代码。

 Private Sub OptionButton1_Click() Range("D3:H3") = False Range("C3") = True End Sub Private Sub OptionButton2_Click() Range("C3") = False Range("D3") = True Range("E3:H3") = False End Sub 

然后格式化单元格,使文本与背景颜色相同,并使用条件格式来通过引用TRUE或FALSE来改变单选button所在单元格的颜色,对用户来说非常好用,而且容易。

我猜你以前没有用过它们,所以你只需要去开发者选项卡,我总是使用ActiveX单选button。 然后使用devise模式编辑选项button的属性,并更改“GroupName”以将任何单选button链接在一起,只需要单击它们就可以使用相同的组名命名单选button。