Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA-从用户中的组合框复制文本,并在循环中粘贴到excel工作表上_Excel_Vba_Loops_Combobox_Userform - Fatal编程技术网

VBA-从用户中的组合框复制文本,并在循环中粘贴到excel工作表上

VBA-从用户中的组合框复制文本,并在循环中粘贴到excel工作表上,excel,vba,loops,combobox,userform,Excel,Vba,Loops,Combobox,Userform,我是excel VBA的新手,我尝试过采用堆栈溢出上的一些解决方案,但我仍然被卡住了 我正在尝试使用userform中组合框中选定选项的文本将此选定文本的行粘贴到excel工作表中 下面的代码用于选择当前活动用户表单的组合框值“CboIncomesPatch”,并将该值粘贴到活动表的单元格“M8”中,然后循环粘贴,直到达到计数器计时到数字总单位数为止。单位总数是一个文本框值“TxtNumberOfUnits”,例如“15”。组合框中的15个文本应粘贴在单元格“M8”中,然后粘贴所有后续行“M9”

我是excel VBA的新手,我尝试过采用堆栈溢出上的一些解决方案,但我仍然被卡住了

我正在尝试使用userform中组合框中选定选项的文本将此选定文本的行粘贴到excel工作表中

下面的代码用于选择当前活动用户表单的组合框值“CboIncomesPatch”,并将该值粘贴到活动表的单元格“M8”中,然后循环粘贴,直到达到计数器计时到数字总单位数为止。单位总数是一个文本框值“TxtNumberOfUnits”,例如“15”。组合框中的15个文本应粘贴在单元格“M8”中,然后粘贴所有后续行“M9”、“M10”等。组合框使用的范围是硬编码的,所有选项都是人名,均为字符串(无数字)

错误发生在以下代码的.SelectedItem。当我将循环用于文本框(不包括代码中选择combobox值的部分)时,循环会起作用

请让我知道,如果我可以在这里提供更多的信息,以帮助您的答案

谢谢,

尼尔

Sub\u Patch()
作为整数的Dim计数器
Dim-ws-As-UserForm
将总计设置为整数
收入微薄如弦
设置ws=UserForm(this.CboIncomesPatch.GetItemText(Me.CboIncomesPatch.SelectedItem))
设置收入。值=ws
总计=TxtNumberOfUnits.Value
计数器=0
Application.ActiveSheet.range(“M8”)。选择

Do While Counter您不能使用组合框的.SelectedText属性吗

Set ws = UserForm(Me.CboIncomesPatch.SelectedText)
Set Incomes.Value = ws

请在下面找到执行此操作的正确代码:

Sub Incomes_Patch()

Dim Counter As Integer
'Dim ws As UserForm
Dim Total As Integer
Dim Incomes As String

'I am not sure why you wanted the form object, you need the combobox value right? you can directly get it from Combobox.

'Set ws = UserForm(this.CboIncomesPatch.GetItemText(Me.CboIncomesPatch.SelectedItem))
Incomes = Me.CboIncomesPatch.Text ' selected item doesn't work in Combo, so you need to use Text

Total = TxtNumberOfUnits.Text
Counter = 0

Application.ActiveSheet.Range("M8").Select
Do While Counter <= Total
If Counter = Total Then Exit Do
    ActiveCell.Value = Incomes & Counter
    ActiveCell.Offset(1, 0).Select
    Counter = Counter + 1
Loop

End Sub
Sub\u Patch()
作为整数的Dim计数器
'Dim ws As UserForm
将总计设置为整数
收入微薄如弦
'我不确定您为什么需要表单对象,您需要combobox值,对吗?您可以直接从Combobox获得它。
'Set ws=UserForm(this.CboIncomesPatch.GetItemText(Me.CboIncomesPatch.SelectedItem))
Incomers=Me.CboIncomesPatch.Text'所选项目在组合中不起作用,因此您需要使用文本
总计=TxtNumberOfUnits.Text
计数器=0
Application.ActiveSheet.Range(“M8”)。选择

尽管道歉,但还是接受吧。没有意识到我需要这么做。谢谢你,尼尔
Sub Incomes_Patch()

Dim Counter As Integer
'Dim ws As UserForm
Dim Total As Integer
Dim Incomes As String

'I am not sure why you wanted the form object, you need the combobox value right? you can directly get it from Combobox.

'Set ws = UserForm(this.CboIncomesPatch.GetItemText(Me.CboIncomesPatch.SelectedItem))
Incomes = Me.CboIncomesPatch.Text ' selected item doesn't work in Combo, so you need to use Text

Total = TxtNumberOfUnits.Text
Counter = 0

Application.ActiveSheet.Range("M8").Select
Do While Counter <= Total
If Counter = Total Then Exit Do
    ActiveCell.Value = Incomes & Counter
    ActiveCell.Offset(1, 0).Select
    Counter = Counter + 1
Loop

End Sub