2016-11-17 129 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