2016-11-17 127 views
0

我想这个两个特点复制和粘贴数据集(表)的特定列:命名为单细胞参考范围在另一范围复制粘贴

  • 数据不会在启动知行

  • 观测的数量是未知的(这样是行的数据的数量)

我使用的查找功能,以获得第一和最后一个细胞数据的范围,BU那么我不知道如何在复制功能中引用它们。以下是我想要做的一个例子:

Sub prueba_copy() 

Dim sht As Worksheet 
Dim sht3 As Worksheet 
Dim LastRow As Long 
Dim FirstRow As Range 

Set sht = ThisWorkbook.Worksheets(Sheet1.Name) 
Set sht3 = ThisWorkbook.Worksheets(Sheet3.Name) 

Set FirstRow = sht.Cells.Find("TIPO DE USO", searchorder:=xlByRows,  LookAt:=xlPart) ' the result is range: F4 

LastRow = sht.Cells.Find("*", after:=Cells(1, 2), searchorder:=xlByRows, searchdirection:=xlPrevious).Row 'the result is: 9 

FirstRowLastRow将要用于引用的范围内进行复制。我想将数据从FirstRow复制到的FirstRow(EI F)列和LASTROW(EI 9)的行,所以该行动将读取范围(F4:F9).copy

'copy paste 
sht.Range("FirstRow.address():Firstrow.addres().column" & LastRow).Copy sht3.Range("A1") 

End Sub 

我曾尝试许多选择参考范围没有成功。所以,我会很感激你的帮助。

由于我是新手,我也很感谢任何建议,一个好的网页学习。

感谢,

古斯塔沃

回答

1
Dim f As range 

Set f = Sheet1.Cells.Find("TIPO DE USO", searchorder:=xlByRows, LookAt:=xlPart) 

If Not f Is Nothing Then 
    Sheet1.Range(f, Sheet1.Cells(Rows.Count, f.Column).End(xlUp)).Copy _ 
       Sheet3.Range("A1") 
Else 
    Msgbox "Header not found!" 
End If 
0

试试这个代码复制粘贴:

sht.Range(Cells(FirstRow.Row, FirstRow.Column), Cells(LastRow, FirstRow.Column)).Copy sht3.Range("A1") 
0

您正在使用工作表的对象做的唯一的事情申报工作表变量正在干扰代码。例外情况是给工作表一个更有意义的名字,而你没有这样做。

Dim Source As Range 

With Sheet1 
    Set Source = .Cells.Find("TIPO DE USO", searchorder:=xlByRows, LookAt:=xlPart) 

    If Not Source Is Nothing Then 
     .Range(Source, Source.EntireColumn.Rows(.Rows.Count).End(xlUp)).Copy Sheet3.Range("A1") 
    End If 
End With