Skip to main content
Removed redundant tag info from title
Source Link
Ken White
  • 126.2k
  • 15
  • 237
  • 476

How to copy a specific range from one worksheet to another worksheet in another workbook using VBA

I have two workbooks: Source.xlsm, sheet= Orig and Destination.xlsm, sheet=New

I am trying to move data between these sheets in a specific way: Example of both sheets before running the macro (the column ordering is on purpose)

enter image description here enter image description here

In the Orig sheet, cell F1 is storing todaystoday's date in the following format: dd mmm yy

My objective is to take only the rows from Orig with todaystoday's date and place all of them in a specific ordering to the end of the New sheet. So that after running the macro, New looks like:

enter image description here

Any suggestions as to how to progress would be amazing

I have the following code snippets to start to form a solution, all saved in Source.xlsm. This correctly select the bottom two rows of Orig since they have todays date in column D

Sub SelectTodayRows() Dim tableR As Range, cell As Range, r As Range Dim s As String Set tableR = Range("D1:D100000") Set r = Range("F1") For Each cell In tableR If cell = r Then s = s & cell.Row & ":" & cell.Row & ", " End If Next cell s = Left(s, Len(s) - 2) Range(s).Select End Sub 

The next step is appending these selected rows in the correct column ordering to New.

How to copy a specific range from one worksheet to another worksheet in another workbook using VBA

I have two workbooks: Source.xlsm, sheet= Orig and Destination.xlsm, sheet=New

I am trying to move data between these sheets in a specific way: Example of both sheets before running the macro (the column ordering is on purpose)

enter image description here enter image description here

In the Orig sheet, cell F1 is storing todays date in the following format: dd mmm yy

My objective is to take only the rows from Orig with todays date and place all of them in a specific ordering to the end of the New sheet. So that after running the macro, New looks like:

enter image description here

Any suggestions as to how to progress would be amazing

I have the following code snippets to start to form a solution, all saved in Source.xlsm. This correctly select the bottom two rows of Orig since they have todays date in column D

Sub SelectTodayRows() Dim tableR As Range, cell As Range, r As Range Dim s As String Set tableR = Range("D1:D100000") Set r = Range("F1") For Each cell In tableR If cell = r Then s = s & cell.Row & ":" & cell.Row & ", " End If Next cell s = Left(s, Len(s) - 2) Range(s).Select End Sub 

The next step is appending these selected rows in the correct column ordering to New.

How to copy a specific range from one worksheet to another worksheet in another workbook

I have two workbooks: Source.xlsm, sheet= Orig and Destination.xlsm, sheet=New

I am trying to move data between these sheets in a specific way: Example of both sheets before running the macro (the column ordering is on purpose)

enter image description here enter image description here

In the Orig sheet, cell F1 is storing today's date in the following format: dd mmm yy

My objective is to take only the rows from Orig with today's date and place all of them in a specific ordering to the end of the New sheet. So that after running the macro, New looks like:

enter image description here

Any suggestions as to how to progress would be amazing

I have the following code snippets to start to form a solution, all saved in Source.xlsm. This correctly select the bottom two rows of Orig since they have todays date in column D

Sub SelectTodayRows() Dim tableR As Range, cell As Range, r As Range Dim s As String Set tableR = Range("D1:D100000") Set r = Range("F1") For Each cell In tableR If cell = r Then s = s & cell.Row & ":" & cell.Row & ", " End If Next cell s = Left(s, Len(s) - 2) Range(s).Select End Sub 

The next step is appending these selected rows in the correct column ordering to New.

deleted 901 characters in body
Source Link

I have the source workbook as TestDatatwo workbooks: Source.xlsm, sheet= Orig and selecting specific rows from its Sheet1 Destination.xlsm, sheet=New

I am then attemptingtrying to takemove data between these sheets in a specific way: Example of both sheets before running the macro (the column ordering is on purpose)

enter image description here enter image description here

In the Orig sheet, cell F1 is storing todays date in the following format: dd mmm yy

My objective is to take only the rows from Orig with todays date and place all of them within Sheet1in a specific ordering to the end of AutoBE.xlsmthe New sheet. So that after running the macro, New looks like:

TesData.xlsm hasenter image description here

Any suggestions as to how to progress would be amazing

I have the following code and both TestData and AutoBE are opensnippets to start to form a solution, all saved in Source. AutoBE will already have existing rows so the copied rows must eb appended atxlsm. This correctly select the bottom two rows of the Sheet1Orig since they have todays date in AutoBE.

Declarationscolumn D

Sub CopyWorksheet() 'Source Dim swb As Workbook: Set swb = Workbooks("TestData.xlsm") Dim sws As Worksheet: Set sws = swb.Worksheets("Sheet1") 'Destination Dim dwb As Workbook: Set dwb = Workbooks("AutoBE.xlsm") ' Reference the old worksheet... Dim dwsOld As Worksheet: Set dwsOld = dwb.WorksheetsSelectTodayRows("Sheet1") ' and store its name and its code name in variables. Dim dName As String: dName = dwsOld.Name Dim dCodeName As String: dCodeName = dwsOld.CodeName Dim tableR As Range, cell As Range, r As Range Dim s As String 

This code is used to select a range of rows that have todays date within their H column, so cell AA1 is storing todays date.So, the last Line, Range(s).Select selects the rows with todays date on Sheet1 in TestData:

 Set tableR = Range("H1"D1:H100000"D100000") Set r = Range("AA1""F1") 'want to fix the selection so it should be a cell For Each cell In tableR If cell = r Then s = s & cell.Row & ":" & cell.Row & ", " End If Next cell s = Left(s, Len(s) - 2) Range(s).Select 

Now, I am trying to copy these rows/ range into Sheet1 in AutoBE

'Copy. Range(s).Copy After:=dwsOld 'Reference the new (copied) worksheet. Dim dwsNew As Worksheet: Set dwsNew = dwsOld.Next ' Delete the old worksheet.   Application.DisplayAlerts = False ' delete without confirmation ' dwsOld.Delete Application.DisplayAlerts = True ' Rename the new worksheet and restore the old code name. dwsNew.Name = dName  dwb.VBProject.VBComponents(dwsNew.CodeName).Name = dCodeName End Sub 

If I replace the first line with sws.Copy After:=dwsOld it successfully copies the entire sheet1 from TestData into sheet1 of AutoBE. But I'm struggling to only copyThe next step is appending these selected rows in the selection/range

It returns the followingcorrect column ordering to New.

Compile Error: Named argument Not found and highlights 'After:='

I have the source workbook as TestData.xlsm and selecting specific rows from its Sheet1. I am then attempting to take these specific rows and place them within Sheet1 of AutoBE.xlsm.

TesData.xlsm has the code and both TestData and AutoBE are open. AutoBE will already have existing rows so the copied rows must eb appended at the bottom of the Sheet1 in AutoBE.

Declarations

Sub CopyWorksheet() 'Source Dim swb As Workbook: Set swb = Workbooks("TestData.xlsm") Dim sws As Worksheet: Set sws = swb.Worksheets("Sheet1") 'Destination Dim dwb As Workbook: Set dwb = Workbooks("AutoBE.xlsm") ' Reference the old worksheet... Dim dwsOld As Worksheet: Set dwsOld = dwb.Worksheets("Sheet1") ' and store its name and its code name in variables. Dim dName As String: dName = dwsOld.Name Dim dCodeName As String: dCodeName = dwsOld.CodeName Dim tableR As Range, cell As Range, r As Range Dim s As String 

This code is used to select a range of rows that have todays date within their H column, so cell AA1 is storing todays date.So, the last Line, Range(s).Select selects the rows with todays date on Sheet1 in TestData:

 Set tableR = Range("H1:H100000") Set r = Range("AA1") 'want to fix the selection so it should be a cell For Each cell In tableR If cell = r Then s = s & cell.Row & ":" & cell.Row & ", " End If Next cell s = Left(s, Len(s) - 2) Range(s).Select 

Now, I am trying to copy these rows/ range into Sheet1 in AutoBE

'Copy. Range(s).Copy After:=dwsOld 'Reference the new (copied) worksheet. Dim dwsNew As Worksheet: Set dwsNew = dwsOld.Next ' Delete the old worksheet.   Application.DisplayAlerts = False ' delete without confirmation ' dwsOld.Delete Application.DisplayAlerts = True ' Rename the new worksheet and restore the old code name. dwsNew.Name = dName  dwb.VBProject.VBComponents(dwsNew.CodeName).Name = dCodeName End Sub 

If I replace the first line with sws.Copy After:=dwsOld it successfully copies the entire sheet1 from TestData into sheet1 of AutoBE. But I'm struggling to only copy in the selection/range

It returns the following

Compile Error: Named argument Not found and highlights 'After:='

I have two workbooks: Source.xlsm, sheet= Orig and Destination.xlsm, sheet=New

I am trying to move data between these sheets in a specific way: Example of both sheets before running the macro (the column ordering is on purpose)

enter image description here enter image description here

In the Orig sheet, cell F1 is storing todays date in the following format: dd mmm yy

My objective is to take only the rows from Orig with todays date and place all of them in a specific ordering to the end of the New sheet. So that after running the macro, New looks like:

enter image description here

Any suggestions as to how to progress would be amazing

I have the following code snippets to start to form a solution, all saved in Source.xlsm. This correctly select the bottom two rows of Orig since they have todays date in column D

Sub SelectTodayRows() Dim tableR As Range, cell As Range, r As Range Dim s As String Set tableR = Range("D1:D100000") Set r = Range("F1") For Each cell In tableR If cell = r Then s = s & cell.Row & ":" & cell.Row & ", " End If Next cell s = Left(s, Len(s) - 2) Range(s).Select End Sub 

The next step is appending these selected rows in the correct column ordering to New.

added 183 characters in body
Source Link

TesData.xlsm has the code and both TestData and AutoBE are open. AutoBE will already have existing rows so the copied rows must eb appended at the bottom of the Sheet1 in AutoBE.

Declarations

Declarations

TesData.xlsm has the code and both TestData and AutoBE are open. AutoBE will already have existing rows so the copied rows must eb appended at the bottom of the Sheet1 in AutoBE.

Declarations

Source Link
Loading