VBA, update main worksheet with new data ?

Mr.hankey

New Member
Joined
Feb 23, 2009
Messages
17
Hi all ! I'm trying to merge all my sheet in one , and make automatically updatable using a VBA code .
I found this code , it merges all sheets into one "Target" sheet , but deletes the old "Target"sheet. the thing is if I add funtions in the Target sheet , I'll lost them if I run the VBA code to merge the sheets a second time.
Code:
Sub CombineSheets() 

    'This macro will copy all rows from the first sheet 
    '(including headers) 
    'and on the next sheets will copy only the data 
    '(starting on row 2) 

    Dim i As Integer 
    Dim j As Long 
    Dim SheetCnt As Integer 
    Dim lstRow1 As Long
    Dim lstRow2 As Long
    Dim lstCol As Integer 
    Dim ws1 As Worksheet 

    With Application 
        .DisplayAlerts = False 
        .EnableEvents = False 
        .ScreenUpdating = False 
    End With 

    On Error Resume Next 

    'Delete the Target Sheet on the document (in case it exists) 
    Sheets("Target").Delete 
    'Count the number of sheets on the Workbook 
    SheetCnt = Worksheets.Count 

    'Add the Target Sheet 
    Sheets.Add after:=Worksheets(SheetCnt) 
    ActiveSheet.Name = "Target" 
    Set ws1 = Sheets("Target") 
    lstRow2 = 1 
    'Define the row where to start copying 
    '(first sheet will be row 1 to include headers) 
    j = 1 

    'Combine the sheets 
    For i = 1 To SheetCnt 
        Worksheets(i).Select 

        'check what is the last column with data 
        lstCol = ActiveSheet.Cells(1, Activesheet.Columns.Count).End(xlToLeft).Column 

        'check what is the last row with data 
        lstRow1 = ActiveSheet.Cells(activesheet.rows.count, "A").End(xlUp).Row

        'Define the range to copy 
        Range("A" & j, Cells(lstRow1, lstCol)).Select 

        'Copy the data 
        Selection.Copy 
        ws1.Range("A" & lstRow2).PasteSpecial 
        Application.CutCopyMode = False 

        'Define the new last row on the Target sheet 
        lstRow2 = ws1.Cells(65536, "A").End(xlUp).Row + 1 

        'Define the row where to start copying 
        '(2nd sheet onwards will be row 2 to only get data) 
        j = 2 
    Next 

    With Application 
        .DisplayAlerts = True 
        .EnableEvents = True 
        .ScreenUpdating = True 
    End With 

    Sheets("Target").Select 
    Cells.EntireColumn.AutoFit 
    Range("A1").Select 

End Sub

I'm trying to change it , but my skills in VBA are very limited and every time I change something it doesn't work . what I want to do , is make it instead of deleting the old "Target" sheet and making a new one , update the already existing one with all sheets data (including new sheets) and then delete duplicated lines .
any suggestion help is appreciated .
sorry for my english ^^" .
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
news , I found a code here in the forums that deletes duplicates . now I only need a way to change the code and make update the existing Target worksheet with all the worksheets data instead of deleting it and creating a new one .

I'm also trying to combine the codes , the previous one with this one (that deletes duplicate rows) :
Code:
Sub DelDupes()
Dim r   As Long

Application.ScreenUpdating = 0
Columns("a:b").Insert
r = Range("c" & Rows.Count).End(xlUp).Row
Range("b2:b" & r).FormulaR1C1 = "=RC[1]&""|""&RC[2]&""|""&RC[3]"
Range("a2:a" & r).FormulaR1C1 = "=COUNTIF(R2C2:RC[1],RC[1])=1"
With Range("a1:a" & r)
    .AutoFilter field:=1, Criteria1:="FALSE"
    On Error Resume Next
    .Offset(1).Resize(.Rows.Count, 1).SpecialCells(xlCellTypeVisible). _
        EntireRow.Delete
    On Error GoTo 0
    .AutoFilter
End With
Columns("a:b").Delete
Application.ScreenUpdating = 1
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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