excel 在特定表中添加和编辑数据时,请参考表名而不是列

ego6inou  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(108)

我有一个工作表,有4个表,我使用用户表单输入数据到每个表,但我需要知道如何编辑数据在特定的表不指列A或B……我想指的是表名和标题名称的表
此示例用于将数据添加到表中

Dim cellrange As String
Dim rowno As Integer

Dim AddFood As ListObject
Dim AddBev As ListObject
Dim AddOther As ListObject

Set AddFood = Data.ListObjects("T_Food")
Set AddBev = Data.ListObjects("T_Beverage")
Set AddOther = Data.ListObjects("T_Other")
Dim AddedRow As ListRow

If txtCode.Text = "" Or comDepartment.Text = "" Or txtItemName.Text = "" Or comUnit.Text = "" _
    Or txtPrice.Text = "" Or TxtSalePrice.Text = "" Then
      Beep
   MsgBox " Please Enter Data "
Exit Sub

如果结束

Application.ScreenUpdating = False  
 Application.DisplayAlerts = False

  '''''''''''''''''''''''                                                             

 Set Da = Data 
 Set ww = Application.WorksheetFunction
With Da
     If Me.comDepartment = "Food" Then
  cname = Me.txtItemName.Text
  fc = ww.CountIf(Data.Range("T_Food[[Item Name]]"), cname)
   If fc >= 1 Then
   amedia = MsgBox("  This name already exists  ", vbCritical, "Inventory Program")
 Exit Sub

如果结束

Set AddedRow = AddFood.ListRows.Add()
  With AddedRow
       AddedRow.Range(1) = Me.txtCode.Text    'Code
       AddedRow.Range(2) = Me.txtItemName.Text  'name
       AddedRow.Range(3) = Me.txtCode.Text
       AddedRow.Range(4) = Me.comUnit.Text         'Unit
       AddedRow.Range(5) = Me.txtPrice.Text        'CostPrice
       AddedRow.Range(6) = Me.TxtSalePrice.Text
       AddedRow.Range(8) = Me.comDepartment.Text  'Department
kgsdhlau

kgsdhlau1#

任何Excel行的任何单元格(Range)(即使是ListRow)都有Row属性,该属性为1,Column属性应递增以获得以下属性。因此,请尝试理解下一个代码并应用其含义:

Dim Data As Worksheet, addFood As ListObject, AddedRow As listRow, strExisting As String
  
  Set Data = ActiveSheet 'use here the sheet you need
  Set addFood = Data.ListObjects("T_Food")
  
  Set AddedRow = addFood.ListRows.Add()
  With AddedRow
        If Application.CountIf(addFood.DataBodyRange.Columns(1),  Me.txtCode.Text) = 0 Then
            .Range(1, 1) = Me.txtCode.Text
        Else
            strExisting = strExisting & vbCrLf & Me.txtCode.Text
        End If
        If Application.CountIf(addFood.DataBodyRange.Columns(2), Me.txtItemName.Text) = 0 Then
            .Range(1, 2) = Me.txtItemName.Text
        Else
            strExisting = strExisting & vbCrLf & Me.txtItemName.Text
        End If
  End With
  If strExisting <> "" Then MsgBox "The next values already exist in their respective columns:" & vbCrLf & strExisting

编辑

请尝试理解/使用下一种方法来更改表DataBodyRange中的任何值。如果更方便,您可以直接使用该数字,而不是使用其Header提取列号:

Sub ChangingTableRowsElements()
   Dim Data As Worksheet, addFood As ListObject, findCode As Range
   Dim srcCode As String, changeElem As String, arrCols, ColToBeChanged As String, col
   
   srcCode = Me.txtCode.Text 'the code to be searched for changing
   changeElem = "ToBeChanged" 'the new string value to be placed instead existing
   ColToBeChanged = "Price" 'you should place here A REAL COLUMN HEADER
   
   Set Data = ActiveSheet 'use here the necessary sheet
   Set addFood = Data.ListObjects("T_Food")
   arrCols = addFood.HeaderRowRange.Value 'you may skip this code line if you want supplying the column to be changed number.
   
   Set findCode = addFood.DataBodyRange.Columns(1).Find(what:=srcCode, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False) 'find the code to be changed cell
   If Not findCode Is Nothing Then   'the code could be found in the table first column
      col = Application.match(ColToBeChanged, arrCols, 0) 'you may skip this and make col  = 5, meaning the fifth column
      If Not IsError(col) Then
           findCode.Offset(, col - 1).Value = changeElem
      Else
           MsgBox ColToBeChanged & " could not be found between the table headers...": Exit Sub
      End If
   Else
       MsgBox srcCode & " could not be found between the codes in the table first column..."
   End If
End Sub

请在测试后发送一些反馈。

相关问题