Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Looking for code mention column table instead sheet column

0

I want to mention the table name and table column

Private Sub cmdAdd_Click()
   Dim cellrange As String
  Dim rowno As Integer
   Set ss = Data
   Set ww = Application.WorksheetFunction
If comCategory.Text = "" Or txtSubCategory.Text = "" Then
  Beep
MsgBox " Please input the data  ", vbInformation, "Inventory Program"
Exit Sub
End If
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
 With ss
 If Me.comCategory = Data.Range("B7") Then
 Data.Activate
 Set rng = ActiveSheet.ListObjects("T_Food").Range
Dim lastrow As Long
lastrow = rng.Find(What:="*", _
After:=rng.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
''================================================================
     'HERE I WANT TO KNOW IS THAT IS COORECT 
   fc = ww.CountIf(ss.Range("T_Food[[Sub Category]]"), Me.txtSubCategory.Text)
   If fc >= 1 Then
    amedia = MsgBox("This name already exist,Please choose other name ", vbCritical, "Inventory Program")
    Exit Sub
    End If

   'AND HERE THE ONE I WANT TO MENTION TO TABLE NAME INSTEAD OF "D"
       rng.Parent.Cells(lastrow + 1, "D").Value = Me.txtSubCategory.Text  'CODE

   End If

 Me.Label9.Visible = True
 MsgBox " It has successfully added"

 Me.comCategory = ""
 Me.txtSubCategory = ""


 End With
 Application.ScreenUpdating = True

End Sub
Answer
Discuss

Discussion

Hi Amin25

It's late here but what's your question please?
John_Ru (rep: 6142) Jan 19, '24 at 6:05 pm
I want this code its mention the table column name instead of sheet column D

'AND HERE THE ONE I WANT TO MENTION TO TABLE NAME INSTEAD OF  "D"
       rng.Parent.Cells(lastrow + 1, "D").Value = Me.txtSubCategory.Text  'CODE
Amin25 (rep: 16) Jan 20, '24 at 1:08 am
Add to Discussion

Answers

0
Selected Answer

Hi again Amin

In the attached revised file, I've added empty tables (with effectievly no data rows) for your other categories, each name starting with "T_" e.g. "T_Beverage" (and made the header row of each table coloured, like in the T_Department table now).

I've then replaced a chunk of your code with some to run through the rows of whichever Department is chosen (see changes and comments in bold below):

Private Sub cmdAdd_Click()
   Dim NewSub As String

   Set ss = Data
   ' give shorter name the chosen subcategory
   NewSub = txtSubCategory.Text

    If comCategory.Text = "" Or txtSubCategory.Text = "" Then
        Beep
        MsgBox " Please input the data  ", vbInformation, "Inventory Program"
        Exit Sub
    End If

    Application.ScreenUpdating = False
    'Application.DisplayAlerts = False

    ' get the chosen category table data
    With ss.ListObjects("T_" & comCategory.Text)

        If .ListRows.Count = 0 Then
            'if first entry is blank, add row and value
                With .ListRows.Add
                    .Range = NewSub
                End With

            Else
            ' otherwise loop to check if value exists
            For n = 1 To .ListRows.Count
                If .ListRows(n).Range.Value = NewSub Then
                    ' if it does, say then quit
                    MsgBox NewSub & " already exists"
                    Exit Sub
                End If
            Next n
            ' otherwise, add to end
            With .ListRows.Add
                    .Range = NewSub
            End With
        End If
    End With
    ' make change visible before you say what's been done
    Application.ScreenUpdating = True
    'Application.DisplayAlerts = False

    Me.Label9.Visible = True
    MsgBox NewSub & " has been successfully added"

    Me.comCategory = ""
    Me.txtSubCategory = ""

End Sub

Note that if a user edits the Category value in the UserForm then an error will arise (since that named table won't exist).

Revision 21 January 2024

Regarding your Discussion point "I tried to edit item name and after I clicked update its gave me a message 'run time error 9 subscript out of the range' " below, the revised file addresses that too.

That error  arises since (when you chnage the Item Name. functions are triggered and try to loop through looking for tables with the names defined in the array allTables. That included "T_Other" but that table doesn't currently exist so I commented it out, changes in bold in the code extract below:

Private Sub CmdUpdate_Click()

 Const PW As String = "8521" 'using const for fixed values
    Dim allTables(), lo As ListObject, rw As ListRow, tName

     allTables = Array("T_Food", "T_Beverage", "T_Shisha") ', "T_Other") - missing table omitted

Now you can pick an item in the ListBox, change the item name and the table entry will be changed when you click the "Update" button.

Hope this fixes your problem and makes sense. if so, please remember to mark this Answer as Selected.

Discuss

Discussion

Thank you
Amin25 (rep: 16) Jan 20, '24 at 12:13 pm
Mr john for this code if I want to modify the item what is the code to do for it to mention tables name same as the one you 
Amin25 (rep: 16) Jan 21, '24 at 4:03 am
Amin. Sorry but I don't understand your point, especially the part "mention tables name same as the one you". 

The modified code already allows you to change things about the Item, like the Cost and Sales values (and they are written to the Data sheet and UF ListBox). .
John_Ru (rep: 6142) Jan 21, '24 at 5:57 am
I mean this code if I want to add a command button to modify an item for example Poultry will change to Poultry1.  How will the code be?
Amin25 (rep: 16) Jan 21, '24 at 6:45 am
Amin, see my "Revision 21 January 2024" to my Answer, plus the revised file. That's done with the existing code.
John_Ru (rep: 6142) Jan 21, '24 at 8:43 am
Thank you so much, you're great 
Amin25 (rep: 16) Jan 21, '24 at 9:46 am
Thanks for your kind comment Amin.

I may not have time tomorrow (or next week) to sort the answers but at least you have working code in both cases hopefully 
John_Ru (rep: 6142) Jan 21, '24 at 10:21 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login