+ Reply to Thread
Results 1 to 4 of 4

Failure to compile - due to "Selection"

  1. #1
    Registered User
    Join Date
    12-16-2010
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    1

    Failure to compile - due to "Selection"

    Hi everyone,

    Can anyone help with the following problem please. The macro below should open some files, copy data into excel, save the excel file and then cycle round. However, it fails to compile/run with the message:
    "Compile error: Expected Function or variable"
    and points to the first occurence of the word selection, ie the line

    Range(Selection, Selection.End(xlDown)).Select

    If I comment that line, it just makes it point to the next occurence of selection and so on.

    I do not understand why it does this as almost identical code in other workbooks functions fine. Is there something really obvious I am missing, such as changing some setting somewhere?
    Any help or advice would be much appreciated.

    The code is as follows:

    Sub FileOpener()

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    Dim File_Names As Variant
    Dim FFF As String
    Dim Temp_File_Name As String
    Dim Source_Temp_File_Name As String
    Dim i As Integer
    Dim Full_Path_Name As String
    Dim Text_File_Name As Variant
    Dim Name_for_File As String

    ChDrive ("C")
    ChDir ("C:\Projects\Data")
    FFF = "Excel Files (*.xls), *.xls," & "Text Files (*.txt), *.txt," & "All files (*.*), *.*"
    File_Names = Application.GetOpenFilename(FFF, 2, "Select files for import ...", , MultiSelect:=True)

    If IsArray(File_Names) = False Then
    If File_Names = False Then
    MsgBox "Import cancelled by user"
    Exit Sub
    End If
    End If

    For i = LBound(File_Names) To UBound(File_Names)
    Source_Temp_File_Name = ThisWorkbook.Name
    Full_Path_Name = File_Names(i)
    Text_File_Name = Split(Full_Path_Name, ".")
    Name_for_File = Text_File_Name(0) & ".xls"

    Workbooks.Open File_Names(i)
    Temp_File_Name = ActiveWorkbook.Name
    Windows(Temp_File_Name).Activate
    Range("B2:C2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows(Source_Temp_File_Name).Activate
    Sheets("Data").Activate
    Range("P3:Q2000").Select
    Selection.Clear
    With Selection.Interior
    .ColorIndex = 36
    .Pattern = xlSolid
    End With
    Range("P3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Range("C1").Select
    Windows(Temp_File_Name).Close
    Windows(Source_Temp_File_Name).Activate
    ActiveWorkbook.SaveAs Name_for_File

    Next

    End Sub

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,477

    Re: Failure to compile - due to "Selection"

    The good news: The line works in isolation.

    Your code also works for me, barring the fact that I am missing folders, files and worksheets that are referred to in the code.

    The bad news: I think you'll need to step through the code and check that all the variables have the values you expect them to have.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Failure to compile - due to "Selection"

    I think your code could be simplified:
    Please Login or Register  to view this content.
    Please take a few minutes to read the forum rules about CODE tags and use them in future posts.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    12,305

    Re: Failure to compile - due to "Selection"

    Do you have a function/sub or module called Selection?
    Remember what the dormouse said
    Feed your head

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1