简体   繁体   English

Excel VBA在For循环中复制行

[英]Excel VBA copying rows in For loop

Thanks for help.感谢帮助。

I am trying to copy some rows to a new sheet and will be adding some conditions later on, but wanted to get the basics right.我正在尝试将一些行复制到新工作表中,稍后将添加一些条件,但希望获得正确的基础知识。

I want to copy the 4th row on the wsCurrent Sheet to the 1st row on the wsReview Sheet, and then copy rows 5-200 on to that sheet too.我想将 wsCurrent 工作表上的第 4 行复制到 wsReview 工作表上的第一行,然后也将第 5-200 行复制到该工作表上。 Eventually I will be having the for loop skip based on criteria so it is only certain rows that are copied.最终,我将根据条件跳过 for 循环,因此只复制某些行。

The first part of copying the 4th row works fine, but the for loop throws errors and doesn't do what I expect, for example, it takes the "j" as the column reference, rather than the value and if I try and use a more complex column reference, then it won't compile.复制第 4 行的第一部分工作正常,但 for 循环会引发错误并且不符合我的预期,例如,它将“j”作为列引用,而不是值,如果我尝试使用更复杂的列引用,则无法编译。

Sub exportForReview()
Dim wbCurrent As Workbook
Set wbCurrent = ThisWorkbook
Dim wsReview As Worksheet
Set wsReview = wbCurrent.Worksheets("ExportForReview")
Dim wsCurrent As Worksheet
Set wsCurrent = wbCurrent.Worksheets("Master")
Dim j As Integer
j = 2

wsCurrent.Range("4:4").Copy
wsReview.Range("1:1").PasteSpecial Paste:=xlPasteFormats
wsReview.Range("1:1").PasteSpecial Paste:=xlPasteValues

For i = 5 To 200
    wsCurrent.Cells(i).EntireRow.Copy
    wsReview.Cells(j).EntireRow.PasteSpecial Paste:=xlPasteFormats
    wsReview.Cells(j).EntireRow.PasteSpecial Paste:=xlPasteValues
    j = j + 1

Next i    
End Sub

I also tried我也试过

    For i = 5 To 200
    wsCurrent.Range("i:i").Copy
    wsReview.Range("j:j").EntireRow.PasteSpecial Paste:=xlPasteFormats
    wsReview.Range("j:j").PasteSpecial Paste:=xlPasteValues
    j = j + 1

Next i

There seems to be an issue trying to use variables inside the code to select and copy the row, can anyone advise?尝试使用代码中的变量来选择和复制行似乎存在问题,有人可以建议吗?

You're so close :)你离我很近:)

Change this part:改变这部分:

For i = 5 To 200
    wsCurrent.Cells(i).EntireRow.Copy
    wsReview.Cells(j).EntireRow.PasteSpecial Paste:=xlPasteFormats
    wsReview.Cells(j).EntireRow.PasteSpecial Paste:=xlPasteValues
    j = j + 1
Next i    

to this:对此:

For i = 5 To 200
    wsCurrent.Rows(i).Copy
    wsReview.Rows(j).PasteSpecial Paste:=xlPasteFormats
    wsReview.Rows(j).EntireRow.PasteSpecial Paste:=xlPasteValues
    j = j + 1
Next i

Friendly tip, is to consider to define the range!友情提示,就是考虑定义范围! example:例子:

wsCurrent.Range(wsCurrent.Cells(i, "A"), wsCurrent.Cells(i, "DA")).Copy 
wsReview.Range(wsReview.Cells(i, "A"), wsReview.Cells(i, "DA")).PasteSpecial Paste:=xlPasteFormats

It's large amount of data that you want to copy and paste which in large workbook will slow down the performance considerably.您想要复制和粘贴大量数据,这些数据在大型工作簿中会显着降低性能。 You'll propbably have "not responding" for a short time when you run the code.运行代码时,您可能会在短时间内“无响应”。

Cells(row, column) wants a row and column value while rows() or columns() only needs a single value :) Cells(row, column)需要行和列值,而rows()columns()只需要一个值:)

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

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