create an excel macro code : the file should open a source sheet, copy rows and colum,…
- User
create an excel macro code : the file should open a source sheet, copy rows and colum, close the file and paste it on the actuel sheet
- Assistant
Here is the Excel VBA macro code that opens a source sheet, copies rows and columns, closes the file, and pastes it on the active sheet:
Sub CopyDataFromSourceSheet() Dim wbSource As Workbook Dim wsSource As Worksheet Dim wsDestination As Worksheet Dim sourceFileName As String Dim sourceSheetName As String ' Define the source file name and sheet name sourceFileName = "SourceWorkbook.xlsx" ' Update with your source file name sourceSheetName = "SourceSheet" ' Update with your source sheet name ' Open the source workbook Set wbSource = Workbooks.Open(sourceFileName) Set wsSource = wbSource.Sheets(sourceSheetName) ' Set the destination worksheet as the active sheet Set wsDestination = ThisWorkbook.ActiveSheet ' Copy the rows and columns from the source sheet to the destination sheet wsSource.UsedRange.Copy wsDestination.Range("A1") ' Close the source workbook without saving changes wbSource.Close False End SubTo run this macro, press
Alt + F11to open the Visual Basic for Applications (VBA) editor, go to > , and paste the code into the module window. Then, press to run the macro. - User
create an excel macro code : the file should open a source sheet, copy the sheet, close the file and paste it on the actuel sheet
- Assistant
Here is the Excel VBA macro code that opens a source sheet, copies the entire sheet, closes the file, and pastes it onto the active sheet:
Sub CopySheetFromSourceWorkbook() Dim wbSource As Workbook Dim wsSource As Worksheet Dim wsDestination As Worksheet Dim sourceFileName As String Dim sourceSheetName As String ' Define the source file name and sheet name sourceFileName = "SourceWorkbook.xlsx" ' Update with your source file name sourceSheetName = "SourceSheet" ' Update with your source sheet name ' Open the source workbook Set wbSource = Workbooks.Open(sourceFileName) ' Set the source worksheet Set wsSource = wbSource.Sheets(sourceSheetName) ' Set the destination worksheet as the active sheet Set wsDestination = ThisWorkbook.ActiveSheet ' Copy the entire source sheet to the destination worksheet wsSource.Copy After:=wsDestination ' Close the source workbook without saving changes wbSource.Close False End SubTo run this macro, press
Alt + F11to open the Visual Basic for Applications (VBA) editor, go to > , and paste the code into the module window. Then, press to run the macro.