运行时错误'-2147417848(80010108)':对象'_Worksheet'的方法'单元'失败

问题描述 投票:0回答:1

当我试图将数据从文本框中放入单元格时,我有这个错误。

调试标记此行:

Sheets(vCustomersShName).Cells(RowSelect, 1) = vCustomerID

(CustomerID是时间戳字符串,即050918190442(“ddmmyyhhmmss”))

这是我的代码:

Private Sub CB_Save_Click()

'Get Customer SheetName
Dim vCustomersShName As String
vCustomersShName = GetShName("Tb_Customers")


Dim vCustomerID As String
vCustomerID = UF_NewCustomer.TB_CustomerID.Value

'Clear filter
Sheets(vCustomersShName).Range("A1").AutoFilter


'Sort By CustomerID Asc
Sheets(vCustomersShName).ListObjects("Customers").Sort.SortFields _
        .Add Key:=Range("Customers[Customer_ID]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
With Sheets(vCustomersShName).ListObjects("Customers").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
End With

'Filtering By CustomerID
Sheets(vCustomersShName).ListObjects("Customers").Range.AutoFilter Field:=1, Criteria1:=vCustomerID 'Column 1=CustomerID

'Verify if the first row contain CustomerID
Dim FirstRowCustomer As Integer ' First row after filtering
FirstRowCustomer = Sheets(vCustomersShName).AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2).Row

Dim ExistCustomerID As String
ExistCustomerID = Sheets(vCustomersShName).Cells(FirstRowCustomer, 1).Value

Dim RowSelect As Integer
If Len(ExistCustomerID) = 0 Then 'If FirstRow is empty = It's a new customer --> insert row
    Dim LastRowCustomer As Integer
    LastRowCustomer = Sheets(vCustomersShName).Range("Customers").Rows.Count + 1 ' +1 for the header
    RowSelect = LastRowCustomer + 1 'Insert to new row in Customers table
Else 'If FirstRow is not empty = It's a new customer --> Update row
    RowSelect = FirstRowCustomer
End If

'Fill data from TextBoxs to Customers table
Sheets(vCustomersShName).Cells(RowSelect, 1) = vCustomerID
Sheets(vCustomersShName).Cells(RowSelect, 2) = UF_NewCustomer.TB_Shem.Value
Sheets(vCustomersShName).Cells(RowSelect, 3) = UF_NewCustomer.TB_Mishpacha.Value
Sheets(vCustomersShName).Cells(RowSelect, 4) = UF_NewCustomer.TB_LName.Value
Sheets(vCustomersShName).Cells(RowSelect, 5) = UF_NewCustomer.TB_FName.Value
Sheets(vCustomersShName).Cells(RowSelect, 6) = UF_NewCustomer.TB_Tel.Value
Sheets(vCustomersShName).Cells(RowSelect, 7) = UF_NewCustomer.TB_Mail.Value

If Len(ExistCustomerID) = 0 Then 'Insert new row
    MsgBox UF_NewCustomer.TB_Shem.Value & " " & UF_NewCustomer.TB_Mishpacha.Value & " was ADDED successfully (:"

    Room.Lb_Customers.ListIndex = Room.Lb_Customers.ListCount - 1

Else                        'Update exist row
    MsgBox UF_NewCustomer.TB_Shem.Value & " " & UF_NewCustomer.TB_Mishpacha.Value & " was UPDATED successfully (:"
End If
Unload Me
End Sub
vba excel-vba
1个回答
0
投票

尝试用“工作表”替换对“表格”的引用。此外,使用with块来减少冗余代码。

With Worksheets(vCustomersShName)
    .Cells(RowSelect, 1) = vCustomerID
    .Cells(RowSelect, 2) = UF_NewCustomer.TB_Shem.Value
    .Cells(RowSelect, 3) = UF_NewCustomer.TB_Mishpacha.Value
    .Cells(RowSelect, 4) = UF_NewCustomer.TB_LName.Value
    .Cells(RowSelect, 5) = UF_NewCustomer.TB_FName.Value
    .Cells(RowSelect, 6) = UF_NewCustomer.TB_Tel.Value
    .Cells(RowSelect, 7) = UF_NewCustomer.TB_Mail.Value
End With
© www.soinside.com 2019 - 2024. All rights reserved.