VBA Paste Into Visible Cells only Optimisation
So i wrote code that pastes a copied range from the clipboard into the visible cells only starting from the active cell the user marked
I already optimized my code somewhat speeding it up from 7200 Cells/min to 42000 cells/min but i think there still is plenty of room for optimization. But due to the fact that it is the third day programming VB for me i am asking the community for useful tips n tricks for making my code faster
The way i handle it is that i paste the clipboard into an new worksheet
and then search the next visible cell to insert into i use the variable max length to "limit" the search but in my application thousands of cells may be invisible.
I thought about using a StringBuilder to make it run faster but have no Idea how to implement that
important is that both rows and columns can be invisible
On Error GoTo ErrorHandler 'Enable Error Handling
Application.ScreenUpdating = False
Dim tblRow1 As Integer, lRow As Integer
Dim tblName As String
Dim lastRow, lastCol As Long
Dim outX, outY As Long
Dim maxLength As Long
clipboardTable As String
outputTable As String
outputTable = ActiveSheet.Name 'Safe the Name of the target sheet
outY = ActiveCell.Row 'Safe the Target position in sheet with xY coordinates
outX = ActiveCell.Column
maxLength = Sheets(outputTable).UsedRange.Rows.Count
outYtmp = outY 'Is needed to reset the corsur from the bottom to top
Set wbook = ActiveWorkbook
Set clipSheet = wbook.Sheets.Add
clipboardTable = clipSheet.Name
Sheets(clipboardTable).Activate
Sheets(clipboardTable).PasteSpecial
'Start Sheet
Sheets(clipboardTable).Select
lastRow = Sheets(clipboardTable).UsedRange.Rows.Count
lastCol = Sheets(clipboardTable).UsedRange.Columns.Count
'MsgBox ActiveSheet.UsedRange.Rows.Count
'MsgBox ActiveSheet.UsedRange.Columns.Count
'MsgBox " " & Sheets(inTable).Rows(1).EntireRow.Hidden
Sheets(outputTable).Select
For x = 1 To lastCol
Sheets(outputTable).Select
For j = 1 To maxLength
If Sheets(outputTable).Columns(outX).Hidden = False Then
For y = 1 To lastRow
For i = 1 To maxLength
If Sheets(outputTable).Rows(outY).Hidden = False Then
Sheets(outputTable).Cells([outY], [outX]) = Sheets(clipboardTable).Cells([y], [x])
outY = outY + 1
Exit For
End If
outY = outY + 1
Next
Next
outX = outX + 1
Exit For
End If
outX = outX + 1
Next
outY = outYtmp
Next
Application.DisplayAlerts = False
Sheets(clipboardTable).Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: ' Error-handling routine.
If (Worksheets(clipboardTable).Name <> "") Then
Application.DisplayAlerts = False
Sheets(clipboardTable).Delete
Application.DisplayAlerts = True
End If
End Sub
excel vba optimization cell visible
add a comment |
So i wrote code that pastes a copied range from the clipboard into the visible cells only starting from the active cell the user marked
I already optimized my code somewhat speeding it up from 7200 Cells/min to 42000 cells/min but i think there still is plenty of room for optimization. But due to the fact that it is the third day programming VB for me i am asking the community for useful tips n tricks for making my code faster
The way i handle it is that i paste the clipboard into an new worksheet
and then search the next visible cell to insert into i use the variable max length to "limit" the search but in my application thousands of cells may be invisible.
I thought about using a StringBuilder to make it run faster but have no Idea how to implement that
important is that both rows and columns can be invisible
On Error GoTo ErrorHandler 'Enable Error Handling
Application.ScreenUpdating = False
Dim tblRow1 As Integer, lRow As Integer
Dim tblName As String
Dim lastRow, lastCol As Long
Dim outX, outY As Long
Dim maxLength As Long
clipboardTable As String
outputTable As String
outputTable = ActiveSheet.Name 'Safe the Name of the target sheet
outY = ActiveCell.Row 'Safe the Target position in sheet with xY coordinates
outX = ActiveCell.Column
maxLength = Sheets(outputTable).UsedRange.Rows.Count
outYtmp = outY 'Is needed to reset the corsur from the bottom to top
Set wbook = ActiveWorkbook
Set clipSheet = wbook.Sheets.Add
clipboardTable = clipSheet.Name
Sheets(clipboardTable).Activate
Sheets(clipboardTable).PasteSpecial
'Start Sheet
Sheets(clipboardTable).Select
lastRow = Sheets(clipboardTable).UsedRange.Rows.Count
lastCol = Sheets(clipboardTable).UsedRange.Columns.Count
'MsgBox ActiveSheet.UsedRange.Rows.Count
'MsgBox ActiveSheet.UsedRange.Columns.Count
'MsgBox " " & Sheets(inTable).Rows(1).EntireRow.Hidden
Sheets(outputTable).Select
For x = 1 To lastCol
Sheets(outputTable).Select
For j = 1 To maxLength
If Sheets(outputTable).Columns(outX).Hidden = False Then
For y = 1 To lastRow
For i = 1 To maxLength
If Sheets(outputTable).Rows(outY).Hidden = False Then
Sheets(outputTable).Cells([outY], [outX]) = Sheets(clipboardTable).Cells([y], [x])
outY = outY + 1
Exit For
End If
outY = outY + 1
Next
Next
outX = outX + 1
Exit For
End If
outX = outX + 1
Next
outY = outYtmp
Next
Application.DisplayAlerts = False
Sheets(clipboardTable).Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: ' Error-handling routine.
If (Worksheets(clipboardTable).Name <> "") Then
Application.DisplayAlerts = False
Sheets(clipboardTable).Delete
Application.DisplayAlerts = True
End If
End Sub
excel vba optimization cell visible
Instead of saving the name property of the worksheet to a string variable and then using it with sheets() or worksheets(), why not set an object reference to that particular worksheet? Not an optimisation, although it means the sheet name no longer needs resolving within the nested loop -- especially since the value of outputTable does not change within the loop.
– chillin
Nov 15 '18 at 8:21
Also, you should probably avoid usingselect
andavoid
if you're looking to optimise -- and using arrays may improve speed too depending on use case.
– chillin
Nov 15 '18 at 8:25
@chillin I tried "simple" 2D arrays but it took more than twice the time to compute the code I wrote will copy 100s of thousends cells at once and ten it starts to take many minutes
– Thomaswoegi
Nov 15 '18 at 12:23
add a comment |
So i wrote code that pastes a copied range from the clipboard into the visible cells only starting from the active cell the user marked
I already optimized my code somewhat speeding it up from 7200 Cells/min to 42000 cells/min but i think there still is plenty of room for optimization. But due to the fact that it is the third day programming VB for me i am asking the community for useful tips n tricks for making my code faster
The way i handle it is that i paste the clipboard into an new worksheet
and then search the next visible cell to insert into i use the variable max length to "limit" the search but in my application thousands of cells may be invisible.
I thought about using a StringBuilder to make it run faster but have no Idea how to implement that
important is that both rows and columns can be invisible
On Error GoTo ErrorHandler 'Enable Error Handling
Application.ScreenUpdating = False
Dim tblRow1 As Integer, lRow As Integer
Dim tblName As String
Dim lastRow, lastCol As Long
Dim outX, outY As Long
Dim maxLength As Long
clipboardTable As String
outputTable As String
outputTable = ActiveSheet.Name 'Safe the Name of the target sheet
outY = ActiveCell.Row 'Safe the Target position in sheet with xY coordinates
outX = ActiveCell.Column
maxLength = Sheets(outputTable).UsedRange.Rows.Count
outYtmp = outY 'Is needed to reset the corsur from the bottom to top
Set wbook = ActiveWorkbook
Set clipSheet = wbook.Sheets.Add
clipboardTable = clipSheet.Name
Sheets(clipboardTable).Activate
Sheets(clipboardTable).PasteSpecial
'Start Sheet
Sheets(clipboardTable).Select
lastRow = Sheets(clipboardTable).UsedRange.Rows.Count
lastCol = Sheets(clipboardTable).UsedRange.Columns.Count
'MsgBox ActiveSheet.UsedRange.Rows.Count
'MsgBox ActiveSheet.UsedRange.Columns.Count
'MsgBox " " & Sheets(inTable).Rows(1).EntireRow.Hidden
Sheets(outputTable).Select
For x = 1 To lastCol
Sheets(outputTable).Select
For j = 1 To maxLength
If Sheets(outputTable).Columns(outX).Hidden = False Then
For y = 1 To lastRow
For i = 1 To maxLength
If Sheets(outputTable).Rows(outY).Hidden = False Then
Sheets(outputTable).Cells([outY], [outX]) = Sheets(clipboardTable).Cells([y], [x])
outY = outY + 1
Exit For
End If
outY = outY + 1
Next
Next
outX = outX + 1
Exit For
End If
outX = outX + 1
Next
outY = outYtmp
Next
Application.DisplayAlerts = False
Sheets(clipboardTable).Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: ' Error-handling routine.
If (Worksheets(clipboardTable).Name <> "") Then
Application.DisplayAlerts = False
Sheets(clipboardTable).Delete
Application.DisplayAlerts = True
End If
End Sub
excel vba optimization cell visible
So i wrote code that pastes a copied range from the clipboard into the visible cells only starting from the active cell the user marked
I already optimized my code somewhat speeding it up from 7200 Cells/min to 42000 cells/min but i think there still is plenty of room for optimization. But due to the fact that it is the third day programming VB for me i am asking the community for useful tips n tricks for making my code faster
The way i handle it is that i paste the clipboard into an new worksheet
and then search the next visible cell to insert into i use the variable max length to "limit" the search but in my application thousands of cells may be invisible.
I thought about using a StringBuilder to make it run faster but have no Idea how to implement that
important is that both rows and columns can be invisible
On Error GoTo ErrorHandler 'Enable Error Handling
Application.ScreenUpdating = False
Dim tblRow1 As Integer, lRow As Integer
Dim tblName As String
Dim lastRow, lastCol As Long
Dim outX, outY As Long
Dim maxLength As Long
clipboardTable As String
outputTable As String
outputTable = ActiveSheet.Name 'Safe the Name of the target sheet
outY = ActiveCell.Row 'Safe the Target position in sheet with xY coordinates
outX = ActiveCell.Column
maxLength = Sheets(outputTable).UsedRange.Rows.Count
outYtmp = outY 'Is needed to reset the corsur from the bottom to top
Set wbook = ActiveWorkbook
Set clipSheet = wbook.Sheets.Add
clipboardTable = clipSheet.Name
Sheets(clipboardTable).Activate
Sheets(clipboardTable).PasteSpecial
'Start Sheet
Sheets(clipboardTable).Select
lastRow = Sheets(clipboardTable).UsedRange.Rows.Count
lastCol = Sheets(clipboardTable).UsedRange.Columns.Count
'MsgBox ActiveSheet.UsedRange.Rows.Count
'MsgBox ActiveSheet.UsedRange.Columns.Count
'MsgBox " " & Sheets(inTable).Rows(1).EntireRow.Hidden
Sheets(outputTable).Select
For x = 1 To lastCol
Sheets(outputTable).Select
For j = 1 To maxLength
If Sheets(outputTable).Columns(outX).Hidden = False Then
For y = 1 To lastRow
For i = 1 To maxLength
If Sheets(outputTable).Rows(outY).Hidden = False Then
Sheets(outputTable).Cells([outY], [outX]) = Sheets(clipboardTable).Cells([y], [x])
outY = outY + 1
Exit For
End If
outY = outY + 1
Next
Next
outX = outX + 1
Exit For
End If
outX = outX + 1
Next
outY = outYtmp
Next
Application.DisplayAlerts = False
Sheets(clipboardTable).Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: ' Error-handling routine.
If (Worksheets(clipboardTable).Name <> "") Then
Application.DisplayAlerts = False
Sheets(clipboardTable).Delete
Application.DisplayAlerts = True
End If
End Sub
excel vba optimization cell visible
excel vba optimization cell visible
asked Nov 15 '18 at 7:41
ThomaswoegiThomaswoegi
61
61
Instead of saving the name property of the worksheet to a string variable and then using it with sheets() or worksheets(), why not set an object reference to that particular worksheet? Not an optimisation, although it means the sheet name no longer needs resolving within the nested loop -- especially since the value of outputTable does not change within the loop.
– chillin
Nov 15 '18 at 8:21
Also, you should probably avoid usingselect
andavoid
if you're looking to optimise -- and using arrays may improve speed too depending on use case.
– chillin
Nov 15 '18 at 8:25
@chillin I tried "simple" 2D arrays but it took more than twice the time to compute the code I wrote will copy 100s of thousends cells at once and ten it starts to take many minutes
– Thomaswoegi
Nov 15 '18 at 12:23
add a comment |
Instead of saving the name property of the worksheet to a string variable and then using it with sheets() or worksheets(), why not set an object reference to that particular worksheet? Not an optimisation, although it means the sheet name no longer needs resolving within the nested loop -- especially since the value of outputTable does not change within the loop.
– chillin
Nov 15 '18 at 8:21
Also, you should probably avoid usingselect
andavoid
if you're looking to optimise -- and using arrays may improve speed too depending on use case.
– chillin
Nov 15 '18 at 8:25
@chillin I tried "simple" 2D arrays but it took more than twice the time to compute the code I wrote will copy 100s of thousends cells at once and ten it starts to take many minutes
– Thomaswoegi
Nov 15 '18 at 12:23
Instead of saving the name property of the worksheet to a string variable and then using it with sheets() or worksheets(), why not set an object reference to that particular worksheet? Not an optimisation, although it means the sheet name no longer needs resolving within the nested loop -- especially since the value of outputTable does not change within the loop.
– chillin
Nov 15 '18 at 8:21
Instead of saving the name property of the worksheet to a string variable and then using it with sheets() or worksheets(), why not set an object reference to that particular worksheet? Not an optimisation, although it means the sheet name no longer needs resolving within the nested loop -- especially since the value of outputTable does not change within the loop.
– chillin
Nov 15 '18 at 8:21
Also, you should probably avoid using
select
and avoid
if you're looking to optimise -- and using arrays may improve speed too depending on use case.– chillin
Nov 15 '18 at 8:25
Also, you should probably avoid using
select
and avoid
if you're looking to optimise -- and using arrays may improve speed too depending on use case.– chillin
Nov 15 '18 at 8:25
@chillin I tried "simple" 2D arrays but it took more than twice the time to compute the code I wrote will copy 100s of thousends cells at once and ten it starts to take many minutes
– Thomaswoegi
Nov 15 '18 at 12:23
@chillin I tried "simple" 2D arrays but it took more than twice the time to compute the code I wrote will copy 100s of thousends cells at once and ten it starts to take many minutes
– Thomaswoegi
Nov 15 '18 at 12:23
add a comment |
0
active
oldest
votes
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%2f53314535%2fvba-paste-into-visible-cells-only-optimisation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53314535%2fvba-paste-into-visible-cells-only-optimisation%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
Instead of saving the name property of the worksheet to a string variable and then using it with sheets() or worksheets(), why not set an object reference to that particular worksheet? Not an optimisation, although it means the sheet name no longer needs resolving within the nested loop -- especially since the value of outputTable does not change within the loop.
– chillin
Nov 15 '18 at 8:21
Also, you should probably avoid using
select
andavoid
if you're looking to optimise -- and using arrays may improve speed too depending on use case.– chillin
Nov 15 '18 at 8:25
@chillin I tried "simple" 2D arrays but it took more than twice the time to compute the code I wrote will copy 100s of thousends cells at once and ten it starts to take many minutes
– Thomaswoegi
Nov 15 '18 at 12:23