+ Reply to Thread
Results 1 to 2 of 2

Duplicated sheet's name after re-ran the workbook macro

  1. #1
    Registered User
    Join Date
    04-16-2020
    Location
    toronto
    MS-Off Ver
    10
    Posts
    1

    Duplicated sheet's name after re-ran the workbook macro

    Hi all,

    I am a newbie into VBA. My boss asked me to create a macro which will import all the worksheets from different workbooks into one workbook.

    I created the following 2 scenarios to presented to him,
    Scenario 1; a window will pop up and ask me to select the workbook/s which I want to import to this new workbook, below is the code for it

    #Sub mergeFiles()#
    'Define variables:
    #Dim numberOfFilesChosen, i As Integer#
    #Dim tempFileDialog As FileDialog#
    #Dim mainWorkbook, sourceWorkbook As Workbook#
    #Dim tempWorkSheet As Worksheet##

    #Set mainWorkbook = Application.ActiveWorkbook#
    #Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker) #

    'Allow the user to select multiple workbooks
    #tempFileDialog.AllowMultiSelect = True#

    #numberOfFilesChosen = tempFileDialog.Show#

    'Loop through all selected workbooks
    #For i = 1 To tempFileDialog.SelectedItems.Count#

    'Open each workbook
    #Workbooks.Open tempFileDialog.SelectedItems(i) #

    #Set sourceWorkbook = ActiveWorkbook#

    'Copy each worksheet to the end of the main workbook
    # For Each tempWorkSheet In sourceWorkbook.Worksheets#
    #tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count) #
    #Next tempWorkSheet#

    'Close the source workbook
    #sourceWorkbook.Close#
    #Next i#

    #End Sub#

    Scenario 2, the workbook’s sheets will be pulled automatically from a specified bath and brought them all to the new workbook, below is the code for it

    #Sub CombineWorkbooks()#

    #Dim Path As String#
    #Path = "C:\Users \Desktop\Example\"#

    #Dim FileName As String#
    #FileName = Dir(Path & "*.xlsx")#

    #Dim ws As Worksheet#

    #Application.ScreenUpdating = False#
    #Application.DisplayAlerts = False#

    #Do While FileName <> ""#
    #Workbooks.Open Path & FileName#
    #For Each ws In ActiveWorkbook.Sheets#
    #ws.Copy after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count) #
    #Next ws#
    #Workbooks(FileName).Close#
    #FileName = Dir()#
    #Loop#

    #Application.ScreenUpdating = False#
    #Application.DisplayAlerts = False#

    #End Sub#

    The problem is that every time I ran the macro it pulls the data and put it in a new worksheet.
    What I want is that every time I ran the macro the existing sheets will be updated.

    Thank you so much for your help in advance
    Last edited by hamdan85; 04-20-2020 at 12:48 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,937

    Re: Duplicated sheet's name after re-ran the workbook macro

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ 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. Macro shows duplicated entries but sometimes they are not duplicated due to data.
    By tweacle in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-05-2020, 04:47 PM
  2. Can a macro or something list missing and duplicated data on a seperate sheet.
    By tweacle in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-18-2019, 03:19 PM
  3. Replies: 7
    Last Post: 06-22-2018, 12:07 PM
  4. [SOLVED] Conditional Format - Highlight a Cell in Sheet 2 if Duplicated from Sheet 1
    By sliesneekr in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-26-2014, 09:16 AM
  5. Named formula scoped to workbook are duplicated on worksheet copy.
    By GeneralDisarray in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-27-2014, 02:44 PM
  6. [SOLVED] macro to copy rows from a sheet in one workbook to a sheet in a different workbook...
    By fredderf81 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-16-2013, 01:36 PM
  7. Replies: 1
    Last Post: 07-21-2012, 12:13 PM

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