如果在运行下列代码时出现错误提示‘方法和数据成员未找到’,可能是因为文本框 'TextBox12' 的 'TextFormat' 属性不支持在 VBA 窗体中直接设置。

要解决这个问题,你可以使用 'Format' 函数将 'TextBox12' 的值转换为带百分号格式的字符串,并将该字符串赋给工作表单元格。

以下是修改后的代码:

Private Sub CommandButton2_Click()
    Dim ws As Worksheet
    Dim lastRow As Long
    
    Set ws = Sheets("销售明细表")
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1
    
    ws.Cells(lastRow, "B").Value = ComboBox1.Value
    ws.Cells(lastRow, "C").Value = ComboBox2.Value
    ws.Cells(lastRow, "D").Value = ComboBox3.Value
    ws.Cells(lastRow, "E").Value = TextBox5.Value
    ws.Cells(lastRow, "F").Value = ComboBox4.Value
    ws.Cells(lastRow, "G").Value = TextBox7.Value
    ws.Cells(lastRow, "H").Value = TextBox8.Value
    ws.Cells(lastRow, "I").Value = TextBox9.Value
    ws.Cells(lastRow, "J").Value = TextBox10.Value
    ws.Cells(lastRow, "K").Value = TextBox11.Value
    ws.Cells(lastRow, "L").Value = Format(TextBox12.Value, "0.00%") ' 使用 Format 函数转换为百分号字符串
    ws.Cells(lastRow, "M").Value = TextBox13.Value
    ws.Cells(lastRow, "N").Value = TextBox14.Value
    ws.Cells(lastRow, "O").Value = TextBox15.Value
    ws.Cells(lastRow, "P").Value = TextBox16.Value
    ws.Cells(lastRow, "Q").Value = TextBox17.Value
    ws.Cells(lastRow, "S").Value = TextBox18.Value
    ws.Cells(lastRow, "T").Value = ComboBox5.Value
    ws.Cells(lastRow, "U").Value = TextBox20.Value
    
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=ws.Range("E10:E" & lastRow), SortOn:=xlSortOnValues, Order:=xlDescending
        .SetRange ws.Range("B10:U" & lastRow)
        .Header = xlYes
        .Apply
    End With
End Sub

在上述修改后的代码中,我们使用 'Format' 函数将 'TextBox12' 的值转换为百分号格式的字符串,然后将该字符串赋给工作表单元格。

请确保将修改后的代码放置在窗体的代码模块中,并将按钮 "CommandButton2_Click" 与按钮 "CommandButton2" 关联。

VBA 错误提示“方法和数据成员未找到”:TextBox TextFormat 属性的解决方案

原文地址: https://www.cveoy.top/t/topic/5i2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录