复制输入框指示的范围

Copy range indicated by inputbox

我正在寻找可以将 excel sheet 中的特定范围复制到新的 sheet 的模型。目前我有五个专栏。第一列是年份。我想指出,例如 inputbox 应该复制哪一年。在 B 列中,显示了一年中的几周。在列 C 到 E 中,记录了特定数据。结果,我的 sheet 看起来像这样:

Col A   Col B   Col C   Col D   ColE
Year    Week    Amount  time    forecast
2000    1       368     2000w1  400
2000    2       8646    2000w2  8500
until...
2014    52      46546   2014w52 47000

用输入框,我想说明2014年应该复制到下一个sheet。直到现在我 wrote/compiled 以下宏:

Sub Copy_year()
    Dim Forecastyear As String
    Dim Rng As Range

    Forecastyear = InputBox("Enter a year to forecast")

    If Trim(Forecastyear) <> "" Then
        With Sheets(2).Range("A:A")
            Set Rng = .Find(What:=Forecastyear, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                MsgBox "Nothing found"
            End If
        End With
    End If

    'Range(ActiveCell, RC[52,4]).Select
End Sub

这是 select 第一个包含年份值的单元格。我想 select 包含 selected 单元格的行的完整 5 列以及接下来的 51 周(总共 52 周)。我怎样才能 select 所有这些数据?

使用这个

Sub Copy_year()
    Dim Forecastyear As String
    Dim Rng As Range

    Forecastyear = InputBox("Enter a year to forecast")

    If Trim(Forecastyear) <> "" Then
        With Sheets(2)
            For Each cell In .Range("A:A")
                If cell.Value = Forecastyear Then'find first occurrence of year
                    Set Rng = cell
                    Exit For
                End If
            Next
            .Range(Rng.Address).Resize(52, 5).Select'resize for 52 rows and 5 columns
        End With
    End If
End Sub

你现在打算用它做什么?就 cpu 时间

而言,仅选择是相当低效的