用 VBA 表单中的文本框输入替换 Excel 单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11828342/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:10:52  来源:igfitidea点击:

Replace Excel Cell Value with Text Box Input in VBA Form

excelvbaexcel-vba

提问by cjatpuresilica

I have created a simple form in Excel 2010 in which I wish to replace the individual text string value in 11 cells in the cell range B5 : B15

我在Excel 2010 中创建了一个简单的表单,我希望在其中替换单元格范围内 11 个单元格中的单个文本字符串值B5 : B15

I have used this code to test the first cell at B5which is referenced in A5by the Label Christian Name and I have labelled the text box on the form Christian_Input:

我已使用此代码来测试Label Christian NameB5所引用的第一个单元格,并A5在表单 Christian_Input 上标记了文本框:

Private Sub Christian_Input_Change()

ThisWorkbook.Worksheets("Sheet1").Cells(5, 1) = Christian_Input.Text
End Sub

When I enter a new text string in the form and run it, the form simply appears over my Sheet1 with the replacement text in the input text box but the text string in cell B5is neither highlighted or replaced.

当我在表单中输入一个新的文本字符串并运行它时,该表单只是出现在我的 Sheet1 上,输入文本框中的替换文本,但单元格中的文本字符串B5既没有突出显示也没有被替换。

Finally when I open the file the form is not visible.

最后,当我打开文件时,表单不可见。

Can anyone please advise what alterations I need to make.

任何人都可以请告知我需要进行哪些更改。

回答by enderland

ThisWorkbook.Worksheets("Sheet1").range("B5:B15").value = Christian_Input.Text
ThisWorkbook.Worksheets("Sheet1").range("B5").select

Add the following to your "This Workbook" module to auto-start the form when you open the workbook (replace UserFormNameHere with the name of your form):

将以下内容添加到您的“此工作簿”模块以在您打开工作簿时自动启动表单(将 UserFormNameHere 替换为您的表单名称):

Private Sub Workbook_Open()
    UserFormNameHere.show   
End Sub