簡體   English   中英

引用命名為單個單元格的范圍在另一個要復制粘貼的范圍內

[英]Reference named single cell ranges in another range to copy-paste

我想使用以下兩個功能復制和粘貼數據集(表)的特定列:

  • 數據不在已知行中開始

  • 觀察數未知(數據行數也未知)

我使用find函數來獲取數據的第一個和最后一個單元格的范圍,但是后來我不知道如何在復制函數中引用它們。 以下是我想做的一個例子:

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

我嘗試了許多選項來引用范圍,但均未成功。 因此,我非常感謝您的幫助。

由於這是我的新手,我也很感謝任何有關學習良好網頁的建議。

謝謝,

古斯塔沃

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 

嘗試使用以下代碼進行復制粘貼:

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

使用工作表的對象聲明工作表變量的唯一操作是使代碼混亂。 例外是給工作表一個更有意義的名稱,而您卻沒有這樣做。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM