Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default How to pass file references to a sub in VBA

This might be a simple question for you, but it's not for me even after years
of VB...

Within one of the procedures I am writing, at several different places I
need to flush the content of a string variable into a text file on disc.

Instead of repeating several times the same instructions which write the
variable to the file, I wonder whether it is possible to create a dedicated
SUB that does it for me. In this case, how would I pass the reference to the
file to that SUB?

See exemple below if needed.

Thank you.

Stefano Gatto


Sub CreateFileWithNiceNumbers()
Dim lngNumber As Long
Dim intCounter As Integer

Open "C:\temp\NiceNumbers.txt" For Output As #1
For intCounter = 1 To 250
lngNumber = Int(1000 * Rnd + 1) 'random number between 1 and 1000
If lngNumber 500 And lngNumber < 600 Then
Print #1, "A nice number is " & lngNumber
End If
Next intCounter

For intCounter = 1 To 80
lngNumber = Int(400 * Rnd + 1) + 300 'random number between 300 and
700
If lngNumber 500 And lngNumber < 600 Then
Print #1, "A nice number is " & lngNumber
End If
Next intCounter
End Sub

I would like to have this repeating code:

If lngNumber 500 And lngNumber < 600 Then
Print #1, "A nice number is " & lngNumber
End If

in one single place, like "Sub WriteToFile(lngNumber)". How would I pass the
file reference?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default How to pass file references to a sub in VBA

Stefano,

Use code similar to the following:

Option Explicit
Public FileNumber As Integer

Sub OpenFile()
FileNumber = FreeFile
Open "C:\Test.txt" For Output Access Write As #FileNumber
End Sub

Sub WriteIt(Txt As String)
Print #FileNumber, Txt
End Sub

Sub CloseFile()
Close #FileNumber
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)



"Stefano Gatto" wrote in message
...
This might be a simple question for you, but it's not for me even after
years
of VB...

Within one of the procedures I am writing, at several different places I
need to flush the content of a string variable into a text file on disc.

Instead of repeating several times the same instructions which write the
variable to the file, I wonder whether it is possible to create a
dedicated
SUB that does it for me. In this case, how would I pass the reference to
the
file to that SUB?

See exemple below if needed.

Thank you.

Stefano Gatto


Sub CreateFileWithNiceNumbers()
Dim lngNumber As Long
Dim intCounter As Integer

Open "C:\temp\NiceNumbers.txt" For Output As #1
For intCounter = 1 To 250
lngNumber = Int(1000 * Rnd + 1) 'random number between 1 and 1000
If lngNumber 500 And lngNumber < 600 Then
Print #1, "A nice number is " & lngNumber
End If
Next intCounter

For intCounter = 1 To 80
lngNumber = Int(400 * Rnd + 1) + 300 'random number between 300 and
700
If lngNumber 500 And lngNumber < 600 Then
Print #1, "A nice number is " & lngNumber
End If
Next intCounter
End Sub

I would like to have this repeating code:

If lngNumber 500 And lngNumber < 600 Then
Print #1, "A nice number is " & lngNumber
End If

in one single place, like "Sub WriteToFile(lngNumber)". How would I pass
the
file reference?



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass parameter to launch batch file with variable from excel Frank Pozzuto New Users to Excel 0 January 15th 10 06:17 PM
Can I pass arguments to my formatted empty spreadsheet file? Paul H[_2_] Excel Discussion (Misc queries) 3 July 25th 09 07:34 PM
Using a Loop Statement to Pass Data between WKS and Output File Belzeddar Excel Programming 2 January 14th 04 07:40 AM
Saving a file with VBA that references current file name chilidog1000 Excel Programming 2 October 17th 03 08:14 PM
Pass file name from Spreadsheet Range Bruce Roberson Excel Programming 5 August 16th 03 12:10 AM


All times are GMT +1. The time now is 11:54 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"