Rob Webster
New Member
Hi all,
I have a constant stored in my workbook as a defined name, the constant isn't in a cell.
i'm trying to use the constant as the start number, from which to count the number of times that a loop is performed. so that I have a record of how many times the loop has passed in total in the file (regardless of how many times it has been opened saved closed etc). the ending number (when the code has finished) is then used/saved as the constants new value for next time the code runs.
the object is to count the total number of times that the loop has run regardless of how many times the sub procedure that the code is in has been run and to store it in the file, but not in a cell so that it doesn't get overwritten by mistake.
the problem is that on first run, the value in the name is zero. visual basic seems to read this as "=Empty" as a string rather than zero. so cant add the numbers to it. if Istart the counter at =1, again visual basic reads it as a string. I've tried to use the worksheet function.substitute, to remove the "=" and then cLng to convert the remainder of the string to its value but with no luck. can any one offer any suggestions?
sorry new to forum and trying to get to grips with how to tag code
I have a constant stored in my workbook as a defined name, the constant isn't in a cell.
i'm trying to use the constant as the start number, from which to count the number of times that a loop is performed. so that I have a record of how many times the loop has passed in total in the file (regardless of how many times it has been opened saved closed etc). the ending number (when the code has finished) is then used/saved as the constants new value for next time the code runs.
the object is to count the total number of times that the loop has run regardless of how many times the sub procedure that the code is in has been run and to store it in the file, but not in a cell so that it doesn't get overwritten by mistake.
the problem is that on first run, the value in the name is zero. visual basic seems to read this as "=Empty" as a string rather than zero. so cant add the numbers to it. if Istart the counter at =1, again visual basic reads it as a string. I've tried to use the worksheet function.substitute, to remove the "=" and then cLng to convert the remainder of the string to its value but with no luck. can any one offer any suggestions?
sorry new to forum and trying to get to grips with how to tag code
Code:
Dim IDstr As String
Dim IDtxtno As String
Dim IDCount As Long
Dim IDLen As Long
IDstr = ActiveWorkbook.Names("VAL_ID").Value
IDLen = Len(Names("VAL_ID").Value)
If IDstr = "=0" Then
IDCount = 0
Else
IDtxtno = WorksheetFunction.Right(IDstr, (IDLen - 1))
IDCount = WorksheetFunction.Value(IDtxtno)
End If