seperate G-Code

Dr.Evil925

New Member
Joined
Feb 24, 2011
Messages
22
I need to seperate out a G-Code file into multiple cells. Any sugestions?
This is my start file.
N3G54G90G0X6.2Y.185S10000M3

<colgroup><col width="64"></colgroup><tbody>
</tbody>

<tbody>
</tbody>
I want my result to be
N3
G54
G90
G0
X6.2
Y.185
S10000
M3

<tbody>
</tbody>
 
You didn't give us an indication of the layout you wanted from that file, so I guessed (if I guessed wrong, the code can be modified). Here is a macro that asks you to select the file, processes that file totally in memory and then outputs the result to the active worksheet (which must be empty... the code checks). The layout I chose is Column A contains the N numbers, the remaining columns contain the numbers (only) from the codes that follow that N number. There is a G number before the first N number... I output as the first line with Column A left blank because there was no N number associated with it. All other lines in the file are ignored.
Rich (BB code):
Sub ProcessGCodeFile()
  Dim X As Long, Z As Long, FileNum As Long, Index As Long
  Dim TotalFile As String, PathFile As String
  Dim NLines() As String, Gspaced() As String, LinesOut() As String
  Const MaxPossibleGCodesPerLine As Long = 20
  If ActiveSheet.UsedRange.Cells.Count = 1 And Len(ActiveSheet.UsedRange.Cells(1).Value) = 0 Then
    MsgBox "The active worksheet must be totally empty..." & vbLf & _
           "I see data on the currently active sheet", vbCritical
    Exit Sub
  End If
  With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    .Show
    On Error GoTo NoFileSelected
    PathFile = .SelectedItems(1)
  End With
  FileNum = FreeFile
  Open PathFile For Binary As #FileNum
    TotalFile = Space(LOF(FileNum))
    Get #FileNum, , TotalFile
  Close #FileNum
  NLines = Split(Replace(TotalFile, "G00", "N G00", , 1), vbNewLine & "N")
  ReDim LinesOut(1 To UBound(NLines), 1 To MaxPossibleGCodesPerLine)
  For X = 1 To UBound(NLines)
    Gspaced = Split(Split(NLines(X), vbNewLine)(0))
    Index = 0
    For Z = 0 To UBound(Gspaced)
      Index = Index + 1
      LinesOut(X, Index) = Mid(Gspaced(Z), 2 + (Z = 0))
    Next
  Next
  Range("A1").Resize(UBound(LinesOut), UBound(LinesOut, 2)) = LinesOut
NoFileSelected:
End Sub
Rick, I think that check needs a check. :eek:
Thanks Peter, you are correct. That greater than sign should be an equal sign. Fixed and shown in red above
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Thanks Peter, you are correct. That greater than sign should be an equal sign. Fixed and shown in red above
Surely that now says if the sheet is empty then display a message that says "I see data" and if the sheet actually contains data it proceeds to run the rest of the macro? :confused:
 
Upvote 0
Surely that now says if the sheet is empty then display a message that says "I see data" and if the sheet actually contains data it proceeds to run the rest of the macro? :confused:
It surely does. Maybe I should just remove the test and let the OP overwrite any data on the sheet? Only kidding! Here is revised code that I believe now works correctly. Thanks for keeping me honest Peter.
Code:
Sub ProcessGCodeFile()
  Dim X As Long, Z As Long, FileNum As Long, Index As Long
  Dim TotalFile As String, PathFile As String
  Dim NLines() As String, Gspaced() As String, LinesOut() As String
  Const MaxPossibleGCodesPerLine As Long = 20
  If WorksheetFunction.CountA(ActiveSheet.UsedRange) Then
    MsgBox "The active worksheet must be totally empty..." & vbLf & _
           "I see data on the currently active sheet", vbCritical
    Exit Sub
  End If
  With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    .Show
    On Error GoTo NoFileSelected
    PathFile = .SelectedItems(1)
  End With
  FileNum = FreeFile
  Open PathFile For Binary As #FileNum
    TotalFile = Space(LOF(FileNum))
    Get #FileNum, , TotalFile
  Close #FileNum
  NLines = Split(Replace(TotalFile, "G00", "N G00", , 1), vbNewLine & "N")
  ReDim LinesOut(1 To UBound(NLines), 1 To MaxPossibleGCodesPerLine)
  For X = 1 To UBound(NLines)
    Gspaced = Split(Split(NLines(X), vbNewLine)(0))
    Index = 0
    For Z = 0 To UBound(Gspaced)
      Index = Index + 1
      LinesOut(X, Index) = Mid(Gspaced(Z), 2 + (Z = 0))
    Next
  Next
  Range("A1").Resize(UBound(LinesOut), UBound(LinesOut, 2)) = LinesOut
NoFileSelected:
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,254
Messages
6,123,893
Members
449,132
Latest member
Rosie14

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