Easiest way to write data to a file

I have a couple of macros that periodically act up. I say
periodically, because some of the calculations are based on random
numbers. I would like to have them write a few lines of debug data to
a side file that I can examine after the macro completes.

I wrote the Write2File subroutine below and the TestWrite2File routine
to test it. It works except that it overwrites the previous data.

How do I get it to append the data the the file?

Are there any other improvements I should make to these routines?


'***** Routine to test Write2File *****
Public Sub TestWrite2File()
Dim counter

For counter = 1 To 5
  Write2File "C:\temp.txt", "Counter = " & counter
Next counter

End Sub


'***** Write2File routine *****
Public Sub Write2File(FileID, Data)

Dim FileNumber

FileNumber = FreeFile                  ' Get unused file number
Open FileID For Output As #FileNumber  ' Open the file
Write #FileNumber, Data                ' Write the data
Close #FileNumber                      ' Close the file

End Sub
0
Prof
1/6/2010 2:16:03 AM
excel 39879 articles. 2 followers. Follow

1 Replies
559 Views

Similar Articles

[PageSpeed] 31

On Tue, 05 Jan 2010 18:16:03 -0800, Prof Wonmug <wonmug@e.mcc> wrote:

>I have a couple of macros that periodically act up. I say
>periodically, because some of the calculations are based on random
>numbers. I would like to have them write a few lines of debug data to
>a side file that I can examine after the macro completes.
>
>I wrote the Write2File subroutine below and the TestWrite2File routine
>to test it. It works except that it overwrites the previous data.
>
>How do I get it to append the data the the file?
>
>Are there any other improvements I should make to these routines?
>
>
>'***** Routine to test Write2File *****
>Public Sub TestWrite2File()
>Dim counter
>
>For counter = 1 To 5
>  Write2File "C:\temp.txt", "Counter = " & counter
>Next counter
>
>End Sub
>
>
>'***** Write2File routine *****
>Public Sub Write2File(FileID, Data)
>
>Dim FileNumber
>
>FileNumber = FreeFile                  ' Get unused file number
>Open FileID For Output As #FileNumber  ' Open the file
>Write #FileNumber, Data                ' Write the data
>Close #FileNumber                      ' Close the file
>
>End Sub

I found the answer. I need to open the file "for Append" rather than
"for Output":

   Open FileID For Append As #FileNumber  ' Open the file

Is it still best to close the file after every write?
0
Prof
1/6/2010 3:18:16 AM
Reply:

Similar Artilces:

PDF files
I am using Office 2001 on and iMac with os9.2. Can anyone tell me how save/covert a word document to a pdf file? Thanks-T-nix Tom Shadonix <musicoffice@rew2000.com> wrote: > I am using Office 2001 on and iMac with os9.2. Can anyone tell me how > save/covert a word document to a pdf file? Thanks-T-nix Under MacOS 9, you'll need to get Adobe Acrobat (the full version). There is no native way to "print to PDF" from MacOS 9. Corentin -- --- Mac:MS MVP (Francophone) --- http://www.mvps.org - http://mvp.support.microsoft.c...

save excel file to xml
how can I save a excel file to xml? Thanks, qq In Excel 2003, save as XML spreadsheet. "XML spreadsheet" is a drop-down choice in the File Type shown in the Save As dialog box. Bill <quickcur@yahoo.com> wrote in message news:1104798698.421024.161510@z14g2000cwz.googlegroups.com... how can I save a excel file to xml? Thanks, qq ...

How to stop getting the file save box when running a macro
I am running a macro that take a few minutes. Half way through I get a box saving do you want to save the file whith the normal 'Skip, Cancel' options. How can I stop getting this whilst the macro is running? Thanks It sounds as if Autosave is kicking in. You would need to turn it off while your code is running. Here are some routines I wrote a long time ago for that purpose. I hope they work with your Excel version these days but, fwiw... Sub DisableAutosave() ToggleAutoSave False End Sub Sub EnableAutosave() ToggleAutoSave True End Sub Sub ToggleAutoSave(Setting ...

Excel 97
Hi ExcelForum, First time aboard! Something that I have seen in the past, but just cannot figure out ho to do. Problem is what I recall involved the plus and minus sign being off to the left of the worksheet. I have a spreadsheet with months in merged cells above the days of th month below i.e. C2 is a merged cell "January" and cells C3 to AG3 ar individual cells with the days of the month 1,2,3, etc. This goe along the width of the worksheet for 6 months or so. February begins a AH2 and the days are AH3 to BJ3. What I am trying to do is compact all of January by clicking on a mi...

Can't deselect data in a Pivot Table...
Hi all, Does anyone know why it is that I am unable to deselect data in a drop down list in my Pivot Table? The boxes have ticks in but when I click them to hide them, nothing happens. Many Thanks Andy ...

I wanta infopath form to submit data to an excel databse file
...

Limiting files to open to only files with certain name.
I have a User Form with a Control Button that is for opening Exsisting Files. I only want to Open Files with the word "Spec" in the name? Is there a way to only show & Open Excel Files with "Spec' in the name? I have the following code that opens the Dialog Box, but it shows all the files with the ".xlsm" extension. I want to narrow it down to only files with "Spec" in the name. It would be nice if it would show all the differnt excel file extensions. ' Open Existing Eng Spec 9 Control Button Private Sub Open_Existing_Eng_Spec...

How to backup exchange data folder ?
I want to keep a good backup of my exchange 2003. I am looking for some instructions that stops the services, copy exchange public and private folder and other required data files and restart the services. Can I create something like that in a batch file and get the back every now and then ? I would really appreciate if someone please send me some instructions. A good backup? The best thing to do is backup the system state, etc, plus the actual Exchange information stores via an online Exchange 'aware' backup (either Windows Backup on the actual Exchange server itself or so...

Will restoring CRM 3.0 databases to remove bad data cause offline client issues?
We recently did an import into our CRM 3.0 install, and would like to restore our CRM Db's to remove that data. If we have Outlook clients that are currently online, but have gone offline and back online agains since the bad data was imported, what will the effect on them be? Will the data that is removed from restoring the databases still be on the laptops? Or will the next time they go offline will it be removed? Thanks! John Fink ...

Data Validation Formatting
In a cell where I have both a comment and a data validation, the data validation list defaults to a very small font. I removed the comments and still the list appears very small. In general, is there a way to format the font/appearance of the data validation list as it appears in the dropdown? Thanks Hi no chance to change the format but see: http://www.contextures.com/xlDataVal08.html#Larger for a workaround -- Regards Frank Kabel Frankfurt, Germany "Andy" <anonymous@discussions.microsoft.com> schrieb im Newsbeitrag news:13b3101c44425$66a8be30$a001280a@phx.gbl... >...

Opening a New Excel file?
On my home computer (MS Office 2000) when I open 2 excel files, there are two copies of Excel that open. Kinda like how MS Word does it. But my work Excel, opened all the sheets in 1 excel. So I have to use Window->FILENAME to see it. But at home, I just use alt-tab. How do I set this up? Is there a setting? Thanks Hi Mike. Try Tools - Options - View - check Windows in task bar HTH >-----Original Message----- >On my home computer (MS Office 2000) when I open 2 excel files, there >are two copies of Excel that open. >Kinda like how MS Word does it. > >But my work Exce...

Exchange 2003 Page File
I am trying to find the best practices for setting up page files on Exchange 2003. I have read things like memory x 1.5 or memory x 2 or even higher. I have read about putting the pagefiles in multiple directories on the same physical drive (there is a technet article on this) - but nothing that pulls it all together. Like a mailbox server with 4 gB ram. Mirrored C: (OS) Mirrored D: (logs) RAID E: (data) - where would I put the page fiels, and how many woudl I create? Do we ever let the system manage the page file? Would the same thing happen on a bridgehead or public folder server? Ho...

File format converter won't work
This is a multi-part message in MIME format. ------=_NextPart_000_000E_01C5F418.4A470D10 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable After several months without problem, Publisher 2003 now will not open a = text file created in Word. I get this error message after I try to = insert it into a text box: "Publisher cannot start the file format converter C:\program = files\Microsoft shared\Textconv\msconv97.dll" I KNOW msconv97.dll is there. I've run the "Fix" program and also = uninstalled Publisher and then r...

error writing to adodb.dll on 1.2 to 3.0 upgrade
We need some help. We did an upgrade of a 1.2 CRM server yesterday to 3.0. It had an error "Error writing to file:adodb.dll. Verify that you have access to that directory." The install log ends with SQL Server Reporing Services failed. The CRM pages come up, but most links error out. Where do I turn now? Does anyone know what permission it's missing? Then, what do I do with this install? Can I rerun in the upgrade or reinstall again? Thanks for any help. ...

Data on Internet
Is it possible and practical to operate a database where the data is held on the internet so that it can be accessed and modified by a select few? If so where should I start reading? Would I nead any additional software, we are using Access 2000 Colin ms-access is not a web system. It does not have web development features, and is complete the wrong product and technology to use for the web. You would no more use an auto-Cad program to write games for computers then you would use ms-access to try and create web based systems. They are simply different horses for different courses. On the o...

Aligning Decimal Points with non-numeric data
I am creating a table and would like to align my data by the decimal point. I understand that the accounting format will do this, however my data sometimes includes symbols. For example 1.23 7.94 8.45** (3.12) 5.88 The accounting format seems to deal well with the (3.12), but I can't get the decimal places to align for the 8.45**. Any advice??? Also the accounting format won't allow me to indent. Thanks in advance! This format code (Format->Cells->Number tab->Custom) seems to work fo me. 0.00_*_*_);(0.00)_*_*;0.00_*_*_);@_) Make sure the alignment is set to right ...

Is there any way to get rid of a link in a spreadsheet?
I copied a wooksheet to keep a format but changed the content. The original worksheet was linked to another. The new one is not but I can't get rid of the link. Couldn't find anything in help. Any way to get rid of a link? You need to find the link and delete the reference. To replace the reference in formulae you can either delete the entire formula or use Edit/Replace and replace the file reference with "". (It helps if the link-to file is open as the reference won't include the path. If it is in a named range or formula and there are many the process gets cu...

Cell data overruns into adjacent cell(cells).
I have one entry that is drastically longer than the data that follows, and I want it to overrun into the adjacent cells. It used to, but now it won't. I've tried left justification, but it doesn't seem to help. Any ideas? Whether you want or don't want it to run over, you can probably accomplish this by right clicking the cell, column or row and select Format Cells and under Alighment see if Wrap Text is checked. Just a thought. ...

File Frozen
I have a file on Publisher 2003 that I was able to edit, and I came back to it today and am not able to access it. None of my tools show up as accessible either.... help! ...

Newbie: trouble validating xml file
IDE: Visual Studio 2003 .NET (7.1.3088) OS: XP Pro ..NET Framework 1.1 (1.1.4322) I'm experimenting with xml in .net, and having trouble to get a xml file validated without any errors/warnings in VS . So I searched the web and found this example at http://support.microsoft.com/default.aspx?scid=kb;en-us;Q307379&ID=kb;en-us;Q307379&SD=MSDN section "Use Namespaces in the XSD Schema" ProductWithXSD.xml: <?xml version="1.0"?> <Product ProductID="123" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="ur...

Show data if more than one record in group
I need for my report to list data only if there is more than one record in a group. Please help! Use a query or subquery to count records and then use as criteria to only pull records if there are more than one in a group. -- KARL DEWEY Build a little - Test a little "jcade4" wrote: > I need for my report to list data only if there is more than one record in a > group. Please help! Add a text box named as text on header section with Control Course: =Count([ID]) Chose Code under View menu and paste the following code Private Sub Detail_Print(Cancel As Integer, Pr...

How to find/replace data in column
Hi, In Excel I have a column of 0's as text and I want to change the whole column to 1 but still as text. How can I do this? I tried a find/replace, but the 1 was entered as a number not as text. Thanks Rock Thank you Gord... The column I wish to change is actually K. Does this mean the A1 in the formula should in fact be K1 ? Rock Gord Dibben wrote: > Rock > > In an adjacent column. > > =IF(A1="0",TEXT(A1,"1")) > > Drag/copy down. > > > Gord Dibben Excel MVP > > > On Wed, 16 Nov 2005 08:26:57 +1100, Rock <194...

Count missing data
Hello, I would like to count missing data in a Column range due to the criteria in another column range. The problem I have is that the "Birth Date" column range has a "vlookup" formula in it, which sometimes formulates a Blank. I need to count these Blank cells when the Status = "A". Example: Birth Date Status 02/03/71 A A 12/02/58 A 09/15/67 A A 11/21/79 A Missing = 2 Thanks Ruan Try this: =SUMPRODUCT((A2:A100={" ",""})*(B2:B100="a")) -- HTH, RD...

Turn off data access warning
I have publisher files that are linked to a Microsoft Access Table. Everytime I open one of these publisher files I get a warning that asks if I want to open the publication and access the external data. Is there any way to turn off this warning? Not sure any of these articles will be helpful Description of Office features that are intended to enable collaboration and that are not intended to increase security http://support.microsoft.com/kb/822924/en-us Do you have Norton? How to use Office programs with the Norton AntiVirus Office plug-in http://support.microsoft.com/kb/329820/en-us...

Changing format of Data Validation's Input Message
I'm using Data Validations. The cell has an input message, which appears by default as a rectangular bubble right under the cell. Is it possible to change the format of this bubble, to different colours, fonts, borders, etc? I want to make it more noticeable. I didn't see any formatting capabilities in that dialog. Hall wrote: > > I'm using Data Validations. The cell has an input message, which appears by > default as a rectangular bubble right under the cell. > > Is it possible to change the format of this bubble, to different colours, > fonts, borde...