Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用vba复制Excel工作表中的动态范围_Vba_Excel - Fatal编程技术网

如何使用vba复制Excel工作表中的动态范围

如何使用vba复制Excel工作表中的动态范围,vba,excel,Vba,Excel,我试图使范围在宏中是动态的,而不指定最后一行x.Sheets(“SheetName”).Range(“A2:K1000”)。在1000行中复制。我想将其更改为动态,因为有时我的范围小于或大于该范围 试试这个: Sub Test() Dim lRow as Long Dim sht as Worksheet Set sht = x.Sheets("SheetName") lRow = sht.Cells(sht.Rows.Count, 2).End(xlUp).Row sht.Range(

我试图使范围在宏中是动态的,而不指定最后一行
x.Sheets(“SheetName”).Range(“A2:K1000”)。在1000行中复制
。我想将其更改为动态,因为有时我的范围小于或大于该范围

试试这个:

Sub Test() 
Dim lRow as Long 
Dim sht as Worksheet
Set sht = x.Sheets("SheetName")

lRow = sht.Cells(sht.Rows.Count, 2).End(xlUp).Row

sht.Range("A2:K" & lRow).Copy
End Sub

通常,从下至上查找包含值的最后一行

with x.Sheets("SheetName")
    .Range(.cells(2, "A"), .cells(.rows.count, "K").end(xlup)).Copy
    'paste it somewhere
end with

像这样的东西可以完成任务:

Option Explicit

Public Sub TestMe()

    Dim lngLastRow As Long

    lngLastRow = 150
    'or come up with a function from here
    'https://www.rondebruin.nl/win/s9/win005.htm

    With x.Worksheets("SheetName")
        .Range(.Cells(2, 1), .Cells(lngLastRow, 1)).Copy
    End With

End Sub
通常,给定列中的最后一行或给定行中的最后一列是在VBA中需要花费大量时间才能完成的事情。因此,阅读以下内容是一个好主意:

re:“或者从这里提出一个函数”-真的。。。?这不是问题的全部吗?@Jeeped-不,OP想要它