Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
dim xl as Excel.Application
set xl = New Excel.Application
xl.workbooks.open "MyPath\MyWorkbook"
xl.Visible = true
and just considered "value1" as being the call to the function. My mistake.value1 = xlSht.Cells(5,7)
Function WriteResultFile(value1)
Me too, but you put me on to it.I missed the fact the function is not called.
sub my_sub()
Dim xlApp, xlBook, xlSht
Dim filename, value1, retval
filename = "c:\ek\ek.xls"
Set xlApp = CreateObject("Excel.Application")
set xlBook = xlApp.WorkBooks.Open(filename)
set xlSht = xlApp.activesheet
retval = writeresultfile(xlSht.Cells(5,7).value)
xlBook.Close False
xlApp.Quit
end sub
Function WriteResultFile(value1)
Const ForReading=1, ForWriting=2, ForAppending=8
filename2 = "c:\ek\ek.TXT"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(filename2, ForWriting, True)
ts.WriteLine(value1)
ts.Close
WriteResultFile=0
'always deallocate after use...
set ts = Nothing
set fso = Nothing
end function
Dim xlApp, xlBook, xlSht
Dim filename, value1
filename = "c:\ek\ek.xls"
Set xlApp = CreateObject("Excel.Application")
set xlBook = xlApp.WorkBooks.Open(filename)
set xlSht = xlApp.activesheet
value1 = xlSht.Cells(5,7).value
Const ForReading=1, ForWriting=2, ForAppending=8
filename2 = "c:\ek\ek.TXT"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(filename2, ForWriting, True)
ts.WriteLine(value1)
ts.Close
WriteResultFile=0
'always deallocate after use...
set ts = Nothing
set fso = Nothing
xlBook.Close False
xlApp.Quit
Sub WriteResultFile(value1)
Const ForReading=1, ForWriting=2, ForAppending=8
filename2 = "c:\ek\ek.TXT"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(filename2, ForWriting, True)
ts.WriteLine(value1)
ts.Close
'WriteResultFile=0 Get rid of this line
'always deallocate after use...
set ts = Nothing
set fso = Nothing
end Sub
WriteResultFile xlSht.Cells(5,7).value
[/cide]
Finally, you use a Sub typically when you need to reuse functionality in the context of your program, so you don't write the same code twice in two places. It's not clear that you need to do this, so you can consider just not using a separate procedure at all, as Frederico's last piece of code demonstrates.
It's important to understand how Subs and Functions work. If you need to reuse code in your application, put it in a procedure (a procedure is a sub or a function). If you need to return a value from the procedure, use a function, otherwise use a sub. In other words, as a general rule, only add complexity when you need it.
HTH
Bob