0

我在根据标准复制/粘贴行时遇到问题。

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

cboRadius.Value 是来自用户表单的数字(该行没有问题。)

每当我尝试运行此代码时,我都会收到“运行时错误'1004':对象'_Worksheet'的方法'Range'失败,黄色箭头指向目标行。问题是什么?

编辑: Ed Heywood-Lonsdale 建议我改变

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

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

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

4

2 回答 2

2

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

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

变成

sh.Range("A" & L & ":A" & lastcolumn1 & L)
于 2013-06-12T12:48:43.733 回答
1

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

但是如果你想循环这些行:

为了使用该Range功能,您需要使用列字母而不是列号。

您在这里有 2 个选项。利用

Chr(lastcolumn1 + 64) 

而不是lastcolumn1。缺陷是 This 仅适用于至 columns 的列Z,并且不适用于没有 if 语句和更多代码的双字母列。像下面这样应该适用于 ColumnZZZ

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)

或者

strColumnLetter = Left(Replace(Cells(1, lastcolumn1).Address(1, 0), "$", ""), InStr(1, Replace(Cells(1, lastcolumn1).Address(1, 0), "$", ""), 1) - 1) 

或者

strColumnLetter = Left(Cells(1, lastcolumn1).Address(1, 0), InStr(1, Cells(1, lastcolumn1).Address(1, 0), "$") - 1)

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

如果您不想将数字转换为列 Letter,您的最后一个选择是获取一系列单元格,因为该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
于 2013-06-12T13:23:43.830 回答