复制范围由输入框指示

时间:2015-07-17 12:08:55

标签: excel vba excel-vba range inputbox

我正在寻找能够将Excel工作表中的特定范围复制到新工作表的模型。目前我有五列。第一列是年份。我想表明,例如inputbox应该复制哪一年。在B栏中,显示了一年中的几周。在列C中直到E,记录特定数据。结果,我的表​​格看起来像这样:

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年应该复制到下一张表。到现在为止我编写/编译了以下宏:

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

这将选择包含年份值的第一个单元格。我想为选定单元格的行和接下来的51周(共52周)选择完整的5列。如何选择所有数据?

1 个答案:

答案 0 :(得分:0)

使用此

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时间方面效率很低