【问题标题】:VBA - copy text from Combobox in User from and paste in loop onto excel worksheetVBA - 从用户窗体中的组合框中复制文本并循环粘贴到 Excel 工作表中
【发布时间】:2013-04-25 15:51:48
【问题描述】:

我是 excel VBA 的新手,我尝试调整 Stack Overflow 上的一些解决方案,但我仍然卡住了!

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

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

错误发生在以下代码的 .SelectedItem 处。当我将它用于文本框时,循环有效,不包括选择代码的组合框值部分。

如果我可以在此处提供更多信息来帮助您回答,请告诉我。

谢谢,

尼尔

Sub Incomes_Patch()

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

Set ws = UserForm(this.CboIncomesPatch.GetItemText(Me.CboIncomesPatch.SelectedItem))
Set Incomes.Value = ws

Total = TxtNumberOfUnits.Value
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

【问题讨论】:

    标签: excel vba loops combobox userform


    【解决方案1】:

    你能不使用组合框的 .SelectedText 属性吗?

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

    【讨论】:

      【解决方案2】:

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

      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
      

      有帮助吗?

      谢谢,V

      【讨论】:

      • 抱歉 - 刚刚接受。没有意识到我需要这样做。谢谢你,尼尔
      猜你喜欢
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 2013-06-08
      相关资源
      最近更新 更多