Excel 复制循环中的行并粘贴到新图纸

Excel 复制循环中的行并粘贴到新图纸,excel,vba,copy,row,range,Excel,Vba,Copy,Row,Range,根据条件复制/粘贴行时遇到问题 Dim lastrow1 As Long Dim lastcolumn1 As Long Dim Distance As Long Distance = 14 Set sh = ThisWorkbook.Sheets("Sample Address Database") Set sh2 = ThisWorkbook.Sheets("Workspace") lastrow1 = sh.Cells(Rows.Count, "A").End(xlUp).row last

根据条件复制/粘贴行时遇到问题

Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim Distance As Long
Distance = 14
Set sh = ThisWorkbook.Sheets("Sample Address Database")
Set sh2 = ThisWorkbook.Sheets("Workspace")
lastrow1 = sh.Cells(Rows.Count, "A").End(xlUp).row
lastcolumn1 = sh.Cells(1, Columns.Count).End(xlToLeft).Column

Dim L As Long
For L = 2 To lastrow1
    If _
    sh.Cells(L, Distance).Value <= CDbl(cboRadius.Value) Then
        sh.Range("A" & L & ":" & lastcolumn1 & L).Copy _
        Destination:=sh2.Range("A" & L)
    End If
Next

现在只复制A列,或者如果我将其更改为B、C、D等。我认为问题在于,它可能没有记录lastcolumn1和L是列/行号,而是将它们设置为一个值,从而导致范围故障。

在定义要复制的范围时尝试添加“a”:

sh.Range("A" & L & ":" & lastcolumn1 & L)
变成

sh.Range("A" & L & ":A" & lastcolumn1 & L)
定义要复制的范围时,请尝试添加“A”:

sh.Range("A" & L & ":" & lastcolumn1 & L)
变成

sh.Range("A" & L & ":A" & lastcolumn1 & L)

我会使用内置的Excel过滤器就地过滤数据,然后复制结果,而不是尝试循环每一行

但是如果要以任何方式循环行:

要使用
范围
功能,您需要使用列字母而不是列数字

你有两个选择。使用

Chr(lastcolumn1 + 64) 
而不是最后一列。该漏洞是,这将只适用于最多列
Z
,并且不适用于没有if语句和更多代码的双字母列。类似于以下内容,最多可用于列
ZZZ

If lastcolumn1> 52 Then
    strColumnLetter = Chr(Int((lastcolumn1- 1) / 52) + 64) & Chr(Int((lastcolumn1- 27) / 26) + 64) & Chr(Int((lastcolumn1- 27) Mod 26) + 65)
ElseIf lastcolumn1> 26 Then
    strColumnLetter = Chr(Int((lastcolumn1- 1) / 26) + 64) & Chr(Int((lastcolumn1- 1) Mod 26) + 65)
Else
    strColumnLetter = Chr(lastcolumn1+ 64)
End If
但你也可以使用

strColumnLetter = Split(Cells(1, lastcolumn1).EntireColumn.Address(False, False), ":")(0)

因为这将适用于Excel所能容纳的尽可能多的列

如果不想将数字转换为列字母,最后一个选项是获取单元格范围,因为
Cells
函数可以接受列数字作为参数

sh.Range(cells(L,1), cells(L,lastcolumn1))
我再次建议使用标准的内置过滤函数过滤掉不需要的数据,然后复制剩下的数据。这只是为了增加更多的选择

如果你提供一些样本信息,我可以给你写一个子,将为你做过滤器复制粘贴,但我不知道你的数据是如何设置的

以下是一个基于原始问题的示例:

Sub FilterAndCopy()

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

Dim sh As Worksheet, sh2 As Worksheet
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim Distance As Long
Distance = 14


Set sh = ThisWorkbook.Sheets("Sample Address Database")
Set sh2 = ThisWorkbook.Sheets("Workspace")

lastrow1 = sh.Cells(Rows.Count, "A").End(xlUp).Row
lastcolumn1 = sh.Cells(1, Columns.Count).End(xlToLeft).Column

With sh
    .Range(.Cells(1, 1), .Cells(lastrow1, lastcolumn1)).AutoFilter , _
    field:=Distance, _
    Criteria1:="<=" & CDbl(151), _
    Operator:=xlAnd

    .Range(.Cells(2, 1), .Cells(lastrow1, lastcolumn1)).Copy _
    sh2.Range("A2")

End With

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

End Sub
子过滤器副本()
Application.ScreenUpdating=False
Application.EnableEvents=False
Application.Calculation=xlCalculationManual
将sh设置为工作表,将sh2设置为工作表
变暗最后一行1的长度
第1列的长度相同
遥远的距离
距离=14
Set sh=ThisWorkbook.Sheets(“示例地址数据库”)
Set sh2=ThisWorkbook.Sheets(“工作区”)
lastrow1=sh.Cells(Rows.Count,“A”).End(xlUp).Row
lastcolumn1=sh.Cells(1,Columns.Count).End(xlToLeft).Column
与sh
.Range(.Cells(1,1),.Cells(lastrow1,lastcolumn1)).AutoFilter_
字段:=距离_

准则1:=“我会使用内置的Excel过滤器就地过滤数据,然后复制结果,而不是尝试循环每一行

但是如果要以任何方式循环行:

要使用
范围
功能,您需要使用列字母而不是列数字

这里有两个选项。使用

Chr(lastcolumn1 + 64) 
而不是lastcolumn1。该缺陷是,这只适用于最多列
Z
,不适用于没有if语句和更多代码的双字母列。如下所示应适用于最多列
ZZZ

If lastcolumn1> 52 Then
    strColumnLetter = Chr(Int((lastcolumn1- 1) / 52) + 64) & Chr(Int((lastcolumn1- 27) / 26) + 64) & Chr(Int((lastcolumn1- 27) Mod 26) + 65)
ElseIf lastcolumn1> 26 Then
    strColumnLetter = Chr(Int((lastcolumn1- 1) / 26) + 64) & Chr(Int((lastcolumn1- 1) Mod 26) + 65)
Else
    strColumnLetter = Chr(lastcolumn1+ 64)
End If
但你也可以使用

strColumnLetter = Split(Cells(1, lastcolumn1).EntireColumn.Address(False, False), ":")(0)

因为这将适用于Excel所能容纳的尽可能多的列

如果不想将数字转换为列字母,最后一个选项是获取单元格范围,因为
Cells
函数可以接受列数字作为参数

sh.Range(cells(L,1), cells(L,lastcolumn1))
我再次建议使用标准的内置过滤功能过滤掉你不想要的数据,然后复制剩下的数据。这只是为了增加更多的选项

如果你提供一些样本信息,我可以给你写一个子,将为你做过滤器复制粘贴,但我不知道你的数据是如何设置的

以下是一个基于原始问题的示例:

Sub FilterAndCopy()

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

Dim sh As Worksheet, sh2 As Worksheet
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim Distance As Long
Distance = 14


Set sh = ThisWorkbook.Sheets("Sample Address Database")
Set sh2 = ThisWorkbook.Sheets("Workspace")

lastrow1 = sh.Cells(Rows.Count, "A").End(xlUp).Row
lastcolumn1 = sh.Cells(1, Columns.Count).End(xlToLeft).Column

With sh
    .Range(.Cells(1, 1), .Cells(lastrow1, lastcolumn1)).AutoFilter , _
    field:=Distance, _
    Criteria1:="<=" & CDbl(151), _
    Operator:=xlAnd

    .Range(.Cells(2, 1), .Cells(lastrow1, lastcolumn1)).Copy _
    sh2.Range("A2")

End With

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

End Sub
子过滤器副本()
Application.ScreenUpdating=False
Application.EnableEvents=False
Application.Calculation=xlCalculationManual
将sh设置为工作表,将sh2设置为工作表
变暗最后一行1的长度
第1列的长度相同
遥远的距离
距离=14
Set sh=ThisWorkbook.Sheets(“示例地址数据库”)
Set sh2=ThisWorkbook.Sheets(“工作区”)
lastrow1=sh.Cells(Rows.Count,“A”).End(xlUp).Row
lastcolumn1=sh.Cells(1,Columns.Count).End(xlToLeft).Column
与sh
.Range(.Cells(1,1),.Cells(lastrow1,lastcolumn1)).AutoFilter_
字段:=距离_

准则1:=“如果您试图选择最右边的列作为
lastcolumn1
,您是否应该使用
xlToRight
?@Jack如果您从a列开始并向右移动,您将转到第一个空格前的最后一列(不总是真正的最后一列),但是如果(如op所做的那样)转到工作表中的最后一个单元格,然后向左转到第一列,其中的数据将是最后使用的列。@user2140261非常好。我不知道那个把戏;谢谢:)如果您试图选择最右边的列作为
lastcolumn1
,您是否应该使用
xlToRight
?@Jack如果您从a列开始并向右移动,您将转到第一个空格前的最后一列(不一定是真正的最后一列),但是如果(如op所做)转到工作表中的最后一个单元格,然后向左转到第一列,其中的数据将是最后使用的列。@user2140261非常好。我不知道那个把戏;谢谢:)啊,我想不出换行符。。。希望你能读到这个!啊,很抱歉。我倾向于使用:sh.Range(“A”&L&“:A”&chr(64+lastcolumn1)&L),但如果列超过Z,则会中断。chr()函数将数字转换为字母-65变成A,66变成b,依此类推。如果你可以通过Z,那么就有一个If函数,如果需要的话,它会变成“A”&chr(64+lastcolumn1-26)。这非常有用,我自己也不会偶然发现。非常感谢你!嘎,我想不出换行符。。。希望你能读到这个!啊,很抱歉。我倾向于使用:sh.Range(“A”&L&“:A”&chr(64+lastcolumn1)&L),但如果该列经过,则会中断