Copy Range feat.немного «Воспитание»
Option Explicit
Sub ColumnWithBlanks()
Const cVntWsName As Variant = "PB" ' Worksheet Name or Index ("PB" or 1)
Const cLngHeaderRow As String = 1 ' Header Row
Const cStrLast As String = "Dept" ' Last Row Column Header
Const cStrSource As String = "Product" ' Source Column Header
Dim rngLast As Range ' Last Row Column (Range)
Dim rngSource As Range ' Source Column, Source Range
With ThisWorkbook.Sheets(cVntWsName)
' Find first (header) cell in Last Row Column
Set rngLast = .Rows(cLngHeaderRow).Find(What:=cStrLast, _
After:=.Cells(cLngHeaderRow, Columns.Count), LookIn:=xlFormulas, _
Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
' Find first (header) cell in Source Column
Set rngSource = .Rows(cLngHeaderRow).Find(What:=cStrSource, _
After:=.Cells(.Rows(cLngHeaderRow), Columns.Count), _
LookIn:=xlFormulas, Lookat:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
' Find last non-empty cell in Last Row Column
Set rngLast = rngLast.Resize(Rows.Count - rngLast.Row + 1, 1) _
.Find(What:="*", After:=rngLast.Cells(1, 1), LookIn:=xlFormulas, _
Lookat:=xlWhole, SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious) _
.Offset(0, rngSource.Column - rngLast.Column)
' Calculate Source Range
Set rngSource = .Range(rngSource.Offset(1, 0), rngLast)
Set rngLast = Nothing
End With
Debug.Print rngSource.Address
' To refer to this worksheet you can use "rngSource.Parent" e.g.:
Debug.Print rngSource.Parent.Name
' To refer to this workbook you can use "rngSource.Parent.Parent" e.g.:
Debug.Print rngSource.Parent.Parent.Name
' To refer to another worksheet in this workbook you can use e.g.
' "rngSource.Parent.Parent.Worksheets("Sheet2")"
Debug.Print rngSource.Parent.Parent.Worksheets("Sheet2").Name
' To copy the range to another range in this worksheet e.g.:
' rngSource.Copy rngSource.Parent.Range("A1")
Set rngSource = Nothing
End Sub