使用Range(Selection,Selection.End(xlUp))时运行时错误1004。选择

时间:2014-05-30 10:45:16

标签: excel excel-vba vba

我正在尝试打开excel工作簿,在单元格B2中向下应用公式。到现在为止还挺好。然后从下到上选择列B,我收到错误。代码如下:

Sub GetImportFileName()
    Dim Finfo As String
    Dim FilterIndex As Integer
    Dim Title As String
    Dim FileName As Variant
    Dim wb1 As Workbook
    Dim wb2 As Workbook

'    Set up list of file filters
    Finfo = "Excel 2007 Files (*.xls),*.xls," & _
    "Excel 2010 Files (*.xlsx),*.xlsx,"

'     Set the dialog box caption
    Title = "Select a File to Import"
'     Get the filename
    FileName = Application.GetOpenFilename(Finfo, _
    FilterIndex, Title)
'     Handle return info from dialog box
    If FileName = False Then
        MsgBox "No file was selected."
    Else
        Set wb2 = Workbooks.Open(FileName) 'New Workbook is open
    End If


  wb2.Worksheets("Sheet1").Activate

  Set wb2 = ActiveWorkbook


  Sheets("Sheet1").Range("A1").Select

   Do While ActiveCell.Value <> Empty
          'Range("B1").Select
          ActiveCell.Offset(0, 1).Select
          ActiveCell.FormulaR1C1 = "=+""'""&RC[-1]&""',"""
          Selection.Copy
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
          ActiveCell.Offset(1, -1).Select
    Loop

    ActiveCell.Offset(-1, 1).Select


    Range(Selection, Selection.End(xlUp)).Select ' **Here I am getting the Run Time Error 1004**

'    Selection.Copy

End Sub

你能帮我解决一下代码有什么问题吗?

先谢谢。

此致

炳廷

1 个答案:

答案 0 :(得分:0)

如果您想从 B2 开始选择列B的填充部分,那么:

Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row).Select