Need VBA code snippet help...

by astaga
5 replies
I have a datafile with 16 char in 16 lines that I need to get into excel (16 column by 16 rows).

The sample code I have doesn't do what I want (I'm sure it is not correct code):
Sub Sample()
Dim MyData As String
Dim lineData() As String, strData() As String, myFile As String
Dim i As Long, rng As Range
myFile = Application.GetOpenFilename("RawData1 (*.txt), *.txt")

Open myFile For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
' Split into wholes line
lineData() = Split(MyData, vbNewLine)
Set rng = Range("A1")
' For each line
For i = 0 To UBound(lineData)
' Split the line
strData = Split(lineData(i), "|")
' Write to the sheet
rng.Offset(i, 0).Resize(1, UBound(strData) + 1) = strData

End Sub

Appreciate any and all help please.
The datafile contents is like this (note . means a blank cell):
*notice #char (including the . adds up to 16 per line)

Many thanks,
#code #snippet #vba
Avatar of Unregistered
  • Profile picture of the author codexmonkey
    Hi Tim. I don't know VBA, but this looks like something I might just use a good text editor for instead of writing a script. Are you just trying to convert to csv so you can open in excel? If so, could you just find/replace all "." with ","?
    {{ DiscussionBoard.errors[11268971].message }}
  • Profile picture of the author tryinhere
    do you have a before spreadsheet and an after spreadsheet? so I can see what you want.
    | > Choosing to go off the grid for a while to focus on family, work and life in general. Have a great 2020 < |
    {{ DiscussionBoard.errors[11269295].message }}
  • Profile picture of the author Mike Anthony
    Also don't know VBA as early on I went with C# in that area (and Pyhon for most text manipulation). Question like this however are much better for Stackoverflow.

    {{ DiscussionBoard.errors[11269734].message }}
  • Profile picture of the author tryinhere
    Whipped this up real quick for you, see spread sheet attached you will just need to copy the data the orange section and select to run the macro, it is basic but without knowing exactly what you want this will do of sorts for you.

    Sub Sort16()
    ' Keyboard Shortcut: Ctrl+Shift+X
    Application.ScreenUpdating = False
    Do Until ActiveCell.Value = ""
    Range("B17").Value = 1
    Do Until Range("B17").Value = 17
    ActiveCell.Offset(0, Range("c17")).Value = ActiveCell.Offset(0, 2).Value
    Range("B17").Value = Range("B17").Value + 1
    ActiveCell.Offset(1, 0).Select
    Application.ScreenUpdating = False
    End Sub

    *** EDIT *** In the last line of the sub I want you to change the code from

    Application.ScreenUpdating = False


    Application.ScreenUpdating = True

    oversight / sorry
    | > Choosing to go off the grid for a while to focus on family, work and life in general. Have a great 2020 < |
    {{ DiscussionBoard.errors[11270349].message }}
  • Profile picture of the author astaga
    don't understand your reply?
    If I replace all "." (a period) with "," (a comma) that defines/separates where blanks are to go (as a cell entry), but I'll have problems in places that have a character/value in them, since there are no commas to define these being a cell entry.

    Thank you for your code.
    Yes, that does the job beautifully.

    {{ DiscussionBoard.errors[11271965].message }}
Avatar of Unregistered

Trending Topics