Excel 2010 - VBA Variable Issue

JamesgTx

New Member
Joined
Jan 6, 2010
Messages
25
I have created a macro that writes certain data from my worksheet into a text file. This text file will be read by a separate program so I need the lines of the text file to be in a certain format. The format for the line of text is

1st 8 spaces: one or two digit integer right justified
spaces 9-24: number displayed as xxx.yyyyy but right justified so I can have up to 11 decimal places
spaces 25 - 39: Same as spaces 9-24

My code reads as follows:

intFH = FreeFile()

Open "Node.txt" For Output As intFH

Dim DNX As Single
Dim DNY As Single
Dim k As Integer

' Loop to print out node data - Note that Z dimesion is always zero for 2D Axisymmetric Models
For i = 1 To 9
DNX = Cells(i + 4, 10).Value
DNY = Cells(i + 4, 11).Value
Print #intFH, " " & i & " "; DNX & " " & DNY
Next i

For i = 1 To 43
DNX = Cells(i + 13, 10).Value
DNY = Cells(i + 13, 11).Value
k = i + 9
Print #intFH, " " & k & " "; DNX & " " & DNY
Next i


'Close File
Close #intFH

I split up the loop into two separate parts trying to get the output to line up correctly. I am thinking that there is an easier and more elegant way to handle this.

Any help is greatly appreciated.

Thanks so much.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Why not insert a 'vbTab' between the variables - that would solve the issue

it would look something like this

Code:
open FileName for output as #1
for i=0 to 10
      Print #1, Var1(i) & vbtab & Var2(i) & vbtab & Var3(i) ' This adds a carry return automatically
next i
close #1
 
Upvote 0
ohhh - you will need a second loop - but the point is using vbtab as the delimiter
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top