通过userform修改excel表中的表格数据

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

在照片中,我用来将数据添加和修改到表中的用户我单击列表框中的任何项目,所有数据将归档所有文本框[enter image description here](https://i.stack.imgur.com/QXJLK.png

但是修改按钮(编辑)代码是指列所以我想让代码引用表名而不是列

这是代码

Private Sub CmdUpdate_Click()

Dim zz As Range
Dim rr As Range
Dim c As Range
Dim old_name, new_name, fname, new_mr, fmr

Dim EditRow As Cells

Set editBev = Data.ListObjects("T_Beverage")


ThisWorkbook.Activate

If Me.comDepartment.Text = "" Or Me.txtItemName.Text = "" Or Me.txtPrice.Text = "" _
  Or Me.comUnit.Text = "" Then
MsgBox "Please enter data  ", vbCritical, "Inventory program "
Exit Sub
End If

Set ww = Application.WorksheetFunction


old_name = Me.TxtEditItem.Value
new_name = Me.txtItemName.Text
'===== no change in name
If new_name = old_name Then GoTo 5

fname = ww.CountIf(Data.Range("T_Food[[Item Name]]"), new_name)
fname = ww.CountIf(Data.Range("T_Beverage[[#All],[Item Name]]"), new_name)
fname = ww.CountIf(Data.Range("T_Other[[#All],[Item Name]]"), new_name)

If fname >= 1 Then: MsgBox "  This name already exists  ", vbCritical, "Inventory Program ": Exit Sub
'==========================

5 Application.ScreenUpdating = False

  Application.Calculation = xlCalculationManual
  
'==change in data table====
With Data
.Unprotect ("0000")

If Me.comDepartment.Value = "Food" Then

For Each c In Data.Range("T_Food[[#All],[Item Name]]") 
If old_name = c Then GoTo 1
Next
1 LastRow = c.Row

.Cells(LastRow, "A").Value = Me.txtCode.Text
.Cells(LastRow, "B").Value = new_name
.Cells(LastRow, "C").Value = Me.txtCode.Text
.Cells(LastRow, "D").Value = Me.comUnit.Text
.Cells(LastRow, "E").Value = Me.txtPrice.Text '
.Cells(LastRow, "F").Value = Me.TxtSalePrice.Text
.Cells(LastRow, "H").Value = Me.comDepartment.Text

.Protect Password:=("0000")

Application.Calculation = xlCalculationAutomatic
Me.TxtEditItem.Value = new_name
End If
If Me.comDepartment.Value = "Beverage" Then

For Each c In Data.Range("T_Beverage[[Item Name]]") 

If old_name = c Then GoTo 2
Next
2 LastRow = c.Row

.Cells(LastRow, "J").Value = Me.txtCode.Text
.Cells(LastRow, "K").Value = new_name
.Cells(LastRow, "K").Value = Me.txtCode.Text
.Cells(LastRow, "M").Value = Me.comUnit.Text
.Cells(LastRow, "N").Value = Me.txtPrice.Text '
.Cells(LastRow, "O").Value = Me.TxtSalePrice.Text
.Cells(LastRow, "Q").Value = Me.comDepartment.Text

.Protect Password:=("0000")

Application.Calculation = xlCalculationAutomatic
Me.TxtEditItem.Value = new_name
End If
excel vba edit
© www.soinside.com 2019 - 2024. All rights reserved.