简体   繁体   English

Excel VBA-使用数组在工作表中移动,然后复制粘贴第n行

[英]Excel VBA - Using Array to move through worksheets and then copy paste nth row

I am a little new to VBA, I just started using it in excel recently and have reached a road block. 我对VBA有点陌生,最近我才开始在excel中使用它,但遇到了障碍。

I am creating a crosswalk in order to upload my data to a business intelligence application. 我正在创建人行横道,以便将我的数据上传到商业智能应用程序。 Unfortunately I do not have a connection to the database to run my reports so I have to do this with excel. 不幸的是,我没有与数据库的连接来运行我的报告,因此我必须使用excel来完成。 The issue that I am running into is that there is a bug in our application where after the 1000th row in an excel document it will skip every 1000th line. 我遇到的问题是我们的应用程序中存在一个错误,该错误在excel文档的第1000行之后将跳过每1000行。 Our development team is aware of this but there is no ETA to resolve the issue. 我们的开发团队已经意识到了这一点,但是没有ETA可以解决问题。 As a work around I am trying to use VBA to copy the 1000th line (starting with 2000) to the end of the same sheet. 作为解决方法,我试图使用VBA将第1000行(从2000开始)复制到同一张纸的末尾。

I currently have code wrote for this that works on a single worksheet, but I have several pages that have over 1000 rows, so I am trying to feed the names of those sheets into an array and cycle through each one and do the copy/pasting. 目前,我已经为此代码编写了可在单个工作表上工作的代码,但是我有几页具有超过1000行,因此我试图将这些工作表的名称输入一个数组,并循环遍历每个表并进行复制/粘贴。 。

My working code for 1 worksheet: 我的1个工作表的工作代码:

Sub Test()

Dim WB As Workbook
Dim WS As Worksheet
Dim i As Integer
Dim x As Integer
Dim r As Range



Set WB = Workbooks("macrotesting.xlsm")
Set WS = Worksheets("Usage")

Set r = WS.UsedRange
k = r.Rows.Count
x = 2000
i = 2

Do While x < k

WS.Range(("A" & x) & (":L" & x)).Copy
WS.Rows.End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues

i = i + 1
x = 1000 * i

Loop

End Sub

I was able to find some help with this here but it is failing. 我可以在这里找到一些帮助,但是失败了。 Code with the Array: 数组代码:

Sub Test()

Dim wsArray As Variant
Dim wsArrayCrnt As Variant
Dim i As Integer
Dim x As Integer
Dim r As Range

wsArray = Array("Usage", "Use")

For Each wsArrayCrnt In wsArray
With Worksheets(wsArrayCrnt)

    r = .UsedRange
    k = r.Rows.Count
    x = 2000
    i = 2

    Do While x < k

    .Range(("A" & x) & (":L" & x)).Copy
    .End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues

    i = i + 1
    x = 1000 * i

    Loop
End With
Next wsArrayCrnt

End Sub

It seems to be failing at the 它似乎在失败

 k = r.Rows.Count

Though I am not sure if it will finish after this. 虽然我不确定此后是否会完成。

I have also tried to dimension the workbook and add it to the with statement and all the variables after. 我还尝试过对工作簿进行标注,并将其添加到with语句以及之后的所有变量中。

Instead of manually typing in which sheets you want could you not just cycle through all the work sheets in the workbook? 除了手动输入所需的工作表外,您还不能仅浏览工作簿中的所有工作表吗?

Sub test()
Dim i As Integer
Dim x As Integer
Dim r As Range

For Each wsheet In ActiveWorkbook.worksheets

    Set r = wsheet.UsedRange
    k = r.Rows.Count
    x = 2000
    i = 2

    Do While x < k
        wsheet.Range(("A" & x) & (":L" & x)).Copy
        wsheet.Rows.End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues

        i = i + 1
        x = 1000 * i
    Loop

Next wsheet

End Sub

This will cycle through each worksheet in the current workbook you are running the macro from and copy every 1000th row to the end of worksheet. 这将在您正在运行宏的当前工作簿中的每个工作表中循环,并将每第1000行复制到工作表的末尾。

(Any sheet with less than 2000 rows will skip over the do while loop and be ignored) (任何少于2000行的工作表都将跳过do while循环并被忽略)

you must type 您必须输入

Set r = .UsedRange

Since UsedRange is an object (namely a Range object) 由于UsedRange是一个对象(即Range对象)

You may also want to use a more compact code like follows: 您可能还希望使用更紧凑的代码,如下所示:

Sub Test()

Dim wsArrayCrnt As Variant
Dim x As Long

For Each wsArrayCrnt In Array("Usage", "Use")
    With Worksheets(wsArrayCrnt)
        x = 2000
        Do While x < .UsedRange.Rows.Count
           .Range(("A" & x) & (":L" & x)).Copy
           .End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues
           x = x + 1000
       Loop
    End With
Next wsArrayCrnt

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM