【问题标题】:Excel VBA unable to get the Match property of the WorksheetFunction classExcel VBA 无法获取 WorksheetFunction 类的 Match 属性
【发布时间】:2018-05-12 04:35:06
【问题描述】:

我的用户表单中有一个文本字段,它与没有空格的标签标题同名。例如,文本框将命名为“SampleName”,标签标题将命名为“Sample Name”。按提交时,我希望将数据输入到与标题和文本字段名称相同的相应列下的工作簿中。我无法从列标题中删除空间,因为导入另一个软件需要该空间。但是,当我点击提交时,它总是返回一个运行时错误,无法获取匹配属性。当我在手动输入 Label.ctlname.Caption 时尝试此代码时,它工作正常。有什么建议吗?

Dim ssheet As Worksheet
Dim rngsource As Range

Set ssheet = ThisWorkbook.Sheets("Sheet1")

nr = ssheet.Cells(Rows.Count, 1).End(xlUp).Row + 1


With ssheet
  Set rngsource = Range(Cells(1, 1), Cells(1, Range("A1").End(xlToRight).Column))

Dim ctl
Dim ctlname As String

For Each ctl In Me.Controls
    If TypeOf ctl Is msforms.TextBox Then
            ctlname = "Label" & ctl.Name & ".Caption"
            .Cells(nr, Application.WorksheetFunction.Match(ctlname, rngsoruce, 0)) = ctl
            ctl.Text = ""
    End If
Next ctl

End With

【问题讨论】:

  • 显示这个Set rngsource = Range(Cells(1, 1), Cells(1, Range("A1").End(xlToRight).Column))合格吗??
  • 那么你想从我们这里得到什么?如何添加空间?为什么你的比赛失败了?如何正确制作字符串?看来您在"Label" 末尾缺少.
  • .Cells(nr, Application.WorksheetFunction.Match(ctlname, rngsoruce, 0)) = ctl - rngsoruce?将Option Explicit 放在模块顶部,这样您就可以轻松找到这些拼写错误。
  • 要添加@Gary'sStudent 所做的类似声明,nr = ssheet.Cells(Rows.Count, 1).End(xlUp).Row + 1 可能也必须被限定,除非 ssheet 是活动表。
  • @ScottCraner 我特别关注我的比赛失败的原因。为了进一步澄清,标签的名称与前面带有“标签”的文本框相同。例如,文本框为“SampleName”,标签为“LabelSampleName”,标签标题为“Sample Name”。我认为基于此检索标签会更容易,然后在文本框名称中添加空格。如果我手动将标签的名称输入到匹配中,它可以正常工作。一旦我将 ctlname 字符串输入到匹配函数中,它就会给我带来问题

标签: vba excel userform


【解决方案1】:

我建议:

  1. 在用户窗体中标识Labels
  2. 将关联的TextBox设置为Label
  3. 构建字段名称并获取字段列
  4. 将 TextBox 值发布到相应的字段列中
  5. 清除文本框

此过程遵循上面定义的步骤,并提供工作表中字段名称的可能格式,使用所需格式并注释\删除其他格式。

Sub UserForm_PostToWsh()
Dim ws As Worksheet, rgSrc As Range
Dim oCtrl As Object, MsfTb As MSForms.TextBox
Dim sFld As String, lRow As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")
    With ws
        lRow = 1 + .Cells(.Rows.Count, 1).End(xlUp).Row
        Set rgSrc = .Cells(1).Resize(1, .Cells(1).End(xlToRight).Column)
        For Each oCtrl In UfrNew_01.Controls

            Rem Validate Control as Label
            If TypeOf oCtrl Is MSForms.Label Then

                Rem Set Associated TextBox
                Set MsfTb = Nothing
                On Error Resume Next
                Set MsfTb = UfrNew_01.Controls(Replace(oCtrl.Name, "Label", vbNullString))
                On Error GoTo 0

                Rem Validate TexBox
                If Not MsfTb Is Nothing Then
                    sFld = oCtrl.Name & "." & oCtrl.Caption     'If Field format is LabelName.LabelCaption use this line
                    'sFld = oCtrl.Name & "." & MsfTb.Name        'If Field format is LabelName.TextBoxName use this line
                    'sFld = oCtrl.Caption & "." & oCtrl.Name     'If Field format is LabelCaption.LabelName use this line
                    'sFld = oCtrl.Caption & "." & MsfTb.Name     'If Field format is LabelCaption.TextBoxName use this line
                    'sFld = MsfTb.Name & "." & oCtrl.Name        'If Field format is TextBoxName.LabelName use this line
                    'sFld = MsfTb.Name & "." & oCtrl.Caption     'If Field format is TextBoxName.LabelCaption use this line

                    Rem Post Text Value
                    .Cells(lRow, WorksheetFunction.Match(sFld, rgSrc, 0)) = MsfTb.Value

                    Rem Initialize TextBox
                    MsfTb.Text = vbNullString

    End If: End If: Next: End With

    End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-13
    • 2015-06-14
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多