vba 根据单选按钮选择分配单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23730752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
assign cell value based on radio button selection
提问by user1787114
i am trying to assign a value to a cell when the user selects a radio button, the value changes depending on the radio selected. The radio buttons are on a form and i am trying to assign the value to a sheet called "Workspace"
当用户选择一个单选按钮时,我试图为单元格分配一个值,该值会根据所选的单选按钮而变化。单选按钮位于表单上,我正在尝试将该值分配给名为“工作区”的工作表
here is the code i have
这是我的代码
Private Sub OK_Click()
'A3 Assignment
If OpQ1_01_1.Value = True Then
Sheets("Workpace").Cells("A3").Value = "1"
ElseIf OpQ1_01_2.Value = True Then
Sheets("Workpace").Cells("A3").Value = "2"
ElseIf OpQ1_01_3.Value = True Then
Sheets("Workspace").Cells(A3).Value = "3"
ElseIf OpQ1_01_4.Value = True Then
Sheets("Workpace").Cells("A3").Value = "4"
ElseIf OpQ1_01_5.Value = True Then
Sheets("Workpace").Cells("A3").Value = "5"
ElseIf OpQ1_01_6.Value = True Then
Sheets("Workpace").Cells("A3").Value = "6"
End If
as far as i can tell it should work, the sheet is there and theres a cell A3, but i keep getting a message stating "application-defined or object-defined error" which isnt telling me anything, but it highlights the assign part of the code for the radio button I selected (in this case the third option)
据我所知它应该可以工作,工作表在那里并且有一个单元格 A3,但我不断收到一条消息,指出“应用程序定义或对象定义的错误”,它没有告诉我任何事情,但它突出显示了分配部分我选择的单选按钮的代码(在本例中为第三个选项)
debug highlights this chunk of code in this case
在这种情况下,调试突出显示了这块代码
Sheets("Workspace").Cells(A3).Value = "3"
回答by L42
Change Cells
to Range
like this:
更改Cells
到Range
这样的:
Sheets("Workspace").Range("A3").Value = "3"
or if you want to stick to Cells
like this:
或者如果你想坚持Cells
这样:
Sheets("Workspace").Cells(3, 1).Value = "3"
'~~> where 3 is the row number and 1 is the column number.
回答by guest
The name of the sheet seems to be missing an "s" in your code (you typed "Workpace" in the code instead of "Workspace"
工作表的名称似乎在您的代码中缺少“s”(您在代码中输入了“Workpace”而不是“Workspace”