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

match sheet name with combobox to populate amount in textbox

0

Hello

I have sheets names like ALIAA sheet and I will add as the same thing. in first part of the code is ok but in this part

 SrcSht = UCase(Trim(Me.ComboBox1.Value))
    LastRow = Evaluate("IsRef(" & SrcSht & "!A1)")


    Set F = LastRow.Find(ComboBox1.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious)
    If Not F Is Nothing Then
       Me.TextBox1.Value = LastRow.Cells(F.Row, "E")
       End If

is really problem after I match names in combobox with sheets names .I need match names in combobox with sheet name like ALIAA, ..... sheets and populate last amount in TEXTBOX1  from column E .

any way to do that when match comobox with sheet name?

thanks

Answer
Discuss

Answers

0
Selected Answer

Hi again Abdo

Firstly, your file uses the named range "NAMES" (in column J of worksheet "CUSTOMERS") to set the RowSource property of  ComboBox1. That can lead to list values which do not exist in the file.

Accordingly, I've removed that property and added this code to get the names of only those sheets which do exist:

Private Sub UserForm_Initialize()

    Dim ws As Worksheet
    ' loop through sheets
    For Each ws In ThisWorkbook.Worksheets
        ' unless it's the named sheet below...
        If ws.Name <> "CUSTOMERS" Then
            ' add sheet name to ComboBox1
            Me.ComboBox1.AddItem ws.Name
        End If
    Next ws

End Sub

While that should reduce errors, a user could alter a chosen value (e,g, add a number to the end) so I've still tested for that in the revised code below (commented to help):

Private Sub ComboBox1_Change()
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim wsExists As Variant

    ' check value has not been editted
    On Error Resume Next
    wsExists = Evaluate("IsRef(" & Me.ComboBox1.Value & "!A1)")

    If wsExists <> True Then
        ' if so, give message in TB1, make yellow and quit
        Me.TextBox1.Value = "Pick a valid sheet"
        Me.TextBox1.BackColor = vbYellow
        Exit Sub

        Else
        Me.TextBox1.BackColor = vbWhite
    End If

    ' if not, get value from row E of chosen sheet
    Set ws = Worksheets(Me.ComboBox1.Value)
    LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    Me.TextBox1.Value = ws.Cells(LastRow, "E")

End Sub

That code is in the revised file attached.

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

Discuss

Discussion

That works well !
many thanks Jon.
Abdo M (rep: 22) Apr 28, '25 at 4:58 am
Great! Thanks for selecting my Answer, Abdo.

p.s  I editted my Answer just to correct some typos. 
John_Ru (rep: 6722) Apr 28, '25 at 6:27 am
Add to Discussion


Answer the Question

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