【问题标题】:Assign textbox values to corresponding cells based on name根据名称将文本框值分配给相应的单元格
【发布时间】:2009-01-30 12:24:52
【问题描述】:

我在表单上有一组文本框,称为 sm1、sm2 等,我想将它们分配给单元格 A1、A2 等。有没有办法把它放在一个循环中,即:

For i = 1 to 100
  Cells(i, 1).Value = ("sm" & c).Value
Next i

【问题讨论】:

    标签: vba loops excel textbox


    【解决方案1】:

    不确定VBA,但表单上应该有“控件”集合,您可以通过控件名称访问它的元素,如上所示。

    cells(i,1).Value = Controls("sm"&c).Value
    

    【讨论】:

      【解决方案2】:

      另一种方法是使用每个文本框的ControlSource 属性将它们绑定到相关单元格

      【讨论】:

        【解决方案3】:

        通过表单上的文本框,您可以使用 Controls 集合。

        For Each oControl In Me.Controls
            If Typename(oControl) = "TextBox" Then
                iCellNumber = Val(Mid$(oControl.Name, 3)) 'Assumes all textboxes have two letter names
                cells(iCellNumber ,1).Value = val(oControl.Text)
            End If
        Next oControl
        

        如果控件在工作表上,您可以使用该工作表上的形状集合

        For Each oControl In Me.Shapes
            If InStr(oControl.Name, "TextBox") = 1 Then
                iCellNumber = Val(Mid$(oControl.Name, 3))
                cells(iCellNumber ,1).Value = val(oControl.Text)
            End If
        Next oControl
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-12
          相关资源
          最近更新 更多