Consulting

Results 1 to 8 of 8

Thread: Run-time error 1004 trying to select cells

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    82
    Location

    Run-time error 1004 trying to select cells

    Not sure why this is not working. I want to open a raw data file, copy all cells, then paste into a master workbook. At the Cells.Select command, I get RTE 1004.
    Here is the code:

    [vba]
    Sub GetExpFile()
    Set UpFile = ActiveWorkbook
    DataFileName = Application.GetOpenFilename(, , "Get Export File")
    If DataFileName = "False" Then
    Exit Sub
    End If
    Set DataFile = Workbooks.Open(DataFileName)

    Cells.Select
    Selection.Copy
    UpFile.Activate
    Sheets("Sheet1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    DataFile.Close

    End Sub
    [/vba]
    I tried adding a line to activate DataFile before selecting, but that didn't change anything. Any ideas?
    Last edited by Aussiebear; 04-16-2011 at 03:36 PM. Reason: Adjusted to use the correct tags around the code section

  2. #2
    VBAX Regular
    Joined
    Aug 2006
    Posts
    82
    Location
    Follow-up: Instead of Cells.Select, I tried selecting to the right and down from cell A2 (where the data begins; don't need the headers). Now I'm getting RTE 1004 when it goes to select cell A2. Not making any sense.

  3. #3
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    What is the full text of the Error? RTE 1004 can encompass more than one error.

  4. #4
    Super Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,005
    Location
    You must either have a merged cell or something protected as there is nothing wrong with that macro.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    VBAX Regular
    Joined
    Aug 2006
    Posts
    82
    Location
    The full text was "Application-defined or object-defined error".
    The only thing different about DataFile is that it is a .csv file type. I wouldn't think this would have anything to do with it, but maybe it does. If this is causing my problem, what would be the easiest way to do what I'm trying to do? Even if I need to add an intermediate step, that's OK.

  6. #6
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    does datafile.sheets(1).cells.select work?
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  7. #7
    VBAX Regular
    Joined
    Aug 2006
    Posts
    82
    Location
    That does work. Thanks! Still can't figure out why something as simple as the shorter code doesn't work, but at least I can move on now.
    One other thing: When I get to the DataFile.Close command, I get a dialog box telling me that there is a large amount of information on the clipoard and asking me if I want to keep it. Is there a way to tell Excel to skip this dialog?

  8. #8
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    It didn't know which parent object of cells you meant so thats why it failed. As far as the message try


    Application.CutCopyMode = False

    before closing.
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

Posting Permissions

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