vba 如何将用户表单文本框值与任意字符串连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20810441/
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
How do I concatenate a user form text box value with an arbitrary string?
提问by detroitwilly
I'm working on a userform for an excel sheet I'm working on. I have a specific text box in the userform for a work order number (named txtWO). I would like to pre-concatenate the user-entered value from this text box (a 5-digit string) with the text 'WO-' and then insert it into a cell in the worksheet. The small bit of VBA I have written to do this is shown below.
我正在处理我正在处理的 Excel 工作表的用户表单。我在用户表单中有一个特定的文本框,用于输入工单编号(名为 txtWO)。我想将此文本框(一个 5 位字符串)中的用户输入值与文本 'WO-' 预先连接,然后将其插入到工作表中的单元格中。我为此编写的一小部分 VBA 如下所示。
ws.Cells(iRow, 2).Formula = "=CONCATENATE(""WO-"",me.txtWO.Value)"
Where iRow is a value, formatted as long, that represents the value of the first empty row in the worksheet.
其中 iRow 是一个值,格式为 long,表示工作表中第一个空行的值。
This is all syntactically correct, as far as the VBA is concerned, but after the the value is inserted in the cell, I receive a #NAME?error. When I show the calculation steps for the formula, 'Me.txtWO.Value' evaluates to a #NAME?error.
就 VBA 而言,这在语法上都是正确的,但是在将值插入单元格后,我收到一个#NAME?错误。当我显示公式的计算步骤时,'Me.txtWO.Value' 的计算结果为#NAME?错误。
I can't tell if this is happening because something is wrong with my VBA, or if I am misunderstanding how to work with text box values. But if anyone can shed any light, it would be much appreciated.
我无法判断这是否是因为我的 VBA 出现问题,或者我是否误解了如何使用文本框值。但是,如果有人能提供任何线索,将不胜感激。
Thanks.
谢谢。
-Sean
-肖恩
采纳答案by serakfalcon
Excel's cells can't access the textbox value. Just do everything in VBA like so:
Excel 的单元格无法访问文本框值。像这样在 VBA 中做所有事情:
ws.Cells(iRow, 2).Value = "WO-" & me.txtWO.Value