Sub that runs through worksheets and reports an input value on another worksheet

Dstars21

New Member
Joined
Oct 5, 2019
Messages
8
I'm currently trying to create a sub that does the following:

1 - asks for an input (a string/name in this case)

2- loops through worksheets to see if their name is there, and if it is, reports their name and the "amount sold" in the column next to their name onto another worksheet.

what i have so far (i get an error with the current range i have set):

Code:
[FONT=verdana]Sub question3()


Dim ws As Worksheet
Dim emprng As Range
Dim I As Integer
Dim namesearch As String


    For Each ws In Worksheets
        Set emprng = ws.Range(Range("A2"), Range("A2").End(xlDown))
            If ws.CodeName <> "Sheet1" Then
                    namesearch = InputBox("Please enter the name of the employee you want to find.")
                        For I = 1 To emprng.Rows.Count
                            If emprng.Cells(I, 1) = namesearch Then
                                ' Report the name found onto another worksheet
                                Else
                                MsgBox ("cannot find that person in the data.")
                            End If
                        Next I
            End If

    Next ws

End Sub[/FONT]
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
this uses a different method to find the value than your code, but it should be a little faster since it eliminates the second For loop. Also in the comment you will see an example of how to put the found value on the destination worksheet.

Code:
Sub question3()
Dim ws As Worksheet
Dim emprng As Range
Dim I As Integer
Dim namesearch As String, fn As Range
    For Each ws In Worksheets
        If ws.CodeName <> "Sheet1" Then
            namesearch = InputBox("Please enter the name of the employee you want to find.")
                Set fn = ws.Range("A:A").Find(namesearch, , xlValues, xlWhole)
                    If Not fn Is Nothing Then
                        ' Report the name found onto another worksheet [COLOR=#008000]destSheet.cells(Rows.Count, 1).End(xlUp)(2) = fn.Value[/COLOR]
                    Else
                        MsgBox ("cannot find that person in the data.")
                    End If
            End If
    Next ws
End Sub
 
Upvote 0
Thanks for the help! How would i get it to also report the 2 values in the columns next to the seached name?
 
Upvote 0
Thanks for the help! How would i get it to also report the 2 values in the columns next to the seached name?

Code:
[COLOR=#008000]destSheet.cells(Rows.Count, 1).End(xlUp)(2).Resize(, 3) = fn.Resize(, 3).Value[/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,495
Messages
6,113,992
Members
448,538
Latest member
alex78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top