Decimal formatting for output to text file

jcrook

New Member
Joined
Jun 12, 2020
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
Good Afternoon,
I am taking input from excel data and manipulating the values to generate CNC program code for metal cutting CNC machines. I'm sure there are many things wrong with this macro. But, I need to output a numbers to a ".txt" file that has at least a decimal point. The values that are currently "doubles" are output with a decimal. The values that are entered into the spreadsheet as decimals, ex: 1.0 or 3.0, endup as integers upon output to the text file. This is a no-no for a cnc control because a coordinate like X4 is actually read as X0.0004 because the industry standard is 4 places to the right of the decimal point if none is input. Where X1. = X1., again X3 = X0.0003. This is tenths of thousands in inch.
I've a screen shot of the spreadsheet, the text output, and some screen shots of the macro code where I'm printing to the text file. I'm not using "Write" because it includes the "" marks, these are not understood by the control.
I need to output everything as a decimal number(double). I would like all values to have at least 1 zero to the right of the dec pt, X1 = X1.0.
Thanks in Advance,
Jay.
 

Attachments

  • Rect Pckt Screen Shot.PNG
    Rect Pckt Screen Shot.PNG
    83.6 KB · Views: 33
  • cnc text file.PNG
    cnc text file.PNG
    29.7 KB · Views: 34
  • rect macro code1.PNG
    rect macro code1.PNG
    44.3 KB · Views: 29
  • rect macro code2.PNG
    rect macro code2.PNG
    49.2 KB · Views: 25

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I can't see exactly where you're outputting one of those values that cause an error. But I assume it reads something like:

Code:
Print #1 range("A1").value

replace that with:

Code:
Print #1 Format(Range("A1"), "0.0000")

if A1 has the value 1 then the latter code will send 1.0000 to the output.

BTW, A couple of thoughts on your code (hope you don't mind):

1. I don't believe you need to put .value after the range as VBA assumes that that is what is meant.
2. I put all my DIM statements at the start of the code - I think It makes it clear what has been declared, that said I can see some clarity in the way you've written yours so just a thought.
3. Do you have 'Option Explicit' at the very top of you're code module? If not, then I would add it as it forces all variables to be declared. This then stops you inadvertently mistyping a variable and getting unexpected results. I don't actually know why it isn't the default setting as it can save countless hours of needless debugging.. (in the VBA editor go to Tools - Options and make sure there is a check mark against the 'Require Variable Declaration').

HTH
 
Last edited:
Upvote 0
Hi Peter,
Thank you for the feedback.
Yes I have Option Explicit at the beginning.
I appreciate your comments on the layout of my code.
I find that when I need a new variable I initiate it where I need it. Probably not the best strategy.
I did find info on the "Print #1 Format(Range("A1"), "0.0000")" code you mentioned.
My problem is as below. I am manipulating the values prior to output, not just taking a cell value and sending it to a text file.
.
.
.
'Output last X,Y Rough Passes at Current Z-Level
xPosCutLoc = pcktCtrLocX + (pcktLenX / 2) - stkFshSide - (cutterDia / 2)
yPosCutLoc = pcktCtrLocY + (pcktLenY / 2) - stkFshSide - (cutterDia / 2)
xNegCutLoc = pcktCtrLocX - (pcktLenX / 2) + stkFshSide + (cutterDia / 2)
yNegCutLoc = pcktCtrLocY - (pcktLenY / 2) + stkFshSide + (cutterDia / 2)
'Open fullFilePath For Append As #1
Print #1, "X" & xPosCutLoc
Print #1, "Y" & yPosCutLoc
Print #1, "X" & xNegCutLoc
Print #1, "Y" & yNegCutLoc
Print #1, "X" & xPosCutLoc
Print #1, "Y" & pcktCtrLocY <--> Print #1, "Y" & pcktCtrLocY = Format(pcktCtrLocY, "0.0000")
If the above is the only way to format the number to get a decimal point some of the lines will be extremely long.
I'll get used to using the continuation code.

Is there a way to upload the macro in a text file and the excel spreadsheet to this site?
This would make it easier.

Thanks,
Jay.
 
Upvote 0
This is from my iPhone so a bit short.

You can upload the spreadsheet by using xl2bb from the link in the forum (though I can't see it in this version). You can copy and paste your code between the rich text code that </> inserts. Alternatively I have seen people use Dropbox linked to share their spreadsheet.

Yes it might make the lines longer, but I think you're interpretation of the use of Format is wrong. I think your last line above should be:

Print #1, "Y" & Format(pcktCtrLocY, "0.0000")

It appears that many of your formulas are very similar so it may be possible to make this simpler by using a loop that steps through the data, does the calculation and stores it in a temp variable. Then output that variable wrapped in the Format function.

Even simply reordering you're code above will help:
VBA Code:
Dim ans as String

Open fullFilePath For Append As #1
'Output last X,Y Rough Passes at Current Z-Level

ans = pcktCtrLocX + (pcktLenX / 2) - stkFshSide - (cutterDia / 2)
Print #1 "X" & Format(ans, "0.0000")

ans = pcktCtrLocY + (pcktLenY / 2) - stkFshSide - (cutterDia / 2)
Print #1 "Y" & Format(ans, "0.0000")

'alternative structure that retains the xNegCutLoc variable:
xNegCutLoc = pcktCtrLocX - (pcktLenX / 2) + stkFshSide + (cutterDia / 2)
ans = "X" & Format(ans, "0.0000")
Print #1 ans

HTH
 
Upvote 0
Hi Peter,
Thanks for you're reply.
I don't have any emotional attachment to my method.
I'll give your suggestions a try and see what happens.

Thanks Again,
Jay.
 
Upvote 0
Hope it works the way you want. Let me know how you get on. If you can post your spreadsheet I'll have another look an implementing loops that might make it easier.
 
Upvote 0
Hi Peter,
The Format command worked great.
I will upload the docs this evening, I'm on my work computer and they don't allow installation of unapproved apps.
Here's the macro code though.

VBA Code:
Option Explicit

Sub Get_Rect_Machine_Values()

'Range("C8:C20").NumberFormat = "0.####"

  'Get Machining Values from Rgh Cir Pckt WrkSht, C8 thru C19
Dim cutterDia As Double
cutterDia = Range("C8").Value
Dim uprLeftCrnrX As Double
uprLeftCrnrX = Range("C9").Value
Dim uprLeftCrnrY As Double
uprLeftCrnrY = Range("C10").Value
Dim pcktLenX As Double
pcktLenX = Range("C11").Value
Dim pcktLenY As Double
pcktLenY = Range("C12").Value
Dim totDepthZ As Double
totDepthZ = Range("C13").Value
Dim crnrRad As Double
crnrRad = Range("C14").Value
Dim radCutWidth As Double
radCutWidth = Range("C15").Value
Dim stkFshSide As Double
stkFshSide = Range("C16").Value
Dim docZ As Double
docZ = Range("C17").Value
Dim stkFshZ As Double
stkFshZ = Range("C18").Value
Dim leadRadOnOff As Double
leadRadOnOff = Range("C19").Value
Dim leadInTan As Double
leadInTan = Range("C20").Value
Dim feedRateRgh As String
feedRateRgh = Range("C21").Value
Dim feedRateFsh As String
feedRateFsh = Range("C22").Value
Dim toolNumRgh As Integer
toolNumRgh = Range("C23").Value
Dim toolNumFsh As Integer
toolNumFsh = Range("C24").Value
Dim operNum As String
operNum = Range("C25").Value
Dim fileName As String
fileName = Range("C26").Value
Dim filePath As String
filePath = Range("C27").Value
Dim fullFilePath As String
Dim toolNum As Integer
toolNum = toolNumRgh
Dim rghRPM As Integer
rghRPM = Range("C28").Value
Dim fshRPM As Integer
fshRPM = Range("C29").Value
Dim spndlDir As String
spndlDir = Range("C30").Value


Dim pcktCtrLocX As Double
pcktCtrLocX = uprLeftCrnrX + (pcktLenX / 2)
Dim pcktCtrLocY As Double
pcktCtrLocY = uprLeftCrnrY + ((pcktLenY / 2) * -1)
  'Test Pocket Center Calculations
'Range("D11").Value = pcktCtrLocX
'Range("D12").Value = pcktCtrLocY


Dim answer As VbMsgBoxResult
fullFilePath = Dir(filePath & "\" & fileName & "_" & operNum & ".txt")

If fullFilePath = "" Then
   answer = MsgBox("File Does Not Exist. Create File?", vbYesNo, "File Verification")
   Select Case answer
     Case vbYes
       fullFilePath = filePath & "\" & fileName & "_" & operNum & ".txt"
       Open fullFilePath For Append As #1
       Close #1
     Case Else
       MsgBox "File Name Not Created"
   End Select
  
Else
   answer = MsgBox("Backup File or Delete old File", vbOKOnly, "File Exists")
End If

  'Test Retrieval of Machining Values
'Range("C40").Value = cutterDia
'Range("C41").Value = cirRad
'Range("C42").Value = fileName

Dim rghMatX As Double
rghMatX = (pcktLenX / 2) - (cutterDia / 2) - stkFshSide
Dim rghMatZ As Double
rghMatZ = (totDepthZ - stkFshZ) * -1

  'Test Calculations
'Range("H17").Value = rghMatRad
'Range("H18").Value = rghMatZ

  'Create & Output Prog Num, Header Note & code
  Dim progNum As String
  progNum = "O1001"
  Dim progHeader As String
  progHeader = "(" & fileName & "_" & operNum & "," & "OP." & operNum & "," & Now & ")"
  Dim startCode As String
  'startCode = "G90G54G0" & "X" & pcktCtrLocX & "Y" & pcktCtrLocY
  startCode = "G90G54G0"
  Dim zStart As String
  zStart = "G43Z0.1" & "H" & toolNum & "M8"

  'Open File for Output
  Open fullFilePath For Append As #1
  Print #1, progNum
  Print #1, progHeader
  Print #1, "M6" & "T" & toolNum
  Print #1, "S" & rghRPM & spndlDir
  Print #1, startCode & "X" & Format(pcktCtrLocX, "0.#####") & "Y" & Format(pcktCtrLocY, "0.#####")
  Print #1, zStart
  'Close #1
 
  'Set Limiting Check Values
Dim zDepth As Double
zDepth = 0
zDepth = zDepth + (docZ * -1)

'Set Counters
Dim radWidth As Double
radWidth = 0
radWidth = radWidth + radCutWidth
Dim radWidthX As Double
radWidthX = radCutWidth
Dim radWidthY As Double
radWidthY = ((pcktLenY / 2) - (cutterDia / 2) - stkFshSide) _
            / (((pcktLenX / 2) - (cutterDia / 2) - stkFshSide) / radCutWidth)
'Range("G18").Value = radWidthY
radWidthY = Format(radWidthY, "0.####")


Dim passNum As Integer
passNum = 1


  'Calc and Output X,Y,Z Rghing Passes
Do While zDepth > rghMatZ
   'Calc next Z-level
   Dim nextZCut As Double
   nextZCut = zDepth * passNum
   'Output Z movement to file
   'Open fullFilePath For Append As #1
   'G1 Z-(zDepth)
   Print #1, "G1" & "Z" & nextZCut & "F" & Format(feedRateFsh, "0.####")
   'Close #1
  
     Do While radWidth < rghMatX
        'Calc X,Y Cut Location Prior to Rectangular Cut
          Dim xPosCutLoc As Double
          xPosCutLoc = pcktCtrLocX + (radWidthX * passNum)
          Dim yPosCutLoc As Double
          yPosCutLoc = pcktCtrLocY + (radWidthY * passNum)
          Dim xNegCutLoc As Double
          xNegCutLoc = pcktCtrLocX - (radWidthX * passNum)
          Dim yNegCutLoc As Double
          yNegCutLoc = pcktCtrLocY - (radWidthY * passNum)
          'Output X,Y Linear interpolation
          'Open fullFilePath For Append As #1
          Print #1, "X" & Format(xPosCutLoc, "0.####") & "Y" & Format(yPosCutLoc, "0.####") & "F" & Format(feedRateRgh, "0.####")
          'Print #1, "Y" & yPosCutLoc
          Print #1, "X" & Format(xNegCutLoc, "0.####")
          Print #1, "Y" & Format(yNegCutLoc, "0.####")
          Print #1, "X" & Format(xPosCutLoc, "0.####")
          Print #1, "Y" & Format(pcktCtrLocY, "0.####")
          'Close #1
          'G1 X(xCutLoc) Y(yCutLoc) F(feedRateRgh)
          passNum = passNum + 1
          radWidth = radCutWidth * passNum
    Loop
  'Output last X,Y Rough Passes at Current Z-Level
  xPosCutLoc = pcktCtrLocX + (pcktLenX / 2) - stkFshSide - (cutterDia / 2)
  yPosCutLoc = pcktCtrLocY + (pcktLenY / 2) - stkFshSide - (cutterDia / 2)
  xNegCutLoc = pcktCtrLocX - (pcktLenX / 2) + stkFshSide + (cutterDia / 2)
  yNegCutLoc = pcktCtrLocY - (pcktLenY / 2) + stkFshSide + (cutterDia / 2)
  'Open fullFilePath For Append As #1
  Print #1, "X" & Format(xPosCutLoc, "0.####") & "Y" & Format(yPosCutLoc, "0.####")
  'Print #1, "Y" & yPosCutLoc
  Print #1, "X" & Format(xNegCutLoc, "0.####")
  Print #1, "Y" & Format(yNegCutLoc, "0.####")
  Print #1, "X" & Format(xPosCutLoc, "0.####")
  Print #1, "Y" & Format(pcktCtrLocY, "0.####")
  Print #1, "Y" & Format(yPosCutLoc, "0.####") & "F" & Format((feedRateRgh * 1.25), "0.####")
 
  'Reset Counters
  passNum = 1
  radWidth = radCutWidth
 
  'Output Retract and Reposition to Rect Pckt Center
  Dim escapeZ As String
  escapeZ = "G0Z0.1"
  Print #1, escapeZ
  'X(ctrLocX) Y(ctrLocY)
  Dim pcktStart As String
  pcktStart = "X" & Format(pcktCtrLocX, "0.####") & "Y" & Format(pcktCtrLocY, "0.####")
  Print #1, pcktStart
    'G1 Z-(zDepth +0.02)
  Dim cutLevelZ As String
  cutLevelZ = "G1" & "Z" & Format((zDepth + 0.02), "0.####")
  Print #1, cutLevelZ & "F" & Format(feedRateRgh, "0.####")
 

zDepth = zDepth + (docZ * -1)

Loop

'Make Last X,Y Rgh Passes at Last Rgh Z-Level
Dim zCode As String
zCode = "G1" & "Z" & rghMatZ & "F" & Format(feedRateFsh, "0.####")
Print #1, zCode

     Do While radWidth < rghMatX
        'Calc X,Y Cut Location Prior to Circle Cut
          'Dim xPosCutLoc As Double
          xPosCutLoc = pcktCtrLocX + (radWidthX * passNum)
          'Dim yPosCutLoc As Double
          yPosCutLoc = pcktCtrLocY + (radWidthY * passNum)
          'Dim xNegCutLoc As Double
          xNegCutLoc = pcktCtrLocX - (radWidthX * passNum)
          'Dim yNegCutLoc As Double
          yNegCutLoc = pcktCtrLocY - (radWidthY * passNum)
          'Output X,Y Linear interpolation
          'Open fullFilePath For Append As #1
          Print #1, "X" & Format(xPosCutLoc, "0.####") & "Y" & Format(yPosCutLoc, "0.####") & "F" & Format(feedRateRgh, "0.####")
          'Print #1, "Y" & yPosCutLoc
          Print #1, "X" & Format(xNegCutLoc, "0.####")
          Print #1, "Y" & Format(yNegCutLoc, "0.####")
          Print #1, "X" & Format(xPosCutLoc, "0.####")
          Print #1, "Y" & Format(pcktCtrLocY, "0.####")
          'Close #1
          'G1 X(xCutLoc) Y(yCutLoc) F(feedRateRgh)
          passNum = passNum + 1
          radWidth = radCutWidth * passNum
    Loop
  'Output last X,Y Rough Passes at Current Z-Level
  xPosCutLoc = pcktCtrLocX + (pcktLenX / 2) - stkFshSide - (cutterDia / 2)
  yPosCutLoc = pcktCtrLocY + (pcktLenY / 2) - stkFshSide - (cutterDia / 2)
  xNegCutLoc = pcktCtrLocX - (pcktLenX / 2) + stkFshSide + (cutterDia / 2)
  yNegCutLoc = pcktCtrLocY - (pcktLenY / 2) + stkFshSide + (cutterDia / 2)
  'Open fullFilePath For Append As #1
  Print #1, "X" & Format(xPosCutLoc, "0.####") & "Y" & Format(yPosCutLoc, "0.####") & "F" & Format(feedRateRgh, "0.####")
  'Print #1, "Y" & yPosCutLoc
  Print #1, "X" & Format(xNegCutLoc, "0.####")
  Print #1, "Y" & Format(yNegCutLoc, "0.####")
  Print #1, "X" & Format(xPosCutLoc, "0.####")
  Print #1, "Y" & Format(pcktCtrLocY, "0.####")
  Print #1, "Y" & Format(yPosCutLoc, "0.####") & "F" & Format((feedRateRgh * 1.25), "0.####")
 
  'Reset Counters
  passNum = 1
  radWidth = radCutWidth
 
  'Output Retract and Reposition to Circle Pckt Center
  'Dim escapeZ As String
  escapeZ = "G0Z0.1"
  Print #1, escapeZ


Close #1
 
  'Check for different Finish Tool Number
  If toolNumFsh <> toolNumRgh Then
     toolNum = toolNumFsh
     Dim retractZ As String
     retractZ = "G91G28G0Z0M9"
     Dim homeXY As String
     homeXY = "G28G0X0Y0M5"
     Dim toolChg As String
     toolChg = "M6" & "T" & toolNum
     'hCode = "G43Z0.1" & "H" & toolNum & "M8"
  'Open File for Output
     Open fullFilePath For Append As #1
     Print #1, retractZ
     Print #1, homeXY
     Print #1, toolChg
     Print #1, "S" & fshRPM & spndlDir
     Print #1, startCode & "X" & Format(pcktCtrLocX, "0.#####") & "Y" & Format(pcktCtrLocY, "0.#####")
     'Print #1, hCode
     Print #1, "G43Z0.1" & "H" & toolNum & "M8"
     Close #1
  End If

  'Create and Output Finish Passes
     Open fullFilePath For Append As #1
    If toolNumFsh = toolNumRgh Then
     Print #1, "X" & Format(pcktCtrLocX, "0.#####") & "Y" & Format(pcktCtrLocY, "0.#####")
    End If
     Print #1, "G1" & "Z" & (rghMatZ + 0.1) & "F" & Format(feedRateRgh, "0.####")
     Print #1, "Z" & (totDepthZ * -1) & "F" & Format(feedRateFsh, "0.####")
     radWidth = 0
     radWidth = radWidth + radCutWidth
     passNum = 1
     Do While radWidth < rghMatX
        'Calc X,Y Cut Location Prior to Circle Cut
          'Dim xPosCutLoc As Double
          xPosCutLoc = pcktCtrLocX + (radWidthX * passNum)
          'Dim yPosCutLoc As Double
          yPosCutLoc = pcktCtrLocY + (radWidthY * passNum)
          'Dim xNegCutLoc As Double
          xNegCutLoc = pcktCtrLocX - (radWidthX * passNum)
          'Dim yNegCutLoc As Double
          yNegCutLoc = pcktCtrLocY - (radWidthY * passNum)
          'Output X,Y Linear interpolation
          'Open fullFilePath For Append As #1
          Print #1, "X" & Format(xPosCutLoc, "0.####") & "Y" & Format(yPosCutLoc, "0.####") & "F" & Format(feedRateRgh, "0.####")
          'Print #1, "Y" & yPosCutLoc
          Print #1, "X" & Format(xNegCutLoc, "0.####")
          Print #1, "Y" & Format(yNegCutLoc, "0.####")
          Print #1, "X" & Format(xPosCutLoc, "0.####")
          Print #1, "Y" & Format(pcktCtrLocY, "0.####")
          'Close #1
          'G1 X(xCutLoc) Y(yCutLoc) F(feedRateRgh)
          passNum = passNum + 1
          radWidth = radCutWidth * passNum
    Loop
  'Output last X,Y Rough Passes as Current Z-Level
  xPosCutLoc = pcktCtrLocX + (pcktLenX / 2) - stkFshSide - (cutterDia / 2)
  yPosCutLoc = pcktCtrLocY + (pcktLenY / 2) - stkFshSide - (cutterDia / 2)
  xNegCutLoc = pcktCtrLocX - (pcktLenX / 2) + stkFshSide + (cutterDia / 2)
  yNegCutLoc = pcktCtrLocY - (pcktLenY / 2) + stkFshSide + (cutterDia / 2)
  'Open fullFilePath For Append As #1
  Print #1, "X" & Format(xPosCutLoc, "0.####") & "Y" & Format(yPosCutLoc, "0.####") & "F" & Format(feedRateRgh, "0.####")
  'Print #1, "Y" & yPosCutLoc
  Print #1, "X" & Format(xNegCutLoc, "0.####")
  Print #1, "Y" & Format(yNegCutLoc, "0.####")
  Print #1, "X" & Format(xPosCutLoc, "0.####")
  Print #1, "Y" & Format(pcktCtrLocY, "0.####")
  Print #1, "Y" & Format(yPosCutLoc, "0.####") & "F" & Format((feedRateRgh * 1.25), "0.####")

  'Reset Counters
  passNum = 1
  radWidth = radCutWidth
 
  'Output Retract and Reposition to Circle Pckt Center
  'Dim escapeZ As String
  escapeZ = "G0Z0.1"
  Print #1, escapeZ

  'Create and Output Final Profile Pass
  'Dim retractZ As String
  retractZ = "G91G28G0Z0M9"
  'Dim homeXY As String
  homeXY = "G28G0X0Y0M5"
  Dim startProfX As Double
  Dim startProfY As Double
  startProfX = pcktCtrLocX + leadRadOnOff
  startProfY = pcktCtrLocY + (pcktLenY / 2) - (cutterDia / 2) - leadRadOnOff - leadInTan
  Print #1, "G0" & "X" & Format(startProfX, "0.####") & "Y" & Format(startProfY, "0.####")
  Print #1, "G1" & "Z" & (rghMatZ + 0.1) & "F" & Format(feedRateRgh, "0.####")
  Print #1, "Z" & (totDepthZ * -1) & "F" & Format(feedRateFsh, "0.####")
  Print #1, "G41" & "X" & Format(startProfX, "0.####") & "Y" & Format((pcktCtrLocY + (pcktLenY / 2) - (cutterDia / 2) - leadRadOnOff), "0.####") & "D" & toolNum; "F" & Format(feedRateFsh, "0.####")
  Print #1, "G3" & "X" & Format(pcktCtrLocX, "0.####") & "Y" & Format((pcktCtrLocY + (pcktLenY / 2) - (cutterDia / 2)), "0.####") & "I-" & Format(leadRadOnOff, "0.####")
  Print #1, "G1" & "X" & Format((pcktCtrLocX - (pcktLenX / 2) + (cutterDia / 2)), "0.####")
  Print #1, "Y" & Format((pcktCtrLocY - (pcktLenY / 2) + (cutterDia / 2)), "0.####")
  Print #1, "X" & Format((pcktCtrLocX + (pcktLenX / 2) - (cutterDia / 2)), "0.####")
  Print #1, "Y" & Format((pcktCtrLocY + (pcktLenY / 2) - (cutterDia / 2)), "0.####")
  Print #1, "X" & Format(pcktCtrLocX, "0.####")
  Print #1, "G3" & "X" & Format((pcktCtrLocX - leadRadOnOff), "0.####") & "Y" & Format((pcktCtrLocY + (pcktLenY / 2) - (cutterDia / 2) - leadRadOnOff), "0.####") & "F" & Format(feedRateRgh, "0.####")
  Print #1, "G1G40" & "X" & Format((pcktCtrLocX - leadRadOnOff), "0.####") & "Y" & Format(startProfY, "0.####")
  Print #1, escapeZ & "M9"
  Print #1, retractZ
  Print #1, homeXY
  Print #1, "M30"
  Print #1, "%"
 
Close #1

End Sub

Thanks Again,
Jay.
 
Upvote 0
Hi Peter,
I forgot to mention that my output, per line, is NOT a fixed length of "words"/"chars".
The length of each "sentence" will vary.
I'm sure a loop can be created but it might not be worth it.

Thanks,
Jay.
 
Upvote 0
I think you're probably right about not adding loops, but its worth thinking about it at the design stage because something like the code below might make it easier:

VBA Code:
Dim rRange as Cell

For Each rRange in Range("C8:C30")
...
Next rRange

Another alternative would be to define names for each of your cells. Then this line of code feedRateRgh = Range("C21").Value could be replaced by Range("feedRateRgh") (you don't need the .value). This means that if you move your data on the spreadsheet the named ranges change automatically and you're not left trying to rewrite the code.

I also noted that you duplicate the definition of certain variables. By doing so you run the risk should you ever need to change the code you'll miss a line and then get some rather difficult to debug errors.

HTH

Data TypeCodeVariableAssigned Value
StringescapeZ = "G0Z0.1"escapeZ"G0Z0.1"
StringescapeZ = "G0Z0.1"escapeZ"G0Z0.1"
StringescapeZ = "G0Z0.1"escapeZ"G0Z0.1"
StringhomeXY = "G28G0X0Y0M5"homeXY"G28G0X0Y0M5"
StringhomeXY = "G28G0X0Y0M5"homeXY"G28G0X0Y0M5"
StringretractZ = "G91G28G0Z0M9"retractZ"G91G28G0Z0M9"
StringretractZ = "G91G28G0Z0M9"retractZ"G91G28G0Z0M9"
DoublexNegCutLoc = pcktCtrLocX - (radWidthX * passNum)xNegCutLocpcktCtrLocX - (radWidthX * passNum)
DoublexNegCutLoc = pcktCtrLocX - (radWidthX * passNum)xNegCutLocpcktCtrLocX - (radWidthX * passNum)
DoublexNegCutLoc = pcktCtrLocX - (radWidthX * passNum)xNegCutLocpcktCtrLocX - (radWidthX * passNum)
DoublexPosCutLoc = pcktCtrLocX + (radWidthX * passNum)xPosCutLocpcktCtrLocX + (radWidthX * passNum)
DoublexPosCutLoc = pcktCtrLocX + (radWidthX * passNum)xPosCutLocpcktCtrLocX + (radWidthX * passNum)
DoublexPosCutLoc = pcktCtrLocX + (radWidthX * passNum)xPosCutLocpcktCtrLocX + (radWidthX * passNum)
DoubleyNegCutLoc = pcktCtrLocY - (radWidthY * passNum)yNegCutLocpcktCtrLocY - (radWidthY * passNum)
DoubleyNegCutLoc = pcktCtrLocY - (radWidthY * passNum)yNegCutLocpcktCtrLocY - (radWidthY * passNum)
DoubleyNegCutLoc = pcktCtrLocY - (radWidthY * passNum)yNegCutLocpcktCtrLocY - (radWidthY * passNum)
DoubleyPosCutLoc = pcktCtrLocY + (radWidthY * passNum)yPosCutLocpcktCtrLocY + (radWidthY * passNum)
DoubleyPosCutLoc = pcktCtrLocY + (radWidthY * passNum)yPosCutLocpcktCtrLocY + (radWidthY * passNum)
DoubleyPosCutLoc = pcktCtrLocY + (radWidthY * passNum)yPosCutLocpcktCtrLocY + (radWidthY * passNum)
 
Upvote 0
Hi Peter,
I liked naming the cell with the variable name.
I was able to rearrange some cells and it worked like a charm.

Thanks,
Jay.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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