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

VBA name already searched

0

Hello! I have the following code. When I search a value, it returns me its data in a new sheet. How can I make it to, if I search a name that has already been searched, will give me a message box saying it already exists? Also, it would be nice if it would send me at the sheet where the name has been searched before, but not necessary important. I need it to give me a message box, so that I know it has been already searched, and not create a new empty sheet.

Answer
Discuss

Discussion

Squishy- this is the issue I raised with you yesterday but you didn't understand!
John_Ru (rep: 6142) Dec 7, '21 at 6:24 am
Also, in general your question needs to be full (and not rely on contributors opening the file to understand what your question is)
.
John_Ru (rep: 6142) Dec 7, '21 at 6:25 am
yeah :(
Squishy (rep: 18) Dec 7, '21 at 6:26 am
See Answer. Out of interest, if you can, please say what types of signals this relates to (in your industry)
John_Ru (rep: 6142) Dec 7, '21 at 6:52 am
Thanks a lot! I work at Hella. I needed this program to make my work less stressfull, and not waste time :D
Squishy (rep: 18) Dec 7, '21 at 7:00 am
Thanks Squishy. I assume it now works good enough to reduce your stress, particularly pre Xmas. 
John_Ru (rep: 6142) Dec 7, '21 at 7:15 am
Yes, it does! Thanks so much for all your help! And if I don't come back soon, merry Christmas!
Squishy (rep: 18) Dec 7, '21 at 7:48 am
Dir auch frohe Weihnachten!
John_Ru (rep: 6142) Dec 7, '21 at 7:58 am
Add to Discussion

Answers

0
Selected Answer

Squishy

To do that (rather than delete the found sheet, as in the current version of the file), the sheet test needs to be done first I think (as in the code extract below).

Here the loop of worksheets is modified so if a match is made, that sheet is displayed and a message box asks if that sheet needs replacing. If not, the macro is quit. If so, the sheet is deleted and the macro continues with the search.

I've added a message at the end (so the user knows something happens (it can be very quick with small files):

Sub Search_Separate_Workbooks()

Dim wbMaster As Workbook
Dim wbSlave As Workbook
Dim filePicker As FileDialog
Dim ws As Worksheet, wbReport As Worksheet, ReportNm As String

On Error Resume Next
ReportNm = Worksheets("Dashboard").Range("F6")

'check for any sheet with same search name

For Each ws In ThisWorkbook.Sheets
    If ws.Name = ReportNm Then
        ws.Activate ' show the sheet if found
        r = MsgBox("Want to replace this result?", vbYesNo, "A sheet with that name exists...")
        If r = vbNo Then ' leave if user wants sheet
            Application.DisplayAlerts = True
            Exit Sub
        End If
' delete so new sheet can replace it
        Application.DisplayAlerts = False 'prevent creation alert
        ws.Delete
        Application.DisplayAlerts = True
    End If
Next ws
.

.

.
Application.ScreenUpdating = True

MsgBox "Created new sheet: " & ReportNm

End Sub
Attached file contains this code.

Hope this fixes things for you.

Discuss


Answer the Question

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