Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· openaiHugging Face Suzhen/CodeChat

i have two excel files in a folder , one with extension .xlsx and another with extension…

  1. User

    i have two excel files in a folder , one with extension .xlsx and another with extension .xslm , both are opend , we need to copy all data from file .xslx to .xslm and save .xslm file in same folder with name of .xlsx file

  2. Assistant

    To achieve this, you can use VBA macros in Excel. Here's a step-by-step guide to copy data from one Excel file (.xlsx) to another Excel file (.xlsm) and save the .xlsm file with the name of the .xlsx file:

    1. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.

    2. Go to Insert > Module to insert a new module.

    3. Copy and paste the following VBA code into the module:

    Sub CopyDataToAnotherFile()
        Dim sourceWorkbook As Workbook
        Dim destWorkbook As Workbook
        Dim sourceSheet As Worksheet
        Dim destSheet As Worksheet
        Dim sourceFilePath As String
        Dim destFilePath As String
        
        ' Set the file paths for source (.xlsx) and destination (.xlsm) files
        sourceFilePath = "Path_to_source_file.xlsx"
        destFilePath = "Path_to_destination_file.xlsm"
        
        ' Open both workbooks
        Set sourceWorkbook = Workbooks.Open(sourceFilePath)
        Set destWorkbook = Workbooks.Open(destFilePath)
        
        ' Set the source and destination sheets
        Set sourceSheet = sourceWorkbook.Sheets(1) ' Assuming data is on first sheet
        Set destSheet = destWorkbook.Sheets(1) ' Assuming data will be copied to first sheet
        
        ' Copy data from source sheet to destination sheet
        sourceSheet.UsedRange.Copy destSheet.Range("A1")
        
        ' Save the destination workbook with the name of the source workbook
        destWorkbook.SaveAs Replace(destFilePath, ".xlsm", ".xlsx"), FileFormat:=xlOpenXMLWorkbookMacroEnabled
        
        ' Close the workbooks without saving changes
        sourceWorkbook.Close False
        destWorkbook.Close True
        
        MsgBox "Data copied successfully!"
    End Sub
    
    1. Replace "Path_to_source_file.xlsx" and "Path_to_destination_file.xlsm" with the actual file paths of the source (.xlsx) and destination (.xlsm) files.

    2. Run the macro by pressing F5 or by selecting Run > Run Sub/UserForm.

    This VBA macro will copy all data from the source Excel file (.xlsx) to the destination Excel file (.xlsm) and save the destination file with the name of the source file but with the .xlsm extension.

  3. User

    in dont want to write path of files ,make path for all in same folder

  4. Assistant

    If you want the macro to work with files in the same folder where the macro is stored, you can use the following VBA code:

    Sub CopyDataToAnotherFileInSameFolder()
        Dim sourceWorkbook As Workbook
        Dim destWorkbook As Workbook
        Dim sourceSheet As Worksheet
        Dim destSheet As Worksheet
        Dim sourceFilePath As String
        Dim destFilePath As String
        Dim sourceFileName As String
        Dim destFileName As String
        
        ' Get the path of the folder where the macro is stored
        Dim folderPath As String
        folderPath = ThisWorkbook.Path & "\"
        
        ' Get the file names
        sourceFileName = Dir(folderPath & "*.xlsx")
        destFileName = Dir(folderPath & "*.xlsm")
        
        ' Open both workbooks
        Set sourceWorkbook = Workbooks.Open(folderPath & sourceFileName)
        Set destWorkbook = Workbooks.Open(folderPath & destFileName)
        
        ' Set the source and destination sheets
        Set sourceSheet = sourceWorkbook.Sheets(1) ' Assuming data is on first sheet
        Set destSheet = destWorkbook.Sheets(1) ' Assuming data will be copied to first sheet
        
        ' Copy data from source sheet to destination sheet
        sourceSheet.UsedRange.Copy destSheet.Range("A1")
        
        ' Save the destination workbook with the name of the source workbook
        destFilePath = Replace(destWorkbook.FullName, ".xlsm", ".xlsx")
        destWorkbook.SaveAs destFilePath, FileFormat:=xlOpenXMLWorkbookMacroEnabled
        
        ' Close the workbooks without saving changes
        sourceWorkbook.Close False
        destWorkbook.Close True
        
        MsgBox "Data copied successfully!"
    End Sub
    

    This code will automatically find the .xlsx and .xlsm files in the same folder where the macro is stored and copy data from the .xlsx file to the .xlsm file. The .xlsm file will be saved with the same name as the .xlsx file, but with the .xlsx extension.

  5. User

    save file with .xlsm extension at the end not .xlsx

  6. Assistant

    To save the destination file with the .xlsm extension at the end instead of .xlsx, you can modify the VBA code as follows:

    Sub CopyDataToAnotherFileInSameFolder()
        Dim sourceWorkbook As Workbook
        Dim destWorkbook As Workbook
        Dim sourceSheet As Worksheet
        Dim destSheet As Worksheet
        Dim sourceFilePath As String
        Dim destFilePath As String
        Dim sourceFileName As String
        Dim destFileName As String
        
        ' Get the path of the folder where the macro is stored
        Dim folderPath As String
        folderPath = ThisWorkbook.Path & "\"
        
        ' Get the file names
        sourceFileName = Dir(folderPath & "*.xlsx")
        destFileName = Dir(folderPath & "*.xlsm")
        
        ' Open both workbooks
        Set sourceWorkbook = Workbooks.Open(folderPath & sourceFileName)
        Set destWorkbook = Workbooks.Open(folderPath & destFileName)
        
        ' Set the source and destination sheets
        Set sourceSheet = sourceWorkbook.Sheets(1) ' Assuming data is on the first sheet
        Set destSheet = destWorkbook.Sheets(1) ' Assuming data will be copied to the first sheet
        
        ' Copy data from source sheet to destination sheet
        sourceSheet.UsedRange.Copy destSheet.Range("A1")
        
        ' Save the destination workbook with the same name, but with .xlsm extension
        destFilePath = folderPath & Replace(destFileName, ".xlsm", "_Temp.xlsm") ' Temporarily rename to avoid overwriting
        destWorkbook.SaveAs destFilePath, FileFormat:=xlOpenXMLWorkbookMacroEnabled
        Kill folderPath & destFileName ' Delete the old destination file
        Name destFilePath As folderPath & Replace(sourceFileName, ".xlsx", ".xlsm") ' Rename the copy to the original name
        
        ' Close the workbooks without saving changes
        sourceWorkbook.Close False
        destWorkbook.Close True
        
        MsgBox "Data copied successfully!"
    End Sub
    

    This code will now save the destination file with the .xlsm extension at the end, using the same name as the source file. It temporarily renames the destination file to avoid overwriting and then renames it back to the original name with the .xlsm extension.