Excel VBA – 无法引用对象/不能将信息放入工作表中

我有一个接口,我正在做一些问题。 我是新来的,经过很多的search,我终于放弃了,来到这里问。

首先,我让我的代码这样做:

  • 从工作表上的button打开一个用户表单。
  • 在用户窗体打开后,它有一些默认的字段(一些文本框,一个标签和一个命令button)。
  • 然后你有一个命令button,创build一个新的一轮领域,并为他们分配一个类来执行一些简单的代码(数字限制一些文本框,并创build一个新的文本框与button的内容)。

我的问题是以下几点:

  • 我试图将ControlSource分配给字段,但Excel不允许我,只有它允许我,字段没有更新单元格,也没有单元格更新字段。
  • 我也尝试从新创build的字段的值直接加载到工作表,但我不知道如何调用该对象,并获得其属性。

这是我正在使用的代码:

 Option Explicit Dim cargarconcepto() As New Clase3 Private Sub CommandButton19_Click() Dim txtCtl As Control Dim concepto As Object Dim i As Long i = labelcounter controlextra = controlextra + 1 Dim Label As Object Dim lblctr As Long Dim nmctr As Long Dim t1 As Object Dim t2 As Control Dim t3 As Control Dim t4 As Control Dim t5 As Object Dim t6 As Object Dim cargaconcepto As Control UserForm1.Frame21.ScrollHeight = 80 + (25 * controlextra) - 25 UserForm1.Frame21.ScrollTop = UserForm1.Frame21.ScrollHeight nmctr = labelcounter + 1 For lblctr = labelcounter To controlextra Set Label = UserForm1.Frame21.Controls.Add("Forms.Label.1", "Labelt" & labelcounter, True) With Label .Caption = "0005 - " .Left = 3 .Height = 12 .Width = 24 .Top = 25 + (25 * labelcounter) End With Set t1 = UserForm1.Frame21.Controls.Add("Forms.TextBox.1", "t" & labelcounter + 1 & "1", True) With t1 .value = Workbooks("A Facturar Proyecto tarjetas.xlsm").Sheets("Data").Range("as1").value .Left = 31 .TextAlign = fmTextAlignCenter .Enabled = False .Height = 15.75 .Width = 54 .Top = 25 + (25 * labelcounter) End With Set t2 = UserForm1.Frame21.Controls.Add("Forms.TextBox.1", "t" & labelcounter + 1 & "2", False) With t2 .value = "" .Left = 97 .Height = 15.75 .Width = 90 .Top = 25 + (25 * labelcounter) .Visible = True End With Set t3 = UserForm1.Frame21.Controls.Add("Forms.TextBox.1", "t" & labelcounter + 1 & "3", False) With t3 .value = "" .Left = 199 .Height = 15.75 .Width = 90 .Top = 25 + (25 * labelcounter) .Visible = True End With Set t4 = UserForm1.Frame21.Controls.Add("Forms.TextBox.1", "t" & labelcounter + 1 & "4", False) With t4 .value = "" .Left = 307 .Height = 15.75 .Width = 90 .Top = 25 + (25 * labelcounter) .Visible = True End With Set t5 = UserForm1.Frame21.Controls.Add("Forms.TextBox.1", "t" & labelcounter + 1 & "5", True) With t5 .value = "" .Left = 415 .Height = 15.75 .Width = 90 .Top = 25 + (25 * labelcounter) End With Set t6 = UserForm1.Frame21.Controls.Add("Forms.TextBox.1", "t" & labelcounter + 1 & "6", True) With t6 .value = "" .Left = 517 .Height = 15.75 .Width = 90 .Top = 25 + (25 * labelcounter) End With Set concepto = UserForm1.Frame21.Controls.Add("Forms.TextBox.1", "concepto" & labelcounter + 1, False) With concepto .value = "" .BackStyle = fmBackStyleTransparent .Left = 619 .Height = 20 .Width = 210 .Top = 25 + (25 * labelcounter) .Enabled = False .value = Workbooks("A Facturar Proyecto tarjetas").Sheets("Data").Range("Ac45") .Visible = True End With Set cargaconcepto = UserForm1.Frame21.Controls.Add("forms.commandbutton.1", "Cargar Concepto" & labelcounter + 1, False) With cargaconcepto .Caption = "Cargar Concepto" .Left = 834 .Height = 20 .Width = 90 .Top = 25 + (25 * labelcounter) .Tag = labelcounter + 1 .Visible = True End With ReDim Preserve num(1 To i) Set num(i).txtCtrl = t2 ReDim Preserve num2(1 To i) Set num2(i).txtCtrl = t3 ReDim Preserve num3(1 To i) Set num3(i).txtCtrl = t4 ReDim Preserve cargarconcepto(1 To i) Set cargarconcepto(i).cargar = cargaconcepto Set cargaconcepto = Nothing Set t3 = Nothing Set t4 = Nothing Set t2 = Nothing labelcounter = labelcounter + 1 Next End Sub 

然后这里是class级:

  Option Explicit Public WithEvents cargar As MSForms.CommandButton Private Sub cargar_Click() Dim concepto As Control Set concepto = UserForm1.Frame21.Controls.Add("Forms.TextBox.1", "concepto" & labelcounter + 1, True) With concepto .value = "" .BackStyle = fmBackStyleTransparent .Left = 619 .Height = 20 .Width = 210 .Top = (25 * b) .Enabled = False .value = Workbooks("A Facturar Proyecto tarjetas").Sheets("Data").Range("Ac45") End With End Sub 

我试图使用像UserForm1.Frame21.concepto & cargar.Tag获取新创build的字段的名称,并因此replace它的值为一个新的AND加载该值到工作表。
但我只是找不到办法做到这一点。 现在我有一个几乎完整的数据input界面,但我不能把数据放到电子表格中。

请我需要帮助!