根据几个条件将行从一个选项卡复制到另一个选项卡

时间:2023-01-21 20:07:41

I'm trying to copy rows from a tab that I imported into my document, to another tab.

我正在尝试将我导入到文档中的选项卡中的行复制到另一个选项卡。

'Clears previous data from sheet
Sheets("60L Specific Data").Select
Cells.Range("A3:AC3000").ClearContents

'Pastes Title Block from Imported into 60L sheet
Sheets("Imported Data").Select
Rows("1:1").Select
Selection.Copy
Sheets("60L Specific Data").Select
Rows("3:3").Select
ActiveSheet.Paste

'Filters using several criteria
Dim i As Integer
Dim j As Integer
j = 4

For i = 2 To 2000

    If Cells(i, 28) <> "" Or Cells(i, 29) <> "" Or InStr(Cells(i, 6).Value, "K60") <> 0 Or InStr(Cells(i, 6).Value, "60T") Then

        Sheets("Imported Data").Select
        Rows(i).Select
        Selection.Copy
        Sheets("60L Specific Data").Select
        Rows(j).Select
        ActiveSheet.Paste
        j = j + 1
    End If
 Next i

Now this issue has been frustrating me. There's a couple testing things I know you'd like to know:

现在这个问题让我很沮丧。我知道你想知道一些测试事项:

First, the first two blocks of code work as intended.
Second, if you remove the if statement out it works. Third, when I substituted in an if statements I knew had to be true for all, it did run, but any statement that would only result in some being chosen doesn't work at all.

首先,前两个代码块按预期工作。其次,如果删除if语句,它就可以了。第三,当我在if语句中替换我知道必须对所有人都是真的时,它确实运行了,但任何只会导致某些被选中的语句根本不起作用。

I'm very confused and have put a lot of time into this, please help!

我很困惑,并且花了很多时间,请帮忙!

1 个解决方案

#1


0  

Always safer/easier to use variables to refer to your sheets, and avoid activate/select where you can:

总是更安全/更容易使用变量来引用您的工作表,并避免激活/选择您可以:

Dim shtSrc As Worksheet, shtDest As Worksheet
Dim rw As Range
Dim i As Long, j As Long

Set shtSrc = Sheets("Imported Data")
Set shtDest = Sheets("60L Specific Data")

'Clears previous data from sheet
shtDest.Range("A3:AC3000").ClearContents

'Pastes Title Block from Imported into 60L sheet
shtSrc.Rows(1).Copy Destination:=shtDest.Cells(3, 1)

j = 4
'Filters using several criteria
For i = 2 To 2000
    Set rw = shtSrc.Rows(i)
    If rw.Cells(28) <> "" Or rw.Cells(29) <> "" Or _
        InStr(rw.Cells(6).Value, "K60") <> 0 Or _
        InStr(rw.Cells(6).Value, "60T") Then

        rw.Copy shtDest.Cells(j, 1)

        j = j + 1
    End If
 Next i

#1


0  

Always safer/easier to use variables to refer to your sheets, and avoid activate/select where you can:

总是更安全/更容易使用变量来引用您的工作表,并避免激活/选择您可以:

Dim shtSrc As Worksheet, shtDest As Worksheet
Dim rw As Range
Dim i As Long, j As Long

Set shtSrc = Sheets("Imported Data")
Set shtDest = Sheets("60L Specific Data")

'Clears previous data from sheet
shtDest.Range("A3:AC3000").ClearContents

'Pastes Title Block from Imported into 60L sheet
shtSrc.Rows(1).Copy Destination:=shtDest.Cells(3, 1)

j = 4
'Filters using several criteria
For i = 2 To 2000
    Set rw = shtSrc.Rows(i)
    If rw.Cells(28) <> "" Or rw.Cells(29) <> "" Or _
        InStr(rw.Cells(6).Value, "K60") <> 0 Or _
        InStr(rw.Cells(6).Value, "60T") Then

        rw.Copy shtDest.Cells(j, 1)

        j = j + 1
    End If
 Next i