vba 激活从用户表单中的 Excel 工作表发送的复选框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19357937/
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
Activate checkbox value sent from Excel sheet in userform
提问by Evan Milacic
Since a while I have been trying to make this work but in spite of my thorough Internet searches the answer keeps eluding me. I made a userForm in VBA Excel with some textboxes en checkboxes. These values are sent to a sheet in the excel file. The checkboxes represent categories so multiple choices can be selected. The value is translated to true = "x" and false = "" before it is send to the sheet.
一段时间以来,我一直在努力使这项工作发挥作用,但尽管我在 Internet 上进行了彻底的搜索,但答案仍然无法解决。我在 VBA Excel 中创建了一个用户窗体,其中包含一些文本框和复选框。这些值将发送到 Excel 文件中的工作表。复选框代表类别,因此可以选择多个选项。该值在发送到工作表之前被转换为 true = "x" 和 false = ""。
If userForm.checkbox1.Value = True Then
ws.Rows.Cells(row, 9).Value = "X"
Else
ws.Rows.Cells(row, 9).Value = ""
End If
The information put in the sheet can be retrieved and edited by using almost the same userForm. This form has a row selection box with which the desired row can be summoned and the corresponding data populates the userform. The "x" is translated back like this:
可以使用几乎相同的 userForm 检索和编辑放入工作表中的信息。该表单有一个行选择框,可以使用该框调用所需的行,并将相应的数据填充到用户表单中。“x”被翻译回如下:
If ws1.Rows.Cells(row, 9).Value = "X" Then
userFormedit.checkbox1.Value = True
Else
userFormedit.checkbox1.Value = False
End If
The problem is that the selected checkboxes show the desired value but in grey. When sending the information back to the sheet, these values are not "seen". So on the sheet these values disappear. One option is to re-check all the boxes again. This is not what I desire since a lot of people are going to use this form and that would be time consuming and impractical. Is there a way to activate/enable the checkboxes?
问题是选定的复选框显示了所需的值,但显示为灰色。将信息发送回工作表时,不会“看到”这些值。所以在工作表上这些值消失了。一种选择是再次重新选中所有复选框。这不是我想要的,因为很多人会使用这种形式,这既费时又不切实际。有没有办法激活/启用复选框?
回答by MikeD
You should consider sending from User form to sheet in a _Click()
and loading sheet values to Form in an _Activate
sub, e.g.
您应该考虑从用户表单发送_Click()
到表单中的表单并将表单值加载到表单中的表单_Activate
,例如
Private Sub CheckBox1_Click()
If CheckBox1.Value Then
[A1] = "X"
Else
[A1] = ""
End If
End Sub
Private Sub UserForm_Activate()
If [A1] = "X" Then
Me.CheckBox1 = True
Else
Me.CheckBox1 = False
End If
End Sub
If a checkbox is shown in grey, the reason could be that it was disabled by the same or another piece of code or from the element's property window (CheckBox1.Enabled = False
).
如果复选框显示为灰色,原因可能是它被相同或另一段代码或元素的属性窗口 ( CheckBox1.Enabled = False
)禁用。
Also mind that Excel is not really multi-user friendly. Once multiple users have opened the same Excel, it resides in their PC's memory and other peoples' changes are not visible unless they are closing & reopening the file.
还要注意 Excel 并不是真正的多用户友好。一旦多个用户打开了同一个 Excel,它就会驻留在他们 PC 的内存中,除非他们关闭并重新打开文件,否则其他人的更改是不可见的。