将非连续空间的选择添加到 Excel 宏中的现有选择
我有一份文档,将根据用户的个人需求将其分发给具有任意行数(全部相同列数)的三个不连续组的用户。 我当前的宏运行速度相当慢,所以我想知道是否有人可以提出比我这里提供的更好的解决方案,或者至少向我指出哪些内置函数可能有助于我正在做的事情。
在下面的脚本中,我将其设置为作用于行中的数据:6、8-19、21-60、63-81。
所有这一切的目的都是删除数据第一列(sFirstCol =“D”)中的值,并将适用行中所有列(E->AC)中的值向左移动一个单元格,留下最右边的列值为空白。
Sub RollOver1()
Dim sFirstCol As String
Dim sSecCol As String
Dim sSLastCol As String
Dim sLastCol As String
Dim iFirstRow As Integer
Dim iLastRow As Integer
Dim excludeRows() As Variant
sFirstCol = "D"
sSecCol = "E"
sSLastCol = "AB"
sLastCol = "AC"
iFirstRow = 6
iLastRow = 81
excludeRows = Array(7, 20, 61, 62)
For i = iFirstRow To iLastRow
Dim bExcludedRow As Boolean
bExcludedRow = False
For Each eR In excludeRows
If eR = i Then
bExcludedRow = True
End If
Next
If bExcludedRow = False Then
Range(sSecCol + LTrim(Str(i)) + ":" + sLastCol + LTrim(Str(i))).Select
Selection.Copy
Range(sFirstCol + LTrim(Str(i)) + ":" + sSLastCol + LTrim(Str(i))).Select
ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, IconFileName:=False
Range(sLastCol + LTrim(Str(i))).Select
Selection.ClearContents
End If
Next
Range(sFirstCol + LTrim(Str(iFirstRow + 1))).Select
ActiveCell.FormulaR1C1 = "='Sheet1'!R[4]C[2]"
Range(sLastCol + LTrim(Str(iFirstRow))).Select
ActiveCell.FormulaR1C1 = "=RC[-1]+7"
Range("A1").Select
End Sub
I have a document that I'm going to hand out to users with three non-contiguous groups of arbitrary numbers of rows (same number of columns across the board) depending on their individual needs.
My current macro is quite slow to run, so I was wondering if someone can suggest a better solution than what I have here, or at least point me in the direction of what built in functions may help what I'm doing.
In the script below, I have it set to act on the data in rows: 6, 8-19, 21-60, 63-81.
All this is meant to be doing is deleting the values in the first column of data (sFirstCol = "D"), and shifting values from all columns (E->AC) in the applicable rows one cell to the left, leaving the rightmost column values blank.
Sub RollOver1()
Dim sFirstCol As String
Dim sSecCol As String
Dim sSLastCol As String
Dim sLastCol As String
Dim iFirstRow As Integer
Dim iLastRow As Integer
Dim excludeRows() As Variant
sFirstCol = "D"
sSecCol = "E"
sSLastCol = "AB"
sLastCol = "AC"
iFirstRow = 6
iLastRow = 81
excludeRows = Array(7, 20, 61, 62)
For i = iFirstRow To iLastRow
Dim bExcludedRow As Boolean
bExcludedRow = False
For Each eR In excludeRows
If eR = i Then
bExcludedRow = True
End If
Next
If bExcludedRow = False Then
Range(sSecCol + LTrim(Str(i)) + ":" + sLastCol + LTrim(Str(i))).Select
Selection.Copy
Range(sFirstCol + LTrim(Str(i)) + ":" + sSLastCol + LTrim(Str(i))).Select
ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, IconFileName:=False
Range(sLastCol + LTrim(Str(i))).Select
Selection.ClearContents
End If
Next
Range(sFirstCol + LTrim(Str(iFirstRow + 1))).Select
ActiveCell.FormulaR1C1 = "='Sheet1'!R[4]C[2]"
Range(sLastCol + LTrim(Str(iFirstRow))).Select
ActiveCell.FormulaR1C1 = "=RC[-1]+7"
Range("A1").Select
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是一些可以加快代码速度的提示:
调暗
所有变量在例程开始时,将“计算”设置为“手动”,关闭“屏幕更新”和“事件”。
最后再次打开它们
不要
选择
您要处理的范围。设置一个变量并对其采取行动。示例:不要一次对工作表上的一行进行操作,而是对连续的范围进行操作。在这种情况下,这将涉及一些更复杂的计算来计算排除行之间的行,但它将带来净收益。
“移动”数据的方法有很多,有些可能比复制、粘贴、清除更快。但是一旦应用了上面的提示,您可能会发现例程运行得足够快。如果没有,请重新发帖。
Here are some pointers that will speed up your code:
Dim
all you variablesAt the start of your routine, set Calculation to Manual, turn off ScreenUpdating and Events.
Turn them on again at the end
Don't
Select
ranges you want to process. Set a variable and act on that. Example:Don't act on you sheet one row at a time, act on a contiguous range. In this case this will involve some more complex calculations to work out the rows between excluded rows, but it will have a net benefit.
There are many ways to 'move' the data, some probably faster than Copy, Paste, Clear. But once you have applied the hints above you may find the routine runs fast enough. If not, post again.