如何使用锁定的工作表上的搜索和更新按钮

时间:2019-04-22 13:36:12

标签: excel vba

我已经输入了将数据添加到锁定的工作表中的代码,但是当尝试使用用户窗体更改数据时,搜索和更新命令不起作用。

我设法找到了添加数据的方法,但没有找到使之适用于搜索和更新的方法

Private Sub Workbook_Open()
Sheet1.Protect Password:="xyz", UserInterfaceOnly:=True
End Sub

Private Sub cmdAdd_Click()
'to check the last filled row
lastrow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
ThisWorkbook.Worksheets("Sheet1").Cells(lastrow + 1, 1).Value = txtInvoiceNumber.Text


Private Sub cmdFind_Click()
Dim totalrow As Long
totalrow = Sheet1.Range("A1").CurrentRegion.Rows.Count
For currentrow = 2 To totalrow
If Trim(txtLineNumber) = Trim(Cells(currentrow, 2)) Then
txtInvoiceNumber.Text = Cells(currentrow, 1)
txtInvoiceDate.Text = Cells(currentrow, 3)


Private Sub cmdUpdate_Click()
Dim totalrow As Long
totalrow = Sheet1.Range("A1").CurrentRegion.Rows.Count
For currentrow = 2 To totalrow
If Trim(txtLineNumber) = Trim(Cells(currentrow, 2)) Then
Cells(currentrow, 1) = txtInvoiceNumber.Text
Cells(currentrow, 3) = txtInvoiceDate.Text

我想知道代码,以便在工作表锁定时搜索/查找和更新按钮也可以更新数据。 谢谢

错误 enter image description here

调试 enter image description here

完整代码

Private Sub cmdFind_Click()
Dim totalrow As Long
totalrow = Sheet1.Range("A1").CurrentRegion.Rows.Count
For currentrow = 2 To totalrow
If Trim(txtLineNumber) = Trim(Cells(currentrow, 2)) Then
txtInvoiceNumber.Text = Cells(currentrow, 1)
txtInvoiceDate.Text = Cells(currentrow, 3)
cmbCustomerName.Text = Cells(currentrow, 4)
txtDestinationC.Text = Cells(currentrow, 5)
txtDestinationCnty.Text = Cells(currentrow, 6)
txtLine.Text = Cells(currentrow, 7)
cmbBrand.Text = Cells(currentrow, 8)
cmbType.Text = Cells(currentrow, 9)
cmbProduct.Text = Cells(currentrow, 10)
cmbPackaging.Text = Cells(currentrow, 11)
txtCartons.Text = Cells(currentrow, 12)
txtQuantity.Text = Cells(currentrow, 13)
cmbCurrency.Text = Cells(currentrow, 14)
txtUnitPrice.Text = Cells(currentrow, 15)
txtDocumentation.Text = Cells(currentrow, 16)
txtAdditionalF.Text = Cells(currentrow, 17)
txtTransport.Text = Cells(currentrow, 18)
cmbStatus.Text = Cells(currentrow, 19)
txtPdDate.Text = Cells(currentrow, 20)
txtCustomerRef.Text = Cells(currentrow, 21)
txtLoadDate.Text = Cells(currentrow, 22)
txtASMnumber.Text = Cells(currentrow, 23)
cmbWHS.Text = Cells(currentrow, 24)
txtCompleteDate.Text = Cells(currentrow, 25)
txtAccPacNumber.Text = Cells(currentrow, 26)
cmbZeroInvReason.Text = Cells(currentrow, 27)
cmbRep.Text = Cells(currentrow, 28)
txtDriver.Text = Cells(currentrow, 29)
txtContactD.Text = Cells(currentrow, 30)
txtContainer.Text = Cells(currentrow, 31)
txtTruckNo.Text = Cells(currentrow, 32)
txtTrailerNo.Text = Cells(currentrow, 33)
txtTransporter.Text = Cells(currentrow, 34)
cmbTransportMode.Text = Cells(currentrow, 35)
txtPOnumber.Text = Cells(currentrow, 36)
cmbCancelR.Text = Cells(currentrow, 37)
txtCrRef.Text = Cells(currentrow, 38)
End If
Next currentrow
End Sub

0 个答案:

没有答案