I really need some help -- I've been searching forums for 3 days and have seen similar problems but none of the solutions have solved my problem. I'm writing a macro for multiple users (most have Excel 2003, some have 2007) to open a .csv file, do some cleanup, save in Excel, then open a prn file, merge some columns from there to the first csv and save the newly created file as final Excel file. The csv files and the prn files will always have different names. I did change the "Name" part of the .Add method to a standardized name so that I'll be able to reference it later -- I hope that didn't mess anything up.
My problem is opening that csv file. I'm getting a "Run-time error 1004" error on "Method 'Range' of object'_Global' failed. It hangs up on the "With ActiveSheet.... Destination:=Range" line of code. The csvFileName variable is getting populated so I know that part is okay. So I'm guessing it's because Excel doesn't know where to deposit the file it just picked up even though my code is telling it to put it in Cell A1. (I'm writing this in 2003 and then going to create the macro for 2007 secondly.) I don't have a worksheet open when the macro runs, could that be part of the problem? Even when I try having one open, I still error out.
Here's the beginning part of the code.....(the rest works) Thank you!
(this is the first time I've ever posted anywhere so I apologize for any social faux pas.)
VB:
==================
Sub CW2_Input()
'
' CW2_Input Macro
' Cleans up IOT raw data. Input box asks for Store Number and MFR code.
Dim LR As Long
Dim strSTID As String
Dim strLINCODE As Variant
Dim csvFilename As String
Dim Response As Variant
Dim IOTfileName As String
'===================
csvFilename = Application _
.GetOpenFilename("Comm Separated Value Files (*.csv), *.csv")
If csvFilename <> False Then
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & csvFilename _
, Destination:=Range("$A$1"))
.Name = "IOT Data"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, _
2, 2, 2, 2, 1, 1, 1, 2, 2, 9)
.TextFileTrailingMinusNumbers = True
.Refresh
End With
End If
If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines