当我尝试将数据从文本框中放入单元格时,我遇到了这个错误。
调试标记了这一行:
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
答案 0 :(得分: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