Results 1 to 2 of 2

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

Threaded View

  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.

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