当使用Range(Selection, select . end (xlUp)). select时,运行时错误1004



我试图打开一个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

你能告诉我代码有什么问题吗?

Thanks in Advance.

问候,Prem

如果您想选择B2开始的B列中已填充的部分,则:

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

最新更新