Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15722

[RESOLVED] Writing to Excel

$
0
0
I am running VB6 on WIN10

I need to write to two different Excel workbooks. The code is almost identical as shown below:

Code:

Private Function WriteToHistoryH()

    'Get the file name
    strDirectory = "C:\My VB6 Executables\Details\Details\"
    strFileName ="History H.xls"ETF
   
    'create Excel object
    Set ExcelApp = CreateObject("Excel.Application")


    'open the workbook
    Set ExcelWorkbook = ExcelApp.Workbooks.Open(strDirectory & strFileName)
    Set ExcelSheet = ExcelWorkbook.Worksheets(1)
   
    'Add Today's Data
    With Sheets("HistoryH")

        ....Detail code here.....

'***  Close the  Workbook
    ExcelWorkbook.Save


    'Close Excel
    ExcelWorkbook.Close savechanges:=False
    ExcelApp.Quit
    Set ExcelApp = Nothing
    Set ExcelWorkbook = Nothing
    Set ExcelSheet = Nothing
 
End Function

The second function is called Private Function WriteToHistoryV(). The strFileName is changed to "History V.xls" . And the With Sheets("HistoryH") is changed to With Sheets("HistoryV")

No matter which order I run these two procedures, the second one crashes. I get Run-time error '1004'; Method 'Sheets' of object "_Global' failed

Can anyone explain to me what is happening? Did I cone something wrong?

Thanks

Viewing all articles
Browse latest Browse all 15722

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>