Excel VBAWriting a .txt file to an Array or Wrksht

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Array Excel File
Click For Summary

Discussion Overview

The discussion revolves around using VBA to read a .txt file and store its contents into an array or worksheet cells. Participants explore methods for handling character data, particularly from files generated by Mathematica, and address issues related to reading and processing strings in VBA.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to load characters from a .txt file into a 1-dimensional array, expressing uncertainty about handling whitespace.
  • Another participant suggests that if a delimiter is needed, a different text function might be more appropriate than a split function.
  • Questions arise about whether the difficulties lie in opening the file, reading it, or processing the string afterward, with a request for clarification on the participant's familiarity with VBA.
  • A participant describes their goal of creating a tool to decode Mathematica's output, noting the complexity of the syntax and considering whether processing should occur within Mathematica instead of externally.
  • Concerns are raised about the peculiarities of Mathematica's output, including the use of backslashes and parentheses.
  • One participant shares their code for reading characters into cells and expresses confusion over why certain characters do not appear when stored in an array.
  • Another participant points out that the loop counter should not be incremented within the loop, which could affect the execution of the loop.
  • A later reply indicates that removing the increment of the loop counter resolved the participant's issue, suggesting that only odd-numbered characters were being stored previously.

Areas of Agreement / Disagreement

Participants generally agree on the challenges of reading and processing text files in VBA, but there are differing opinions on the best approach to handle Mathematica's output and the specifics of character handling in arrays.

Contextual Notes

Some limitations include the dependence on the specific formatting of Mathematica's output and the unresolved nature of how to best process such files externally. There are also unresolved questions about the handling of whitespace and special characters in the context of VBA.

Who May Find This Useful

Readers interested in VBA programming, particularly those dealing with text file manipulation and character processing, may find this discussion relevant.

Saladsamurai
Messages
3,009
Reaction score
7
Go To Post #7 for new Question :smile:

So let's that I have a .txt file called input.txt that simply contains the following:


abc+123

I want to load input.txt using VBA into a 1-dim array called MyArray. I want each character to be an element of the array. That is a is MyArray(1), c is MyArray(3) "+" is MyArray(4) etc.

I am not sure about whitespace. If it is easier, whitespace could be considered a character.

Any ideas?

Edit: ALternatively, each charecter could got to a cell...I just can't figure out how to set the dilmeter since there is not one...
 
Last edited:
Physics news on Phys.org
Saladsamurai said:
So let's that I have a .txt file called input.txt that simply contains the following:


abc+123

I want to load input.txt using VBA into a 1-dim array called MyArray. I want each character to be an element of the array. That is a is MyArray(1), c is MyArray(3) "+" is MyArray(4) etc.

I am not sure about whitespace. If it is easier, whitespace could be considered a character.

Any ideas?

Edit: ALternatively, each charecter could got to a cell...I just can't figure out how to set the dilmeter since there is not one...

If, you need a delimiter then you are probably using some kind of split function which is not what you are looking for. Try a different text function.
 
Where are you have problems? Opening and reading the file or dealing with the string after it is read?

VB help has lots of information, look up "read from file" for help on opening and reading.

You can read in file contents 1 character at time or line at a time. Can you tell us more about where you are with VB? How familiar are you with programming in general?
 
Okay. I Got it.

I am trying to create a Tool in VBA that reads the nonsense that you get when you save a file from Mathematica as .txt and rewrites it into a format that excel can more readily use.

I am having difficulty "decoding" Mathematica's syntax. For example:

This is what I have as an expression:
out.jpg





and this is how Mathematica outputs it to a .txt file
Code:
\!\(G\/K - J\/K - \(H\ \((\(-B\)\ G\ 
        M + D\ F\ G\ M + B\ J\ M - D\ F\ J\ M - A\ B\ F\ N1 + A\ B\ D\ F\ N1 \
- B\^2\ G\ N1 + B\ C\ G\ N1 + B\ F\ G\ N1 - C\ D\ F\ G\ N1 + B\^2\ J\ N1 - B\ \
C\ J\ N1 - B\ F\ J\ N1 + C\ D\ F\ J\ N1 - B\ F\ M\ N2 + B\ D\ F\ M\ N2)\)\)\/\
\(K\ \((B\ D\ F\ M - D\ F\^2\ M - B\ H\ M + D\ F\ H\ M + B\ L\ M - D\ F\ L\ M \
+ B\^2\ D\ F\ N1 - B\ C\ D\ F\ N1 - B\ F\^2\ N1 + C\ D\ F\^2\ N1 - B\^2\ H\ \
N1 + B\ C\ H\ N1 + B\ F\ H\ N1 - C\ D\ F\ H\ N1 + B\^2\ L\ N1 - B\ C\ L\ N1 - \
B\ F\ L\ N1 + C\ D\ F\ L\ N1)\)\)\)
 
Saladsamurai said:
Okay. I Got it.

I am trying to create a Tool in VBA that reads the nonsense that you get when you save a file from Mathematica as .txt and rewrites it into a format that excel can more readily use.

I am having difficulty "decoding" Mathematica's syntax. For example:

This is what I have as an expression:
out.jpg





and this is how Mathematica outputs it to a .txt file
Code:
\!\(G\/K - J\/K - \(H\ \((\(-B\)\ G\ 
        M + D\ F\ G\ M + B\ J\ M - D\ F\ J\ M - A\ B\ F\ N1 + A\ B\ D\ F\ N1 \
- B\^2\ G\ N1 + B\ C\ G\ N1 + B\ F\ G\ N1 - C\ D\ F\ G\ N1 + B\^2\ J\ N1 - B\ \
C\ J\ N1 - B\ F\ J\ N1 + C\ D\ F\ J\ N1 - B\ F\ M\ N2 + B\ D\ F\ M\ N2)\)\)\/\
\(K\ \((B\ D\ F\ M - D\ F\^2\ M - B\ H\ M + D\ F\ H\ M + B\ L\ M - D\ F\ L\ M \
+ B\^2\ D\ F\ N1 - B\ C\ D\ F\ N1 - B\ F\^2\ N1 + C\ D\ F\^2\ N1 - B\^2\ H\ \
N1 + B\ C\ H\ N1 + B\ F\ H\ N1 - C\ D\ F\ H\ N1 + B\^2\ L\ N1 - B\ C\ L\ N1 - \
B\ F\ L\ N1 + C\ D\ F\ L\ N1)\)\)\)

It looks tricky. I'm wondering if this is the best approach. I'm not sure what capabilities mathematical has but maple I belie writes you choose to write an expression as latex or mathml. I'm wondering if the processing of the file should be done in mathamatica instead of externally though visual basic. However, I guess this wouldn't work so well, if you were trying to read other peoples files and you didn't have access to Mathamatica.
 
It's not tooo bad... It uses a "\" before any kind of operation except for + and - as well as a space-"\" for a line continuation.

It's just the seemingly arbitrary use of parenthesis.
 
Okay New Question

Here is the code I am using:

This writes each character of the text file into a cell in the first column of my worksheet.
Code:
Option ExplicitSub OpenFile()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim Text As String
Dim NumberCharacters As Integer
Dim MyArray() As String    
'***************************************************************
   
    'Clear ActiveSheet

    Sheets("Sheet1").Range("1:65536").ClearContents
    Open "Z:\Casey B\Short.txt" For Input As #1
    
'   Fills Column with Individual Characters Read From .txt File
    
    i = 1
        Do While Not EOF(1)
            Text = Input(1, #1)
            Cells(i, 1) = Text
            i = i + 1
    
        Loop

    Close #1
    
    
    
    NumberCharacters = i - 1

Now here is the part that I do not understand. If I have excel output anyone of these cells to a Message Box, they show up just fine.

But if store each element from the cells in an Array like this:

Code:
''   Fill a 1-Dim Array~Each Character is an element
'
'    ReDim MyArray(NumberCharacters)
'    MsgBox "MyArray contains " & _
'           UBound(MyArray) & " elements."
'
'    For j = 1 To NumberCharacters
'        MyArray(j) = Cells(j, 1)
'        j = j + 1
'
'    Next j

And I have excel output the elements to a Message Box, it will not output an exclamation point "!" or a "(" or ")" and one of the slashes.

For example: if cell 1,1 is a "!" and I store it in the first element of MyArray --> MyArray(1)=Cell(1,1)

And then I send MyArray(1) to a message box...nothing shows up...just blank space.

i want to handle everything using arrays, but can't seem to get around this??
 
Saladsamurai said:
And I have excel output the elements to a Message Box, it will not output an exclamation point "!" or a "(" or ")" and one of the slashes.

For example: if cell 1,1 is a "!" and I store it in the first element of MyArray --> MyArray(1)=Cell(1,1)

And then I send MyArray(1) to a message box...nothing shows up...just blank space.

i want to handle everything using arrays, but can't seem to get around this??

Can you show us the code where you send the array to the message box because as far as I know the input to the message box function is a string and not an array of strings.
 
You should not increment j inside of the For loop. j is your loop counter, changing it in the loop will decrease the number of times your loop executes.
 
  • #10
Integral said:
You should not increment j inside of the For loop. j is your loop counter, changing it in the loop will decrease the number of times your loop executes.

Oh yeah...VBA uses "Next j" ...I keep forgetting. Thanks :smile: Here is the whole code (w/out the j+1 correction)



EDIT: Nevermind! Integral...somehow that j+1 removal has fixed my problem. It's too early :yawn: for me to see why... but I think I get it. I think it was only storing every-other character from the Cells...which just happened to be different characters, leading me to believe that it was something unique about those characters.

Thank you guys! :smile:
 
  • #11
Saladsamurai said:
Oh yeah...VBA uses "Next j" ...I keep forgetting. Thanks :smile: Here is the whole code (w/out the j+1 correction)



EDIT: Nevermind! Integral...somehow that j+1 removal has fixed my problem. It's too early :yawn: for me to see why... but I think I get it. I think it was only storing every-other character from the Cells...which just happened to be different characters, leading me to believe that it was something unique about those characters.

Thank you guys! :smile:

Yep, the way you had it only the odd numbered characters would have been written to a cell. I'm glad that fixed your problem. :approve:
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
55K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
7
Views
3K
  • · Replies 52 ·
2
Replies
52
Views
13K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 1 ·
Replies
1
Views
13K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
11K