VBA copy rows fast
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have to work on files with 5000 rows, for each row I have to insert 3 more rows and copy the content in these new rows (after that there will be more steps).
My macro works fine but the process of copying the content is really slow, I´m sure there is a solution that works better, any ideas?
Sub copy_rows()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lastrow = Lastrow * 4
For i = 1 To Lastrow Step 4
Cells(i, 7).EntireRow.Offset(1).Resize(3).Insert Shift:=xlDown
Rows(i).Copy Destination:=Rows(i + 1)
Rows(i).Copy Destination:=Rows(i + 2)
Rows(i).Copy Destination:=Rows(i + 3)
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
Thank you very much
excel vba performance copy rows
add a comment |
I have to work on files with 5000 rows, for each row I have to insert 3 more rows and copy the content in these new rows (after that there will be more steps).
My macro works fine but the process of copying the content is really slow, I´m sure there is a solution that works better, any ideas?
Sub copy_rows()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lastrow = Lastrow * 4
For i = 1 To Lastrow Step 4
Cells(i, 7).EntireRow.Offset(1).Resize(3).Insert Shift:=xlDown
Rows(i).Copy Destination:=Rows(i + 1)
Rows(i).Copy Destination:=Rows(i + 2)
Rows(i).Copy Destination:=Rows(i + 3)
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
Thank you very much
excel vba performance copy rows
add a comment |
I have to work on files with 5000 rows, for each row I have to insert 3 more rows and copy the content in these new rows (after that there will be more steps).
My macro works fine but the process of copying the content is really slow, I´m sure there is a solution that works better, any ideas?
Sub copy_rows()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lastrow = Lastrow * 4
For i = 1 To Lastrow Step 4
Cells(i, 7).EntireRow.Offset(1).Resize(3).Insert Shift:=xlDown
Rows(i).Copy Destination:=Rows(i + 1)
Rows(i).Copy Destination:=Rows(i + 2)
Rows(i).Copy Destination:=Rows(i + 3)
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
Thank you very much
excel vba performance copy rows
I have to work on files with 5000 rows, for each row I have to insert 3 more rows and copy the content in these new rows (after that there will be more steps).
My macro works fine but the process of copying the content is really slow, I´m sure there is a solution that works better, any ideas?
Sub copy_rows()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lastrow = Lastrow * 4
For i = 1 To Lastrow Step 4
Cells(i, 7).EntireRow.Offset(1).Resize(3).Insert Shift:=xlDown
Rows(i).Copy Destination:=Rows(i + 1)
Rows(i).Copy Destination:=Rows(i + 2)
Rows(i).Copy Destination:=Rows(i + 3)
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
Thank you very much
excel vba performance copy rows
excel vba performance copy rows
asked Nov 15 '18 at 9:18
Marco CMarco C
32
32
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
When it comes to speed:
Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.
So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.
The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.
Drawback: you maybe have to take care about formatting
With ActiveSheet
Dim lastRow As Long, lastCol As Long
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
Dim origData, copyData
origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
Dim r As Long, c As Long, i As Long
For r = 1 To lastRow ' All rows in orig data
For c = 1 To lastCol ' All columns in orig data
For i = 1 To 4 ' Copy everything 4 times
copyData((r - 1) * 4 + i, c) = origData(r, c)
Next i
Next c
Next r
.Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet
End With
This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much
– Marco C
Nov 19 '18 at 8:17
add a comment |
Probably the fastest way, if you are not interested in format, but only in the values:
Sub TestMe()
With Worksheets(1)
.Rows(1).Value = .Rows(2).Value
End With
End Sub
add a comment |
FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.
Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.
Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.
Sub copy_rows2()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
dim c as integer
c = 10 'number of columns with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
lastRow = lastRow * 4
For i = 1 To lastRow Step 4
'inserts 3 rows at a time
ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown
'copy data into new rows limited to number of columns c
Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316021%2fvba-copy-rows-fast%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
When it comes to speed:
Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.
So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.
The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.
Drawback: you maybe have to take care about formatting
With ActiveSheet
Dim lastRow As Long, lastCol As Long
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
Dim origData, copyData
origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
Dim r As Long, c As Long, i As Long
For r = 1 To lastRow ' All rows in orig data
For c = 1 To lastCol ' All columns in orig data
For i = 1 To 4 ' Copy everything 4 times
copyData((r - 1) * 4 + i, c) = origData(r, c)
Next i
Next c
Next r
.Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet
End With
This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much
– Marco C
Nov 19 '18 at 8:17
add a comment |
When it comes to speed:
Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.
So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.
The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.
Drawback: you maybe have to take care about formatting
With ActiveSheet
Dim lastRow As Long, lastCol As Long
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
Dim origData, copyData
origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
Dim r As Long, c As Long, i As Long
For r = 1 To lastRow ' All rows in orig data
For c = 1 To lastCol ' All columns in orig data
For i = 1 To 4 ' Copy everything 4 times
copyData((r - 1) * 4 + i, c) = origData(r, c)
Next i
Next c
Next r
.Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet
End With
This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much
– Marco C
Nov 19 '18 at 8:17
add a comment |
When it comes to speed:
Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.
So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.
The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.
Drawback: you maybe have to take care about formatting
With ActiveSheet
Dim lastRow As Long, lastCol As Long
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
Dim origData, copyData
origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
Dim r As Long, c As Long, i As Long
For r = 1 To lastRow ' All rows in orig data
For c = 1 To lastCol ' All columns in orig data
For i = 1 To 4 ' Copy everything 4 times
copyData((r - 1) * 4 + i, c) = origData(r, c)
Next i
Next c
Next r
.Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet
End With
When it comes to speed:
Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.
So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.
The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.
Drawback: you maybe have to take care about formatting
With ActiveSheet
Dim lastRow As Long, lastCol As Long
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastRow = .Cells(.Rows.Count, 1).End(xlUp).row
Dim origData, copyData
origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
Dim r As Long, c As Long, i As Long
For r = 1 To lastRow ' All rows in orig data
For c = 1 To lastCol ' All columns in orig data
For i = 1 To 4 ' Copy everything 4 times
copyData((r - 1) * 4 + i, c) = origData(r, c)
Next i
Next c
Next r
.Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet
End With
answered Nov 15 '18 at 10:13
FunThomasFunThomas
5,5161626
5,5161626
This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much
– Marco C
Nov 19 '18 at 8:17
add a comment |
This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much
– Marco C
Nov 19 '18 at 8:17
This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much
– Marco C
Nov 19 '18 at 8:17
This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much
– Marco C
Nov 19 '18 at 8:17
add a comment |
Probably the fastest way, if you are not interested in format, but only in the values:
Sub TestMe()
With Worksheets(1)
.Rows(1).Value = .Rows(2).Value
End With
End Sub
add a comment |
Probably the fastest way, if you are not interested in format, but only in the values:
Sub TestMe()
With Worksheets(1)
.Rows(1).Value = .Rows(2).Value
End With
End Sub
add a comment |
Probably the fastest way, if you are not interested in format, but only in the values:
Sub TestMe()
With Worksheets(1)
.Rows(1).Value = .Rows(2).Value
End With
End Sub
Probably the fastest way, if you are not interested in format, but only in the values:
Sub TestMe()
With Worksheets(1)
.Rows(1).Value = .Rows(2).Value
End With
End Sub
answered Nov 15 '18 at 9:23
VityataVityata
32.5k72453
32.5k72453
add a comment |
add a comment |
FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.
Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.
Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.
Sub copy_rows2()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
dim c as integer
c = 10 'number of columns with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
lastRow = lastRow * 4
For i = 1 To lastRow Step 4
'inserts 3 rows at a time
ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown
'copy data into new rows limited to number of columns c
Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
add a comment |
FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.
Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.
Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.
Sub copy_rows2()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
dim c as integer
c = 10 'number of columns with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
lastRow = lastRow * 4
For i = 1 To lastRow Step 4
'inserts 3 rows at a time
ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown
'copy data into new rows limited to number of columns c
Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
add a comment |
FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.
Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.
Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.
Sub copy_rows2()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
dim c as integer
c = 10 'number of columns with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
lastRow = lastRow * 4
For i = 1 To lastRow Step 4
'inserts 3 rows at a time
ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown
'copy data into new rows limited to number of columns c
Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.
Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.
Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.
Sub copy_rows2()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
dim c as integer
c = 10 'number of columns with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
lastRow = lastRow * 4
For i = 1 To lastRow Step 4
'inserts 3 rows at a time
ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown
'copy data into new rows limited to number of columns c
Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
answered Nov 15 '18 at 11:45
KagekibaKagekiba
357
357
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316021%2fvba-copy-rows-fast%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown