Excel 试图将数据值复制到另一张图纸Lastrow时出错
我正在尝试使用以下代码将一些数据复制到不同的工作表:Excel 试图将数据值复制到另一张图纸Lastrow时出错,excel,vba,copy,paste,Excel,Vba,Copy,Paste,我正在尝试使用以下代码将一些数据复制到不同的工作表: Sub FilterButton() Dim SourceRange As Range, DestRange As Range Dim DestSheet As Worksheet, Lr As Long With Application .ScreenUpdating = False .EnableEvents = False End With 'fill in t
Sub FilterButton()
Dim SourceRange As Range, DestRange As Range
Dim DestSheet As Worksheet, Lr As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'fill in the Source Sheet and range
Set SourceRange = Sheets("Imported Data").Range("A1:K1")
'Fill in the destination sheet and call the LastRow
'function to find the last row
Set DestSheet = Sheets("Test")
Lr = lastRow(DestSheet)
'With the information from the LastRow function we can
'create a destination cell
Set DestRange = DestSheet.Range("A" & Lr + 1)
'Copy the source range and use PasteSpecial to paste in
'the destination cell
SourceRange.Copy
DestRange.PasteSpecial _
Paste:=xlPasteValues, _
operation:=xlPasteSpecialOperationNone, _
skipblanks:=False, _
Transpose:=False
Application.CutCopyMode = False
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
仅当我尝试执行此操作时,才会出现以下错误:编译错误:未定义子或函数(此错误指向最后一行)。。。我怎样才能解决这个问题
编辑:
Sub FilterButton()
Dim SourceRange As Range, SRange, DestRange, myMultipleRange As Range
Dim DestSheet As Worksheet, Lr As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'fill in the Source Sheets and ranges
Set SourceRange = Sheets("Imported Data").Range("A2:B:C")
Set SRange = Sheets("Imported Data").Range("E2:E8")
Set myMultipleRange = Union(SourceRange, SRange)
'Fill in the destination sheet and find the last known cell
Set DestSheet = Sheets("Test")
'With the information on the new sheet
Set DestRange = DestSheet.Range("A:B:C:E")
'Copy the source range and use PasteSpecial to paste in
'the destination cell
myMultipleRange.Copy
DestRange.PasteSpecial _
Paste:=xlPasteValues, _
operation:=xlPasteSpecialOperationNone, _
skipblanks:=False, _
Transpose:=False
Application.CutCopyMode = False
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
这些范围是我想要的,但我不能在多个选项上使用这些范围:(!此函数实际存在吗?它不是VBA中的默认函数,因此您必须创建自己的函数 因此,接下来的问题是:
lastRow(args1)
的函数
函数
,而不是意外地创建了一个子
功能
是否存在于同一个模块中
,如果不存在,它是否为公共
(以便其他模块可以使用)您的
LastRow
函数可能有很多替代项。您可以尝试使用以下选项:
而不是你的台词:
Lr = lastRow(DestSheet)
把这个放进去:
Lr = DestSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
子过滤器按钮()
变暗源范围作为范围,减小范围作为范围
将图纸尺寸标注为工作表,Lr长度标注为
变暗列范围作为范围
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'fill in the Source Sheet and range
Set SourceRange = Sheets("Imported Data").Range("A:C,E:E")
'Fill in the destination sheet and find the last known cell
Set DestSheet = Sheets("Test")
'With the information on the new sheet
Set DestRange = DestSheet.Range("A:E")
'Copy the source range and use PasteSpecial to paste in
'the destination cell
SourceRange.Copy
DestRange.PasteSpecial _
Paste:=xlPasteValues, _
operation:=xlPasteSpecialOperationNone, _
skipblanks:=False, _
Transpose:=False
Application.CutCopyMode = False
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub我刚刚看到了你的问题,我知道有点晚了。我不确定你是否知道,但你使用的代码是Ron de Bruin在链接中给出的示例的完全复制品 要获得优雅的阐述,请查看各种示例。 请特别参阅本节底部,了解需要包含在代码中的函数的说明(如“LastRow函数”等)。代码中的注释指的是此函数…) 我引述: 重要提示:宏示例使用本页最后一节中的一个或多个函数。如果您刚开始使用VBA,请参阅本页,不要忘记将工作簿中的函数复制到工作簿的标准模块中。
在哪里粘贴我在internet上找到的代码?错误消息是正确的。您认为是什么?
lastRow
?lastRow是工作表中包含数据的最后一行。因此,如果单元格A14包含最后一个已知数据,我希望复制所有内容,直到该行不存在。VBA中没有类似于“la”的指令或命令strow'。这可能是某人制作的函数,您也需要在项目中使用它。因此,您需要返回代码源并搜索“lastrow函数”并将其复制到您的项目中。是否有方法避免使用lastrow,但获取我工作表中最后一个已知的数据单元(从同事处获得此代码,但他找不到原始函数)请参见下面答案中的建议解决方案。最后一个问题,代码可以复制整行..:Set SourceRange=Sheets(“导入的数据”).Range(“A2:E8”)我现在只想复制A、B、C和E列。这可能会在代码中添加几行吗?或者我需要另一个代码结构吗?嗯,我看不到您引用的代码行,但我看到复制的A1:K1范围。但任何一个都会复制整行(或者您问题中的代码不完整)。请尝试使用引号内的范围进行操作。或者仔细检查代码并显示完整的代码以获得进一步帮助。谢谢:)刚刚用给定的问题更新了我知道的全部代码。希望你能帮助我。这行代码有效吗:myMultipleRange.Copy
?你试图复制不允许的非连续范围。我仍然看不到你试图复制粘贴整行。你需要仔细分析你创建的逻辑如下范围:SourceRange、SRange、myMultipleRange
这对我来说很奇怪,但由于您的工作表布局可能是正确的。我现在已经找到了,谢谢您的帮助!我会发布我的答案