Other Basic conversion to vb

  • Thread starter Thread starter John
  • Start date Start date
J

John

I am old Power Basic programmer. (In DOS)

to open file and write to it looked like this:

Open "filename" as #1 for output as (then you select the type of file
you want)

then to output to that file you use print... like

Print #1, whatever

When your done you use close #1


What is the vb equivalent? How do you open a file, tell what kind it is
then write to it and close it?

I've looked in the help files under open, file, close, & save and
haven't found it.

Thanks

John
 
John,

The syntax is the same. See help for the Open statement (not the
method of the Workbook object) in VBA help.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
This is low level file io and hasn't changed. (primarily used for text
files although random access and binary access are supported)

If you want to open a workbook


Workbooks.Open "C:\MyFiles\Myfile2.xls"

What is it you are trying to do?
 
You have already got the most accessible reference: if you have Excel.
Press Alt + F11 then F1 and search for the keywords. As regards files, what
is true of VBA is also true of VB.
 
I wanted to save a PART of a workseet to a text file. Below is my
attempt using macro record (commented) and my sort of down and dirty one
which works well enough. The commented one saves the entire workseet. I
couldn't make it stop doing that.

The types to change into strings I found by trial and error.

John

Sub SaveSheet()
'
'
' *** Best I could do using record macro
' *** This saves entire worksheet, not the selected part
' *** In addition it changes the canme of the sheet in the Workbook
' Sheets("MN INV").Select '
' Range("A16:E28").Select
' ChDir "C:\Temp"
' ActiveWorkbook.SaveAs Filename:= _
' "C:\Temp\TestThisPuppy.txt" _
' , FileFormat:=xlText, CreateBackup:=False
' *** End Attempt using Macro Recording
'

Dim PutIt As Variant
Dim Row, Col as Byte
Sheets("MN INV").Select
Open "C:\temp\TestThisPuppy.txt" For Output As #1

For Row = 16 To 28
PutIt = ""

For Col = 1 To 5
Select Case VarType(Cells(Row, Col))
Case 5 To 7: PutIt = PutIt + Str$(Cells(Row, Col))
Case Else: PutIt = PutIt + Cells(Row, Col)
End Select
If Col < 5 Then PutIt = PutIt + Chr$(9)
Next

Print #1, PutIt
Next

Close #1
End Sub
 
Sub SaveSheet()
Dim sh as Worksheet, sh1 as Worksheet
set sh = Sheets("MN INV")
workbooks.Add xlWBATWorksheet
set sh1 = ActiveSheet
sh.Range("A16:E28").copy sh1.Range("A1")
sh1.Parent.SaveAs Filename:= _
"C:\Temp\TestThisPuppy.txt", _
FileFormat:=xlText, CreateBackup:=False
sh1.parent.Close SaveChanges:=False ' Already Saved.
End sub

32 bit processor, so row and col will be converted to long anyway. Also

Dim row, col as Byte

row is a variant, col is a byte

+ is overloaded for concatenation, so better to use the & which is the
concatenation operator.

Str$(Cells(Row, Col)) puts a space in front of the value in Cells(row,col).
Maybe what you want or not. cStr(cells(row,col)) would not put in a space,
however & Cells(row,col) would coerce it to a string anyway without
explicit conversion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top