PDA

View Full Version : Run-time error 1004 trying to select cells



kualjo
04-15-2011, 12:30 PM
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:


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

I tried adding a line to activate DataFile before selecting, but that didn't change anything. Any ideas?

kualjo
04-15-2011, 01:14 PM
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.

mbarron
04-15-2011, 08:32 PM
What is the full text of the Error? RTE 1004 can encompass more than one error.

Simon Lloyd
04-15-2011, 09:31 PM
You must either have a merged cell or something protected as there is nothing wrong with that macro.

kualjo
04-16-2011, 08:43 AM
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.

BrianMH
04-17-2011, 01:17 AM
does datafile.sheets(1).cells.select work?

kualjo
04-17-2011, 08:26 AM
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?

BrianMH
04-17-2011, 08:31 AM
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.