+ Reply to Thread
Results 1 to 4 of 4

Input Box to change Sheet Names used in VBA Code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-27-2014
    Location
    Fresno, USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Input Box to change Sheet Names used in VBA Code

    I have a couple macros that pull in two sheets to a single workbook from different workbooks in a file and compare the two sheets row by row for differences. The problem is that whenever I'm comparing new pairs of sheets i have to change all the sheet references in the vba code. Is there a way to add an input or message box asking for the two new names of the sheets? For example one box would pop up and say, "Please enter the original sheet name" and another that would pop up and say, "Please enter the new sheet name." I think I should be using something like
     Dim North_America_Old as Variant
    and
    North_America_Old = Application.Inputbox("Enter Old Sheet Name")
    . I'm still not sure how associate the input box to the code representing the original sheet or the sheet i want to compare with the original sheet. Addidtionally, is there a way to combine theses macros to as few as possible? Thank you very much for all of your help!

    Sub GetSourceSheets()
    'This macro will loop through excel files
    'in a location and copy the their worksheets into the current workbook.
    'Instructions: Replace the file path, which starts on the 8th line, with a file path to the folder
    'that contains the two vendor site lists that you wish to compare.
    '!!!! Do not for get to place the back slash (\) at the end of the file path. !!!! End of Instructions
    Application.DisplayAlerts = False
    Path = "C:\Users\turner\Desktop\Excel_Con\Kevin\NA_Vendor\"
    Filename = Dir(Path & "*.xls")
    Do While Filename <> ""
        Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
        For Each Sheet In ActiveWorkbook.Sheets
        Sheet.Copy After:=ThisWorkbook.Sheets(1)
        Next Sheet
        Workbooks(Filename).Close
        Filename = Dir()
    Loop
    Application.DisplayAlerts = True
    End Sub
    
    Sub RunCompare()
    'Instructions: Replace North_American_Old with the original vendor site list sheet name and
    'replace North_American_New with the new vendor site list sheet name you wish
    'to compare to the original vendor site list sheet.
    '!!!!! Keep sheet names enclosed in quotations !!!! End of Instructions
    Call compareSheets("North_America_Old", "North_America_New")
    
    End Sub
    
    
    Sub compareSheets(shtNorth_America_Old As String, shtNorth_America_New As String)
    'Instructions: Replace North_American_Old with the original vendor site list sheet name and
    'replace North_American_New with the new vendor site list sheet name you wish
    'to compare to the original vendor site list sheet.
    '!!!!! Keep sheet names enclosed in quotations and remember to keep "sht" at the beginning of the sheet name!!!!
    'End of Instructions
    Dim result As Variant
    Dim mycell As Range
    Dim mydiffs As Integer
    
    'For each cell in sheet2 that is not the same in Sheet1, color it yellow
    For Each mycell In ActiveWorkbook.Worksheets(shtNorth_America_New).UsedRange
        If Not mycell.Value = ActiveWorkbook.Worksheets(shtNorth_America_Old).Cells(mycell.Row, mycell.Column).Value Then
            
            mycell.Interior.Color = vbRed
            mydiffs = mydiffs + 1
            
        End If
    Next
    
    'Display a message box to demonstrate the differences
    MsgBox mydiffs & " differences found", vbInformation
    
    ActiveWorkbook.Sheets(shtNorth_America_New).Select
    
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Input Box to change Sheet Names used in VBA Code

    set wsOld  = thisworkbook.worksheets(inputbox("Enter Old"))
    set wsNew  = thisworkbook.worksheets(inputbox("Enter New"))
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    01-27-2014
    Location
    Fresno, USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Input Box to change Sheet Names used in VBA Code

    My apologies for a simple question but in which macro, RunCompare or compareSheets, should I add this code? After trying to incorporate the code I keep getting a run time error 9 at this line
    For Each mycell In ActiveWorkbook.Worksheets(shtNorth_America_New).UsedRange
    . Thanks again.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Input Box to change Sheet Names used in VBA Code

    is that subscript out of range? Do you have a sheet called what you have entered?

    I presume in this one, without knowing what you are doing fully.

    Sub RunCompare()
    'Instructions: Replace North_American_Old with the original vendor site list sheet name and
    'replace North_American_New with the new vendor site list sheet name you wish
    'to compare to the original vendor site list sheet.
    '!!!!! Keep sheet names enclosed in quotations !!!! End of Instructions
    Call compareSheets(HERE, AND HERE)
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change VBA code using input box
    By LLL0422 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-28-2014, 03:35 PM
  2. Replies: 6
    Last Post: 11-05-2012, 10:04 AM
  3. excel, changing 2 sheet names by input on one tab?
    By qcdorum in forum Excel General
    Replies: 0
    Last Post: 07-04-2011, 08:38 AM
  4. Popup an input request, listbox of sheet names?
    By jaytea in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-29-2009, 08:29 AM
  5. Doing away with an Input box - how to change this code?
    By HankMcSpank in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2009, 09:22 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1