将非连续空间的选择添加到 Excel 宏中的现有选择

发布于 2024-12-09 08:59:57 字数 1568 浏览 0 评论 0原文

我有一份文档,将根据用户的个人需求将其分发给具有任意行数(全部相同列数)的三个不连续组的用户。 我当前的宏运行速度相当慢,所以我想知道是否有人可以提出比我这里提供的更好的解决方案,或者至少向我指出哪些内置函数可能有助于我正在做的事情。

在下面的脚本中,我将其设置为作用于行中的数据: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶花眉 2024-12-16 08:59:57

以下是一些可以加快代码速度的提示:

调暗所有变量

Dim i As long
Dim eR As variant

在例程开始时,将“计算”设置为“手动”,关闭“屏幕更新”和“事件”。

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

最后再次打开它们

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = true

不要选择您要处理的范围。设置一个变量并对其采取行动。示例:

Dim rng as Range
Set rng = Range(sSecCol + LTrim(Str(i)) + ":" + sLastCol + LTrim(Str(i)))
rng.Copy

不要一次对工作表上的一行进行操作,而是对连续的范围进行操作。在这种情况下,这将涉及一些更复杂的计算来计算排除行之间的行,但它将带来净收益。

“移动”数据的方法有很多,有些可能比复制、粘贴、清除更快。但是一旦应用了上面的提示,您可能会发现例程运行得足够快。如果没有,请重新发帖。

Here are some pointers that will speed up your code:

Dim all you variables

Dim i As long
Dim eR As variant

At the start of your routine, set Calculation to Manual, turn off ScreenUpdating and Events.

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

Turn them on again at the end

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = true

Don't Select ranges you want to process. Set a variable and act on that. Example:

Dim rng as Range
Set rng = Range(sSecCol + LTrim(Str(i)) + ":" + sLastCol + LTrim(Str(i)))
rng.Copy

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文