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

Do Loop until (want to use table column name)

0

I want to use the table column name instead of the row number. This code looking for item name code (Table name T_Food   first column (Code #) second column (Item Name) See bold items.

       column Number                               1              2

       Table column name                      Code #    item name 

On Error Resume Next
Set ww = Application.WorksheetFunction
Set Da = Data
Dim sut, s As Long
Dim iRow As Integer
    iRow = 1
    On Error Resume Next   
    txtCode.Text = ""
    'Get Price based on comDepartment selection
    If Me.comDepartment.Value = Data.Range("AE7") Then
      Do
        iRow = iRow + 1
            Loop Until CbSubCategory.Text = Da.Cells(iRow, 2)    'tablename T_Food Column item Name
                txtCode.Text = Da.Cells(iRow, 1)        'tablename T_Food Column Code #
      End If
Answer
Discuss

Discussion

Amin

Again your question isn't explained well enough- we don't have a file so don't really know that the object Da is is lines like:
Da.Cells(....


You're also using Loop Until and there's a danger of an infinite loop(is the text isn't matched in column 46 of Da).

Please edit your ORIGINAL Question to add detail/ Excel file (in order that Willie or others might have a chance of answering).
John_Ru (rep: 6142) Jan 23, '24 at 5:19 am
I'm sorry     Da= Data (Sheet name)
Amin25 (rep: 16) Jan 23, '24 at 5:47 am
This code is working well but I want to alteration this code to look for a table column name instead row number
Amin25 (rep: 16) Jan 23, '24 at 5:56 am
  'here is looking for the Item name 
Loop Until ComboBox1.Text = Da.Cells(iRow, 46)   
   'here is for item code            
TextBox1.Text = Da.Cells(iRow, 45)   
Amin25 (rep: 16) Jan 23, '24 at 6:00 am
Are you there?
Amin25 (rep: 16) Jan 24, '24 at 7:54 am
Yes but I told you I was busy this week. Also you didn't do what I asked above (and I note that Willie hasn't replied, possibly because he doesn't see the question fully)
John_Ru (rep: 6142) Jan 24, '24 at 8:09 am
Ok thank you 
Amin25 (rep: 16) Jan 24, '24 at 8:11 am
Are you still busy?
Amin25 (rep: 16) Jan 29, '24 at 1:07 am
Amin. I may have some time today but please attach a file to your Question do I can check. 
John_Ru (rep: 6142) Jan 29, '24 at 1:53 am
Ok
Amin25 (rep: 16) Jan 29, '24 at 2:30 am
Amin. I have some free time soon so please attach a file (or it will have to wait until tomorrow). 
John_Ru (rep: 6142) Jan 29, '24 at 6:39 am
I attached the file. 
Amin25 (rep: 16) Jan 29, '24 at 7:55 am
Add to Discussion

Answers

0
Selected Answer

Hi again Amin

Now you've attached the file I see it includes an implied request:

I need here code to delete selected item in the list box and the sheet also depends on the department Name( Food, Beverage etc)

but this is a Q&A Forum really (though we might give a lot more help for your first question or two) so I'll give you hints on your question instead...

Your Data worksheet (object Da in your code) has 10 tables which can be addressed by name. e.g. 

Da.ListObjects("T_Food").HeaderRowRange(1,2)

refers to the second column of that table, i.e. the heading "Item Name".

When you're deleting table rows, you can refer to rows like:

Data.ListObjects("T_Food").DataBodyRange.Rows(2)

which is the second data item and the range A8:H8.

Within that, you can use:

Data.ListObjects("T_Food").DataBodyRange.Cells(2,2)

which is the second column in row 2 so the subcategory "Ground Beef".

You can then read properties or use methods to manipulate table elements. 

Hope this is enough help to get you going since I'll be busy with family issues for tomorrow at least.

Revision 01 February 2024

In the FIRST attached file, I've added this code behind the Delete button of your UserForm 

Private Sub CbDelete_Click()

    Dim n As Long, Deltd As String

    On Error Resume Next

    Set Da = Data
    ' check UF has been completed
    If comDepartment.Text = "" Or CbSubCategory.Text = "" Or txtItemName.Text = "" Then
        MsgBox "Please complete entries"
        Exit Sub
    End If

    ' search in Department table (starting "T_"), looking for Item
    With Da.ListObjects("T_" & comDepartment).ListColumns("Item Name").DataBodyRange
        ' loop down contents
        For n = 1 To .Count
            ' if row matches (and Sub Category is same)...
            If txtItemName.Text = .Cells(n) And CbSubCategory.Text = .Cells(n).Offset(0, 6) Then
                ' delete and shift lower rows up
                .Cells(n).Offset(0, -1).Resize(1, 8).Delete Shift:=xlShiftUp
                Deltd = txtItemName.Text
                ' update ListBox
                Call CbSubCategory_Change
                ' tell user
                MsgBox "Deleted entry for " & Deltd
                Exit For
            End If
        Next n
    End With

End Sub

The ListBox gets updated using the cbSubCategory_Change module but for some reason it doesn't limit to just the chosen values- I used Shisha / Charcoal to test). I don't have time to fix your cbSubCategory_Change code to use table names but you should have enough information to do so yourself.

Revision #2 01 February 2024

The SECOND file attached corrects the ListBox Update with this code, with additions in bold:

Private Sub CbDelete_Click()

    Dim n As Long, Deltd As String

    On Error Resume Next

    Set Da = Data
    ' check UF has been completed
    If comDepartment.Text = "" Or CbSubCategory.Text = "" Or txtItemName.Text = "" Then
        MsgBox "Please complete entries"
        Exit Sub
    End If

    ' search in Department table (starting "T_"), looking for Item
    With Da.ListObjects("T_" & comDepartment).ListColumns("Item Name").DataBodyRange
        ' loop down contents
        For n = 1 To .Count
            ' if row matches (and Sub Category is same)...
            If txtItemName.Text = .Cells(n) And CbSubCategory.Text = .Cells(n).Offset(0, 6) Then
                'disable Dept change
                UFEvents = False
                ' delete and shift lower rows up
                .Cells(n).Offset(0, -1).Resize(1, 8).Delete Shift:=xlShiftUp
                Deltd = txtItemName.Text
                ' update ListBox
                Call CbSubCategory_Change
                ' tell user
                MsgBox "Deleted entry for " & Deltd
                UFEvents = True
                Exit For
            End If
        Next n
    End With

End Sub

This is becasue the comDepartment_Change was being triggered. I had to declare a variable at the top of the UserForm module as follows:

Dim UFEvents As Boolean

then use this to cut short that module with a new line (in bold): 

Private Sub comDepartment_Change()
    On Error Resume Next
    If UFEvents = False Then Exit Sub
    << existing code >>

Hope this answers your stated question above if so please remember to mark this Answer as Selected.

Discuss

Discussion

I tried but it's doesn't working, but it's okay, just take care of your issues first, and when you are free will try to solve my problem with this sheet file, Thank you so much
Amin25 (rep: 16) Jan 30, '24 at 3:43 am
Are you free or still busy?
Amin25 (rep: 16) Feb 1, '24 at 6:41 am
Amin. I'm surprised that you've made no progress on this (did you try?) but I have a little time this afternoon. Just looked at your file again and a note says "... I need here code to delete selected item in the list box and the sheet also depends on the department Name( Food, Beverage etc)". Do you intend to have separate sheets per Department or did you mean that the item should be deleted on BOTH the UF ListBox and the Data sheet?  
John_Ru (rep: 6142) Feb 1, '24 at 8:05 am
I mean the item should be deleted on BOTH the UF ListBox and the Data sheet (for example in the UF if I want to delete item from food or beverage etc. so will delete it from UF and Data sheet table name
Amin25 (rep: 16) Feb 1, '24 at 9:22 am
Amin. See revision to Answer and file.
John_Ru (rep: 6142) Feb 1, '24 at 10:55 am
Mr.John_Ru for the  Delete button is working well. and for UF  frmCode not yet (want to exchange the column number to the table column name 
How to do it?

Amin25 (rep: 16) Feb 1, '24 at 12:35 pm
Amin. Please see Revision #2 01 February 2024 and the SECOND file.

The code shows how to use the header name from the table which is what I thought you meant.

If you just want to call a column by its letter, use
Columns("D")

instead of Columns(4) for example.

I have no more time to spend on this.
John_Ru (rep: 6142) Feb 1, '24 at 12:47 pm
Other than to say if you did mean table names and column headings, look at the line:
    With Da.ListObjects("T_" & comDepartment).ListColumns("Item Name").DataBodyRange
John_Ru (rep: 6142) Feb 1, '24 at 12:57 pm
in this code, txtCode is looking for CbSubCategory (for example if CbSubCategory vale is chicken breast then  txtCode  will give me this code item so I want to change the column number to looking for the table name 
bold number
On Error Resume Next
Set ww = Application.WorksheetFunction
Set Da = Data
Dim sut, s As Long
Dim iRow As Integer
    iRow = 1
    On Error Resume Next   
    txtCode.Text = ""
   
    If Me.comDepartment.Value = Data.Range("AE7") Then  ' for example = Food
      Do
        iRow = iRow + 1
            Loop Until CbSubCategory.Text = Da.Cells(iRow, 2)     item Name
                txtCode.Text = Da.Cells(iRow, 1)       Code # 
Amin25 (rep: 16) Feb 1, '24 at 1:43 pm
and in this code need also to look for table name instead of the bold letter in code

If Me.comDepartment.Value = Data.Range("AE7") Then
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'HERE Want to replace row number and column number with column table               'name like (T_Food[Item Name])
    For sut = 7 To Data.Cells(Rows.Count, 2).End(xlUp).Row
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    deg1 = Data.Range("H" & sut)       'here also replacing the latter H to (T_Food[Dep])
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    deg2 = CbSubCategory.Value
    
    'TextBox1.Text = duzenle(TextBox1.Text)
    If UCase(deg1) Like UCase(deg2) & "*" Then
    LB_01.AddItem
    LB_01.List(s, 0) = Data.Range("A" & sut)           'Here also
    LB_01.List(s, 1) = Data.Range("B" & sut)
    LB_01.List(s, 2) = Data.Range("C" & sut)
   
    
    s = s + 1
    
Amin25 (rep: 16) Feb 1, '24 at 1:44 pm
Amin. Sorry but I don't have time to fix all of your code.

I've answered your question, given you working code for the Delete button and given you clues for other procedures but you have to put some effort to fix your (perceived) problems. I have other demands on my spare time, not least my family. 

I suggest you read my answer fully and try to work it out (and ask a new question if you get stuck).

Before that, please mark my Answer as Selected (in accordance with the Rules of the Forum - see the blue hyperlink above) 
John_Ru (rep: 6142) Feb 1, '24 at 2:44 pm
Thanks for selecting my Answer, Amin. Good luck on adjusting your code (though for practical use it may not need altering) 
John_Ru (rep: 6142) Feb 1, '24 at 4:19 pm
I just need to alter the codes to look for table names instead of rows or columns because If I need to insert a column before the table so no need to go back to the code to change the column number or anything like that, that's what I want 
Amin25 (rep: 16) Feb 2, '24 at 10:10 am
I already showed you how to do that (and told you that I no more time to spare for this; you need to try and come back if you have problems ). 
John_Ru (rep: 6142) Feb 2, '24 at 2:10 pm
I tried already many times with this code it start to show me from the second row of the table for example with CbSubCategory if I choose the first item txtcode does not show me the item code if a second item, etc. the txtcode shows me the code 
Private Sub CbSubCategory_Change()
 On Error Resume Next
    
    Dim Da As Worksheet
    Set Da = ThisWorkbook.Sheets("Data")
    
    Dim iRow As Integer
    iRow = 1
    
    txtCode.Text = ""
    
    If Me.comDepartment.Value = Da.Range("AE7").Value Then
        Do
            iRow = iRow + 1
           Loop Until CbSubCategory.Text = Da.ListObjects("T_Food").DataBodyRange.Cells(iRow, 2).Value
        
           txtCode.Text = Da.ListObjects("T_Food").DataBodyRange.Cells(iRow, 1).Value
         
            If iRow > Da.ListObjects("T_Food").ListRows.Count Then
        
            MsgBox ("Item not found: {iRow}"), vbExclamation
        
    End If
        
    End If
 
End Sub
Amin25 (rep: 16) Feb 2, '24 at 3:18 pm
Add to Discussion


Answer the Question

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