当我点击我工作表上的任何复选框时,我会疯狂地试图找到运行代码的方法。我已经看过多篇文章谈论制作类模块,但我似乎无法让它工作。
我的代码将填充B列以匹配C列。无论我手动输入C10,将填入B10,即使C10是公式:= D9。所以,我可以在D10中键入TRUE,C10中的公式将导致:TRUE然后代码填充B10说:TRUE。真棒......诀窍是有一个链接到D10的复选框。当我单击复选框时,D10表示TRUE,C10中的公式表示TRUE,但这就是它。 VBA代码无法识别复选框单击。如果我然后单击工作表(选择更改),那么代码将运行,所以我知道我需要一个不同的事件。
将事件更改为“Checkbox1_Click()”很容易,但我希望它可以用于我点击的任何复选框。经过几天的搜索和尝试不同的事情,我没有运气。
这是我到目前为止运行的代码
#include <iostream>
using namespace std;
int t , n ;unsigned long long m;
int a2 [4] = {2,4,8,6};
int a3 [4] = {3,9,7,1}, a4 [2] = {4,6};
int a5 = 5,a6 =6;
int a7 [4] = {7,9,3,1}, a8 [4] = {8,4,2,6}, a9 [2] = {9,1},led[10] = {1,1,4,4,2,1,1,4,4,2};
int ans(int y,int i){if(y == 0 || y==1 || y==5 || y== 6){return y;}if(y== 2){return a2[i];}if(y==3){return a3[i];}if(y==4){a4[i];}if(y==7){return a7[i];}if(y==8){return a8[i];}if(y==9){return a9[i];}
}
int main()
{
int x,i;cin>>t;
while(t--)
{
cin>>n>>m;
if (n==0 || n==1 ||m==0 ){cout<<1<<endl;continue;}x = n%10;i = (m%led[x])-1;cout<<ans(x,i)<<endl;}
return 0;
}
任何帮助将不胜感激。
答案 0 :(得分:0)
您只需要让每个复选框的_Click()
事件都知道您要运行Worksheet_SelectionChange
事件。为此,您需要将以下行添加到每个_Click()
子目录中:
Call Worksheet_SelectionChange(Range("a1"))
请注意,由于您未在代码中使用SelectionChange
,因此传递给Target
sub的范围无关紧要。
答案 1 :(得分:0)
这是有效的
' this goes into sheet code
Private Sub Worksheet_Activate()
activateCheckBoxes
End Sub
' put all this code in class a module and name the class module "ChkClass"
Option Explicit
Public WithEvents ChkBoxGroup As MSForms.CheckBox
Private Sub ChkBoxGroup_Change()
Debug.Print "ChkBoxGroup_Change"
End Sub
Private Sub ChkBoxGroup_Click()
Debug.Print "ChkBoxGroup_Click"; vbTab;
Debug.Print ChkBoxGroup.Caption; vbTab; ChkBoxGroup.Value
ChkBoxGroup.TopLeftCell.Offset(0, 2) = ChkBoxGroup.Value
End Sub
。
' this code goes into a module
Option Explicit
Dim CheckBoxes() As New ChkClass
Const numChkBoxes = 20
'
Sub doCheckBoxes()
makeCheckBoxes
activateCheckBoxes
End Sub
Sub makeCheckBoxes() ' creates a column of checkBoxes
Dim sht As Worksheet
Set sht = ActiveSheet
Dim i As Integer
For i = 1 To sht.Shapes.Count
' Debug.Print sht.Shapes(1).Properties
sht.Shapes(1).Delete
DoEvents
Next i
Dim xSize As Integer: xSize = 2 ' horizontal size (number of cells)
Dim ySize As Integer: ySize = 1 ' vertical size
Dim t As Range
Set t = sht.Range("b2").Resize(ySize, xSize)
For i = 1 To numChkBoxes
sht.Shapes.AddOLEObject ClassType:="Forms.CheckBox.1", Left:=t.Left, Top:=t.Top, Width:=t.Width - 2, Height:=t.Height
DoEvents
Set t = t.Offset(ySize)
Next i
End Sub
Sub activateCheckBoxes() ' assigns all checkBoxes on worksheet to ChkClass.ChkBoxGroup
Dim sht As Worksheet
Set sht = ActiveSheet
ReDim CheckBoxes(1 To 1)
Dim i As Integer
For i = 1 To sht.Shapes.Count
ReDim Preserve CheckBoxes(1 To i)
Set CheckBoxes(i).ChkBoxGroup = sht.Shapes(i).OLEFormat.Object.Object
Next i
End Sub