+ Reply to Thread
Results 1 to 4 of 4

Thread: 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 TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,230

    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

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

    Re: Failure to compile - due to "Selection"

    I think your code could be simplified:
    Sub FileOpener()
        Const sPath     As String = "C:\Projects\Data"
        Dim avsFile     As Variant
        Dim vsFile      As Variant
    
        Application.ScreenUpdating = False
    
        ChDrive sPath
        ChDir sPath
        avsFile = Application.GetOpenFilename(FileFilter:="Text Files (*.txt), *.txt", _
                                              Title:="Select files for import ...", _
                                              MultiSelect:=True)
        If Not IsArray(avsFile) Then
            MsgBox "Import cancelled by user"
            Exit Sub
        End If
    
        For Each vsFile In avsFile
            With ThisWorkbook.Worksheets("Data").Range("P3:Q2000")
                .Clear
                .Interior.ColorIndex = 36
                .Interior.Pattern = xlSolid
            End With
    
            Workbooks.Open vsFile
    
            With Range("B2:C2")
                .Range(.Cells, .Cells.End(xlDown)).Copy
                ThisWorkbook.Worksheets("Data").Range("P3").PasteSpecial Paste:=xlPasteValues
                .Worksheet.Parent.Close SaveChanges:=False
            End With
            
            ThisWorkbook.SaveAs Left(vsFile, InStrRev(vsFile, ".")) & "xls"
        Next
    End Sub
    Please take a few minutes to read the forum rules about CODE tags and use them in future posts.

    Thanks.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: Failure to compile - due to "Selection"

    Do you have a function/sub or module called Selection?

+ 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.2.0