tremak
asked on
Open excel file from another excel file using VBA
I need to have a VBA macro stored in a excel workbook that can open up another excel workbook and retrieve values from the 2nd workbook.
Here is the code I have so far. When I run it, I get a run-time error 438 "Object doesn't support this property or method".
Here's my code.
What am I doing wrong?
Sub CreateCsvFile()
Dim intFileNum As Integer
Dim intColNum As Integer
Dim strRec As String
Dim strFileName As String
Dim strDateTime As String
Dim strCell As String
Dim xlApp As Excel.Application
Dim xlWB1 As Excel.Workbook
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlWB1 = xlApp.Workbooks.Open("c:\d ata\cadata .xlsx")
strFileName = "C:\DATA\CaData.csv"
Close #intFileNum
intFileNum = FreeFile()
Open strFileName For Output As intFileNum
strDateTime = Date & " " & Time & ",REV30,"
strRec = strDateTime
' Loop thru and get seven day operating history
For intColNum = 4 To 13
strCell = "C" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "D" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "E" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "F" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "G" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "H" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "I" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "J" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
Next
' Loop thru and get 10 most recent alarms
For intColNum = 4 To 13
strCell = "O" & intColNum
strRec = strRec & Range(strCell) & ","
Next
' Loop thru and get cumulative event counters
For intColNum = 18 To 30
strCell = "G" & intColNum
strRec = strRec & Range(strCell) & ","
Next
Print #intFileNum, strRec
Close #intFileNum
xlWB1.Close
End Sub
Here is the code I have so far. When I run it, I get a run-time error 438 "Object doesn't support this property or method".
Here's my code.
What am I doing wrong?
Sub CreateCsvFile()
Dim intFileNum As Integer
Dim intColNum As Integer
Dim strRec As String
Dim strFileName As String
Dim strDateTime As String
Dim strCell As String
Dim xlApp As Excel.Application
Dim xlWB1 As Excel.Workbook
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlWB1 = xlApp.Workbooks.Open("c:\d
strFileName = "C:\DATA\CaData.csv"
Close #intFileNum
intFileNum = FreeFile()
Open strFileName For Output As intFileNum
strDateTime = Date & " " & Time & ",REV30,"
strRec = strDateTime
' Loop thru and get seven day operating history
For intColNum = 4 To 13
strCell = "C" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "D" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "E" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "F" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "G" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "H" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "I" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
strCell = "J" & intColNum
strRec = strRec & xlWB1.Range(strCell) & ","
Next
' Loop thru and get 10 most recent alarms
For intColNum = 4 To 13
strCell = "O" & intColNum
strRec = strRec & Range(strCell) & ","
Next
' Loop thru and get cumulative event counters
For intColNum = 18 To 30
strCell = "G" & intColNum
strRec = strRec & Range(strCell) & ","
Next
Print #intFileNum, strRec
Close #intFileNum
xlWB1.Close
End Sub
Which line does the code stop on? That is critical to knowing what the problem is.
ASKER
My bad.
The first occurrence of the following line
strRec = strRec & xlWB1.Range(strCell) & ","
The first occurrence of the following line
strRec = strRec & xlWB1.Range(strCell) & ","
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Worked like a charm.
Thanks for the quick assist.
Thanks for the quick assist.
If you don't mind, here are a few other notes on other things I noticed as well:
You don't really need to assign the reference to a variable each time. You could combine it into one line that looks like this and have half as many lines inside your For loops:
strRec = strRec & xlWB1.Range("C" & intColNum) & ","
Also, intColNum seems a bit misleading. If you're referencing the cell by letter and number, then the number is the row number, not the column number. intRowNum would be clearer.
Finally, to improve the efficiency and conciseness of the code, you could use a With statement to reference the sheet rather than stating it each time. The code with all of these shifts and a bit of cleanup is attached.
You don't really need to assign the reference to a variable each time. You could combine it into one line that looks like this and have half as many lines inside your For loops:
strRec = strRec & xlWB1.Range("C" & intColNum) & ","
Also, intColNum seems a bit misleading. If you're referencing the cell by letter and number, then the number is the row number, not the column number. intRowNum would be clearer.
Finally, to improve the efficiency and conciseness of the code, you could use a With statement to reference the sheet rather than stating it each time. The code with all of these shifts and a bit of cleanup is attached.
Sub CreateCsvFile()
Dim intFileNum As Integer
Dim intRowNum As Integer
Dim strRec As String
Dim strFileName As String
Dim strDateTime As String
Dim xlApp As Excel.Application
Dim xlWB1 As Excel.Workbook
Const strFolder As String = "C:\data\"
Set xlApp = New Excel.Application
'Code will run faster when the application is not visible
'xlApp.Visible = True
Set xlWB1 = xlApp.Workbooks.Open(strFolder & "cadata.xlsx")
strFileName = strFolder & "CaData.csv"
Close #intFileNum
intFileNum = FreeFile()
Open strFileName For Output As intFileNum
strDateTime = Date & " " & Time & ",REV30,"
strRec = strDateTime
With xlWB1.Worksheets(1)
' Loop thru and get seven day operating history
For intRowNum = 4 To 13
strRec = strRec & .Range("C" & intRowNum) & ","
strRec = strRec & .Range("D" & intRowNum) & ","
strRec = strRec & .Range("E" & intRowNum) & ","
strRec = strRec & .Range("F" & intRowNum) & ","
strRec = strRec & .Range("G" & intRowNum) & ","
strRec = strRec & .Range("H" & intRowNum) & ","
strRec = strRec & .Range("I" & intRowNum) & ","
strRec = strRec & .Range("J" & intRowNum) & ","
Next
' Loop thru and get 10 most recent alarms
For intRowNum = 4 To 13
strRec = strRec & .Range("O" & intRowNum) & ","
Next
' Loop thru and get cumulative event counters
For intRowNum = 18 To 30
strRec = strRec & .Range("G" & intRowNum) & ","
Next
End With
Print #intFileNum, strRec
Close #intFileNum
xlWB1.Close
'Release set objects
Set xlWB1 = Nothing
Set xlApp = Nothing
End Sub