Verify that the workbook name exists and copy the data

sofas

Active Member
Joined
Sep 11, 2022
Messages
469
Office Version
  1. 2019
Platform
  1. Windows
Welcome.
The code works fine for me when I create the workbook for the first time. However, when the workbook is found, the data is copied over the previous data
Please help in completing it so that I can copy the data below the previous one

VBA Code:
Function CheckForExistingWorkbooks() As Workbook
Dim wb1 As Workbook
Dim thisWb As Workbook
Set thisWb = ThisWorkbook

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim filename As String
filename = Range("d2").Value

lastrow = thisWb.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row

    Dim TestStr As String, FilePath As String
    FilePath = ThisWorkbook.Path & filename & ".xlsx"
    If Len(Dir(FilePath)) = 0 Then
        Set wb1 = Workbooks.Add
        
        
thisWb.Sheets(1).Range("A1:M" & lastrow).Copy
        
        With wb1.Worksheets(1).Range("A1")
        .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
        .PasteSpecial Paste:=xlPasteFormats
        .PasteSpecial Paste:=xlPasteColumnWidths
     End With
        
  
wb1.SaveAs ThisWorkbook.Path & "\data\" & filename & ".xlsx", FileFormat:=51
    wb1.Close
    
    Else
    
    
    Set wb1 = Workbooks.Open(FilePath)
    
'    When you verify that the file already exists, the data
'    is copied into the same worksheet below the previous data

wb1.SaveAs ThisWorkbook.Path & "\data\" & filename & ".xlsx", FileFormat:=51
  
    wb1.Close
    Set CheckForExistingWorkbooks = wb1
    End If
    
End Function
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
How about this?

VBA Code:
Function CheckForExistingWorkbooks() As Workbook
    Dim wb1 As Workbook
    Dim thisWb As Workbook
    Set thisWb = ThisWorkbook

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim filename As String
    filename = thisWb.Sheets(1).Range("D2").Value

    Dim lastrow As Long
    lastrow = thisWb.Sheets(1).Cells(thisWb.Sheets(1).Rows.Count, 1).End(xlUp).Row

    Dim FilePath As String
    FilePath = ThisWorkbook.Path & "\data\" & filename & ".xlsx"

    If Len(Dir(FilePath)) = 0 Then
        Set wb1 = Workbooks.Add

        thisWb.Sheets(1).Range("A1:M" & lastrow).Copy

        With wb1.Sheets(1).Range("A1")
            .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
            .PasteSpecial Paste:=xlPasteFormats
            .PasteSpecial Paste:=xlPasteColumnWidths
        End With

        wb1.SaveAs ThisWorkbook.Path & "\data\" & filename & ".xlsx", FileFormat:=51
        wb1.Close
    Else
        Set wb1 = Workbooks.Open(FilePath)

        Dim lastUsedRow As Long
        lastUsedRow = wb1.Sheets(1).Cells(wb1.Sheets(1).Rows.Count, 1).End(xlUp).Row

        Dim pasteRow As Long
        pasteRow = lastUsedRow + 1

        thisWb.Sheets(1).Range("A1:M" & lastrow).Copy
        wb1.Sheets(1).Cells(pasteRow, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
        wb1.Sheets(1).Cells(pasteRow, 1).PasteSpecial Paste:=xlPasteFormats
        wb1.Sheets(1).Cells(pasteRow, 1).PasteSpecial Paste:=xlPasteColumnWidths

        wb1.SaveAs ThisWorkbook.Path & "\data\" & filename & ".xlsx", FileFormat:=51

        wb1.Close
        Set CheckForExistingWorkbooks = wb1
    End If

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Function
 
Upvote 0
Solution
How about this?

VBA Code:
Function CheckForExistingWorkbooks() As Workbook
    Dim wb1 As Workbook
    Dim thisWb As Workbook
    Set thisWb = ThisWorkbook

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim filename As String
    filename = thisWb.Sheets(1).Range("D2").Value

    Dim lastrow As Long
    lastrow = thisWb.Sheets(1).Cells(thisWb.Sheets(1).Rows.Count, 1).End(xlUp).Row

    Dim FilePath As String
    FilePath = ThisWorkbook.Path & "\data\" & filename & ".xlsx"

    If Len(Dir(FilePath)) = 0 Then
        Set wb1 = Workbooks.Add

        thisWb.Sheets(1).Range("A1:M" & lastrow).Copy

        With wb1.Sheets(1).Range("A1")
            .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
            .PasteSpecial Paste:=xlPasteFormats
            .PasteSpecial Paste:=xlPasteColumnWidths
        End With

        wb1.SaveAs ThisWorkbook.Path & "\data\" & filename & ".xlsx", FileFormat:=51
        wb1.Close
    Else
        Set wb1 = Workbooks.Open(FilePath)

        Dim lastUsedRow As Long
        lastUsedRow = wb1.Sheets(1).Cells(wb1.Sheets(1).Rows.Count, 1).End(xlUp).Row

        Dim pasteRow As Long
        pasteRow = lastUsedRow + 1

        thisWb.Sheets(1).Range("A1:M" & lastrow).Copy
        wb1.Sheets(1).Cells(pasteRow, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
        wb1.Sheets(1).Cells(pasteRow, 1).PasteSpecial Paste:=xlPasteFormats
        wb1.Sheets(1).Cells(pasteRow, 1).PasteSpecial Paste:=xlPasteColumnWidths

        wb1.SaveAs ThisWorkbook.Path & "\data\" & filename & ".xlsx", FileFormat:=51

        wb1.Close
        Set CheckForExistingWorkbooks = wb1
    End If

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Function
Wonderful...thank you very much. This is what is really needed.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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