Progress bar in Status bar, a blank and a filled in char are not equal width - vba

I am playing around creating code for a progress bar that runs in the Excel Status Bar. I want to replace my old dated userform with the 2 rectangles (which worked but I would sooner a less obtrusive method now).
Problem: The width of the chars I am using to signify "Filled In" and "Not Filled in" are slightly different, when using 100 of them you can see the percentage at the end appears to shift right as the progress increases.
Here is some working sample code to show you exactly what I mean:
Sub TestNewProgBar()
Dim X As Long
For X = 1 To 100000
Call NewProgressBar("Testing", X, 100000)
Next
End Sub
Sub NewProgressBar(MyMessage As String, CurrentVal As Long, MaxVal As Long)
Dim FilledIn As Long, NotFilledIn As Long
If CurrentVal >= MaxVal Then
Application.StatusBar = MyMessage & ": Complete"
Else
FilledIn = Round((CurrentVal / MaxVal) * 100, 0)
NotFilledIn = (100 - FilledIn)
Application.StatusBar = MyMessage & ": " & Application.WorksheetFunction.Rept(ChrW(9608), FilledIn) & Application.WorksheetFunction.Rept(ChrW(9620), NotFilledIn) & "| " & FilledIn & "%"
End If
End Sub
Run TestNewProgBar and look at the status bar.
Is this going to be a simple case of choosing a different Unicode symbol or are there forces beyond my control at work here?

There's a Unicode block from U+25A0 to U+25FF called Geometric Shapes. There are some matching pairs of black/ white shapes in there that will successfully work for your progress bar implementation.
In the test code below, some pairs work and some do not!. Personally I like the last example (pairing U+25AE and U+25AD).
Option Explicit
Sub TestNewProgBar()
Dim lngCounter As Long
Dim lngMax As Long
Dim strFilledChar As String
Dim strNotFilledChar As String
'iterations
lngMax = 100000
'small squares - works
strFilledChar = ChrW(&H25AA)
strNotFilledChar = ChrW(&H25AB)
For lngCounter = 1 To lngMax
Call NewProgressBar("Small squares", lngCounter, lngMax, strFilledChar, strNotFilledChar)
Next
'large squares - doesn't work
strFilledChar = ChrW(&H25A0)
strNotFilledChar = ChrW(&H25A1)
For lngCounter = 1 To lngMax
Call NewProgressBar("Large squares", lngCounter, lngMax, strFilledChar, strNotFilledChar)
Next
'large squares 2 - doesn't work (but opposite effect)
strFilledChar = ChrW(&H25A3)
strNotFilledChar = ChrW(&H25A1)
For lngCounter = 1 To lngMax
Call NewProgressBar("Large squares 2", lngCounter, lngMax, strFilledChar, strNotFilledChar)
Next
'mixed vertical/ horizontal rectangles - works!
strFilledChar = ChrW(&H25AE)
strNotFilledChar = ChrW(&H25AD)
For lngCounter = 1 To lngMax
Call NewProgressBar("Mixed rectangles", lngCounter, lngMax, strFilledChar, strNotFilledChar)
Next
End Sub
Sub NewProgressBar(strMyMessage As String, lngCurrentVal As Long, lngMaxVal As Long, strFilledChar As String, strNotFilledChar As String)
Dim lngFilledIn As Long
Dim lngNotFilledIn As Long
Dim strStatus As String
If lngCurrentVal >= lngMaxVal Then
Application.StatusBar = strMyMessage & ": Complete"
Else
lngFilledIn = Round((lngCurrentVal / lngMaxVal) * 100, 0)
lngNotFilledIn = (100 - lngFilledIn)
strStatus = strMyMessage & ": " & _
String(lngFilledIn, strFilledChar) & _
String(lngNotFilledIn, strNotFilledChar) & _
"| " & lngFilledIn & "%"
Application.StatusBar = strStatus
End If
End Sub

Edit:
To follow up on my 'aside' below, I did some experimenting, and Comintern was onto something when s/he provided a link to this issue. The problem described above is to do with ScreenUpdating. If Screenupdating is set to false when the status bar is changed, the character widths of ChrW(9608) and ChrW(9620) are the same.
I've no idea why, but it does the trick. So you'll want to do the following:
Application.Screenupdating = False
'code which changes the status bar
Application.Screenupdating = True
(my previous comment continues below)
I prefer this pairing:
strFilledChar = ChrW(&H2588) 'a black rectangle, "Full Block"
strUnfilledChar = ChrW(&H2584) 'an array of sparse dots, "Light Shade"
Or this one:
strFilledChar = ChrW(&H2588) 'a black rectangle, "Full Block"
strUnfilledChar = ChrW(&H2500) 'a horizontal line, "Block Drawings Light Horizontal"
(as an aside, I encountered the same problem as described in the question whereby ChrW(9608) and ChrW(9620) have different widths - but only in one of my workbooks. In another workbook, they have the same widths and so the progress bar displays properly. I have no idea why.)

Related

Word VBA Progress Bar with Unknown Number of Steps

I have a macro that loops through an unknown number of times. The number of times varies based on a total number of rows in multiple tables in a reference document, and that number of rows will vary across reference documents that may be used. The relevant snippet of code for the loop is below:
For Each oRow In oTbl.Rows
p = p + 1
Helper.ProgressIndicator_Code (p)
strPhrase = Split(Trim(oRow.Range.Cells(1).Range.Text), vbCr)(0)
strRule = Split(Trim(oRow.Cells(2).Range.Text), vbCr)(0)
If strPhrase <> "" Then
If Not strStartWord = vbNullString Then
'Process defined sections
arrEndWords = Split(strEndWord, "|")
For lngIndex = 0 To UBound(arrEndWords)
Set oRng = GetDocRange(strStartWord, arrEndWords(lngIndex))
If Not oRng Is Nothing Then Exit For
Next lngIndex
Else
'Process whole document
Set oRng = m_oDocCurrent.Range
End If
If Not oRng Is Nothing Then
Set oRngScope = oRng.Duplicate
With oRng.Find
.Text = strPhrase
Do While .Execute
If Not oRng.InRange(oRngScope) Then Exit For
oRng.HighlightColorIndex = wdTurquoise
If strRule <> "" Then
Set oComment = m_oDocCurrent.Comments.Add(Range:=oRng, Text:=strUsr & ": " & strRule)
oComment.Author = UCase("WordCheck")
oComment.Initial = UCase("WC")
End If
Loop
End With
End If
End If
Next oRow
The progress bar is a classic progress bar for which a label field width is updated using the below code based on a value of p as updated in the above code:
Sub progress(pctCompl As Integer)
ProgressIndicator.Text.Caption = pctCompl & "% Completed"
ProgressIndicator.Bar.Width = pctCompl * 2
DoEvents
End Sub
Here's my problem: The value of p varies based on which reference document is used, so my progress bar is never even approximately accurate with respect to the processing of the VBA macro. The progress bar doesn't have to be exact, merely close and to indicate that progress is being made and nothing has hung.
I'm not looking for written code, just would be very grateful for suggestions or advice as to approaches for making my progress bar more accurate so that I can learn (e.g., I just ran the macro for three different reference documents - one gave me 25%, one gave 44%, and one gave 82%; none showed even close to 100% when completed). Essentially I need to divide i by an unknown number to get my percentage, which is clearly impossible, so some function for a close approximation is needed.
Edit: New code based on #macropod suggestion.
Dim strCheckDoc As String, docRef As Document, projectPath As String, _
j As Integer, i As Integer, k As Integer, oNumRows as Long
j = 1
For i = 0 To UBound(strUsr)
strCheckDoc = [path to reference document unique to each strUsr]
Set docRef = Documents.Open(strCheckDoc, ReadOnly:=True, Visible:=False)
For k = 1 To docRef.Tables.Count
oNumRows = oNumRows + docRef.Tables(i).Rows.Count
Next k
Next i
Then the code to update the progress bar is:
Dim pctCompl As Single
pctCompl = Round((p / oNumRows) * 100)
ProgressIndicator.Text.Caption = pctCompl & "% Completed"
ProgressIndicator.Bar.Width = pctCompl * 2
DoEvents
The progress bar now gets to 64% when complete (i.e., it should be at 100%). I'm also working on a way to make oNumRows only count a row if the row has content in the first column.

Application.Match not exact value

Have a piece of code that looks for matches between 2 sheets (sheet1 is customer list and rData is copied pdf with invoices). It usually is exact match but in some cases I'm looking for 6 first characters that matches rData
Dim rData As Variant
Dim r As Variant
Dim r20 As Variant
Dim result As Variant
Dim i As Long
rData = ActiveWorkbook.Sheets(2).Range("A1:A60000")
r20 = ActiveWorkbook.Sheets(1).Range("C2:C33")
For Each r In r20
result = Application.Match(r, rData, 0)
If Not IsError(result) Then
For i = 1 To 5
If (result - i) > 0 Then
If (Left(Trim(rData(result - i, 1)), 3) = "418") Then
MsgBox "customer: " & r & ". invoice: " & rData(result - i, 1)
End If
End If
Next
For i = 1 To 15
If (result + i) > 0 Then
If (Left(Trim(rData(result + i, 1)), 3) = "418") Then
MsgBox "customer: " & r & ". invoice: " & rData(result + i, 1)
End If
End If
Next
End If
Next r
End Sub
Only part of this that is giving me a headache is this part result = Application.Match(r, rData, 0). How do it get match for not exact match?
Sample of Sheet1
This is what more or less looks like. Matching after CustomerNumber# is easy because they are the same every invoice. BUT sometimes invoice does not have it so I'm searching after CustomerName and sometimes they have uppercase letters, sometimes there is extra stuff behind it and therefore it cannot find exact match.
Hope it makes sense.
To match the customer name from your customer list to the customer name in the invoice even if it has extra characters appended, you can use the wildcard * in Match().
You also have a typo in the Match() function. r20 should be rData.
This is your code with the fixes applied:
Sub Test()
'v4
Dim rData As Variant
Dim r As Variant
Dim r20 As Variant
Dim result As Variant
Dim i As Long
rData = ActiveWorkbook.Sheets(2).Range("A1:A60000")
r20 = ActiveWorkbook.Sheets(1).Range("C2:C33")
For Each r In r20
result = Application.Match(r & "*", rData, 0) ' <~ Fixed here
If Not IsError(result) Then
For i = 1 To 5
If (result - i) > 0 Then
If (Left(Trim(rData(result - i, 1)), 3) = "418") Then
MsgBox "customer: " & r & ". invoice: " & rData(result - i, 1)
End If
End If
Next
For i = 1 To 15
If (result + i) > 0 Then
If (Left(Trim(rData(result + i, 1)), 3) = "418") Then
MsgBox "customer: " & r & ". invoice: " & rData(result + i, 1)
End If
End If
Next
End If
Next r
End Sub
Notes:
Match() is case insensitive, so it works with different capitalisations.
The data in Sheets(2) must all be text for Match() to work correctly with wildcards.
EDIT1: New better version
EDIT2: Refactored constants and made data ranges dynamic
EDIT3: Allows for any prefix to an invoice number of a fixed length
The following is a better, rewritten version of your code:
Sub MuchBetter()
'v3
Const s_InvoiceDataWorksheet As String = "Sheet2"
Const s_InvoiceDataColumn As String = "A:A"
Const s_CustomerWorksheet As String = "Sheet1"
Const s_CustomerStartCell As String = "C2"
Const s_InvoiceNumPrefix As String = "418"
Const n_InvoiceNumLength As Long = 8
Const n_InvScanStartOffset As Long = -5
Const n_InvScanEndOffset As Long = 15
Dim ƒ As Excel.WorksheetFunction: Set ƒ = Excel.WorksheetFunction ' Shortcut
With Worksheets(s_InvoiceDataWorksheet).Range(s_InvoiceDataColumn)
With .Parent.Range(.Cells(1), .Cells(Cells.Rows.Count).End(xlUp))
Dim varInvoiceDataArray As Variant
varInvoiceDataArray = ƒ.Transpose(.Cells.Value2)
End With
End With
With Worksheets(s_CustomerWorksheet).Range(s_CustomerStartCell)
With .Parent.Range(.Cells(1), .EntireColumn.Cells(Cells.Rows.Count).End(xlUp))
Dim varCustomerArray As Variant
varCustomerArray = ƒ.Transpose(.Cells.Value2)
End With
End With
Dim varCustomer As Variant
For Each varCustomer In varCustomerArray
Dim dblCustomerIndex As Double
dblCustomerIndex = Application.Match(varCustomer & "*", varInvoiceDataArray, 0)
If Not IsError(dblCustomerIndex) _
And varCustomer <> vbNullString _
Then
Dim i As Long
For i = ƒ.Max(dblCustomerIndex + n_InvScanStartOffset, 1) _
To ƒ.Min(dblCustomerIndex + n_InvScanEndOffset, UBound(varInvoiceDataArray))
Dim strInvoiceNum As String
strInvoiceNum = Right$(Trim$(varInvoiceDataArray(i)), n_InvoiceNumLength)
If (Left$(strInvoiceNum, Len(s_InvoiceNumPrefix)) = s_InvoiceNumPrefix) Then
MsgBox "customer: " & varCustomer & ". invoice: " & strInvoiceNum
End If
Next
End If
Next varCustomer
End Sub
Notes:
It is a good idea to use constants so all literal values are typed once only and kept grouped together.
Using the RVBA naming convention greatly increases the readability of the code, and reduces the likelihood of bugs.
Using long, appropriately named variables makes the code essentially self-documenting.
Using .Value2 whenever reading cell values is highly recommended (it avoids implicit casting, making it slightly faster as well as eliminating certain issues caused by the casting ).
Surprisingly, in VBA there are good reasons to put a variable declaration as close as possible to the first use of the variable. Two such reasons are 1) it improves readability, and 2) it simplifies future refactoring. Just remember that the variable is not reinitialised every time the Dim is encountered. Initialisation only occurs the first time.
The twin loops have been rolled into one according to the DRY principle.
Whilst the check for an empty customer name/number is not strictly necessary if you can guarantee it will never be so, it is good defensive programming as an empty value will cause erroneous results.
The negative index check inside the loop has been removed and replaced with the one-time use of the Max() worksheet function in the For statement.
The Min() worksheet function is also used in the For statement to avoid trying to read past the end of the array.
Always use worksheet functions on the WorksheetFunction object unless you are explicitly checking for errors, in which case use the Application object.

Hysteresis thresholding in visual basic

I'm trying to make a hysteresis thresholding in visual basic for canny edge detection. As I'm new into this topic and new to vb (I mainly use php), many of the references I read,point out that I need to do this. Since the image is already on black and white, I only get 1 color for the intensities. I already did the gaussian blur, grayscale, sobel mask, and non maxima supression in only seconds. but on hysteresis, the time to execute the function is taking too long. I don't know where I did wrong. The image is on 640 x 480 if that helps. I try to change the resolution to smaller one, it's indeed faster but I want to keep the resolution to 640 x 480. I already change the code, and this is my final approach
Dim bmp_thres As New Bitmap(pic_nonmaxima)
Dim visited_maps As New List(Of String)
Dim threshold_H As Integer = 100
Dim threshold_L As Integer = 50
Dim Ycount As Integer
For Ycount = 1 To bmp_thres.Height - 2
Dim Xcount As Integer
For Xcount = 1 To bmp_thres.Width - 2
'check current pointer
Dim currPointer As String = Xcount & "," & Ycount
'find if coordinate visited already
Dim find_array As String
If visited_maps IsNot Nothing Then
find_array = visited_maps.Contains(currPointer)
Else
find_array = "False"
End If
If find_array Then
'if existed, do nothing
Else
'if not, do something
Dim currThreshold As Integer
Dim currColor As Color
currColor = bmp_thres.GetPixel(Xcount, Ycount)
currThreshold = currColor.R
'add coordinate into visited maps
Dim visited As String = Xcount & "" & Ycount
visited_maps.Add(visited)
If currThreshold > threshold_H Then
bmp_thres.SetPixel(Xcount, Ycount, Color.FromArgb(255, 255, 255))
Else
bmp_thres.SetPixel(Xcount, Ycount, Color.FromArgb(0, 0, 0))
'check connectedness
Dim coord_N As String = Xcount & "," & Ycount + 1
Dim coord_E As String = Xcount + 1 & "," & Ycount
Dim coord_S As String = Xcount & "," & Ycount - 1
Dim coord_W As String = Xcount - 1 & "," & Ycount
Dim coord_NE As String = Xcount + 1 & "," & Ycount + 1
Dim coord_SE As String = Xcount + 1 & "," & Ycount - 1
Dim coord_SW As String = Xcount - 1 & "," & Ycount - 1
Dim coord_NW As String = Xcount - 1 & "," & Ycount + 1
Dim myCoord As New List(Of String)
myCoord.Add(coord_N)
myCoord.Add(coord_E)
myCoord.Add(coord_S)
myCoord.Add(coord_W)
myCoord.Add(coord_NE)
myCoord.Add(coord_SE)
myCoord.Add(coord_SW)
myCoord.Add(coord_NW)
For Each coord In myCoord
If Not visited_maps.Contains(coord) Then
'Split by ,
Dim split_Coord() As String = Split(coord, ",")
'check thres on coord
Dim coordColor As Color = bmp_thres.GetPixel(split_Coord(0), split_Coord(1))
Dim coordThres As Integer = coordColor.R
If coordThres > threshold_H Then
bmp_thres.SetPixel(split_Coord(0), split_Coord(1), Color.FromArgb(255, 255, 255))
Else
bmp_thres.SetPixel(split_Coord(0), split_Coord(1), Color.FromArgb(0, 0, 0))
End If
End If
visited_maps.Add(coord)
Next 'end if foreach
End If ' end if checking current threshold
End If 'end if find coord in visited maps
Next 'end for xcount
Next 'end for ycount
Return bmp_thres
Or if you spot some wrong codes I did, please point out to me.
If I get it right, when we do hysteresis thresholding, we first check the coordinate if it's visited already, if it's visited we check the next coordinate. if it's not, we add the coordinate into the visited maps and if the current coordinate is larger than the threshold high, we change the pixel value into white else black. then we check the connectedness, if they pass the threshold low, we change the pixel value into white else black. then we add all the connectedness into visited maps. repeat.
What can I do to reduce the time ? or please point out my mistake. any help will be appreciated. sorry for the english if you didnt understand. this will help my final year project T_T
I think this could be on topic at Code Review as it is working (albeit slowly). But just in case it isn't, I'm leaving this here as a partial answer.
You have a slight bug in your coordinate search. Regardless of whether it is already in visited_maps or not you still add it in, which will be a lot of extra results in the list.
If Not visited_maps.Contains(coord) Then
' YOUR CODE
End If
visited_maps.Add(coord)
This line: visited_maps.Add(coord) needs to be inside the If so you don't have repeat values expanding your list further than it needs to. A 640 * 480 px image will create over 300 000 entries into your list, and with this coordinate bug it will have even more.
List is probably also not the most appropriate type, something like a HashSet is better because you don't need to access by index. Have a look at What is the difference between HashSet and List?
When you call Color.FromArgb(255, 255, 255) you are creating a new Color object every time. That's going to be at least 300 000 objects again, when you could declare one instance for black and another for white at the top and then use those as needed.
I'm not sure what the performance difference of using the Point structure over a comma-separated string would be, but it would save a lot of splitting/concatenation and be much nicer to read.
Dim currPointer As String = Xcount & "," & Ycount
Dim coord_N As String = Xcount & "," & Ycount + 1
Would become
Dim currPointer As Point = New Point(Xcount, Ycount)
Dim coord_N as Point = New Point(currPointer.X, currPointer.Y + 1)
There are still more things wrong but they are fairly minor so I'll leave them off for now

Extracting text from string between two identical characters using VBA

Let's say I have the following string within a cell:
E. Stark, T. Lannister, A. Martell, P Baelish, B. Dondarrion, and J. Mormont. Increased levels of nudity across Westeros contributes to its sporadic seasonal climate. Nat. Proc. Aca. Sci. (2011) 3: 142-149.
And I want to extract only the title from this. The approach I am considering is to write a script that says "Pull text from this string, but only if it is more than 50 characters long." This way it only returns the title, and not stuff like " Stark, T" and " Martell, P". The code I have so far is:
Sub TitleTest()
Dim txt As String
Dim Output As String
Dim i As Integer
Dim rng As Range
Dim j As Integer
Dim k As Integer
j = 5
Set rng = Range("A" & j) 'text is in cell A5
txt = rng.Value 'txt is string
i = 1
While j <= 10 'there are five references between A5 and A10
k = InStr(i, txt, ".") - InStr(i, txt, ". ") + 1 'k is supposed to be the length of the string returned, but I can't differenciate one "." from the other.
Output = Mid(txt, InStr(i, txt, "."), k)
If Len(Output) < 100 Then
i = i + 1
ElseIf Len(Output) > 10 Then
Output = Mid(txt, InStr(i, txt, "."), InStr(i, txt, ". "))
Range("B5") = Output
j = j + 1
End If
Wend
End Sub
Of course, this would work well if it wasn't two "." I was trying to full information from. Is there a way to write the InStr function in such a way that it won't find the same character twice? Am I going about this in the wrong way?
Thanks in advance,
EDIT: Another approach that might work (if possible), is if I could have one character be " any lower case letter." and ".". Would even this be possible? I can't find any example of how this could be achieved...
Here you go, it works exactly as you wish. Judging from your code I am sure that you can adapt it for your needs quite quickly:
Option Explicit
Sub ExtractTextSub()
Debug.Print ExtractText("E. Stark, T. Lannister, A. Martell, P Baelish, B. Dondarrion, and J. Mormont. Increased levels of nudity across Westeros contributes to its sporadic seasonal climate. Nat. Proc. Aca. Sci. (2011) 3: 142-149.")
End Sub
Public Function ExtractText(str_text As String) As String
Dim arr As Variant
Dim l_counter As Long
arr = Split(str_text, ".")
For l_counter = LBound(arr) To UBound(arr)
If Len(arr(l_counter)) > 50 Then
ExtractText = arr(l_counter)
End If
Next l_counter
End Function
Edit: 5 votes in no time made me improve my code a bit :) This would return the longest string, without thinking of the 50 chars. Furthermore, on Error handlaer and a constant for the point. Plus adding a point to the end of the extract.
Option Explicit
Public Const STR_POINT = "."
Sub ExtractTextSub()
Debug.Print ExtractText("E. Stark, T. Lannister, A. Martell, P Baelish, B. Dondarrion, and J. Mormont. Increased levels of nudity across Westeros contributes to its sporadic seasonal climate. Nat. Proc. Aca. Sci. (2011) 3: 142-149.")
End Sub
Public Function ExtractText(str_text As String) As String
On Error GoTo ExtractText_Error
Dim arr As Variant
Dim l_counter As Long
Dim str_longest As String
arr = Split(str_text, STR_POINT)
For l_counter = LBound(arr) To UBound(arr)
If Len(arr(l_counter)) > Len(ExtractText) Then
ExtractText = arr(l_counter)
End If
Next l_counter
ExtractText = ExtractText & STR_POINT
On Error GoTo 0
Exit Function
ExtractText_Error:
MsgBox "Error " & Err.Number & Err.Description
End Function

Unexpected String Results

I have the following code to check values entered into two input boxes, if both values are zero then the MsgBox should display "Stop!" (I will change this later to exiting the sub but I am using a MsgBox for testing)
From testing I've seen these results:
A zero in both strings produces the expected message box.
A non zero in the first string followed by any non zero value in the second string does nothing (as expected).
A zero in the first string followed by a second string value equal to or greater than 10 produces the message box (unexpected).
I've also noticed that if the second string is 6-9 it is displayed as x.00000000000001%. I think this is a floating point issue and could be related? This behaviour occurs without the IF... InStr function too.
Option Explicit
Sub Models()
Dim MinPer As String, MaxPer As String, Frmula As String
Dim Data As Worksheet, Results As Worksheet
Set Data = Sheets("Data")
Set Results = Sheets("Results")
Application.ScreenUpdating = False
MinPer = 1 - InputBox("Enter Minimum Threshold Percentage, do not include the % symbol", _
"Minimum?") / 100
MaxPer = 1 + InputBox("Enter Maximum Threshold Percentage, do not include the % symbol", _
"Maximum?") / 100
If (InStr(MinPer, "0") = 0) And (InStr(MaxPer, "0") = 0) Then
MsgBox "STOP!"
End If
' Remainder of code...
This is the most interesting problem I've come across so far in VBA and welcome any discussion about it.
Edit: I use this code to display on screen the paramaters for the end-user to see. Hence how I noticed the .00000000001% issue:
.Range("D2").Value = "Min is " & 100 - MinPer * 100 & "%"
.Range("D3").Value = "Max is " & MaxPer * 100 - 100 & "%"
Two things
1) Declare MinPer, MaxPer as Long or a Double and not a String as you are storing outputs from a calculation
2) Don't directly use the InputBox in the calculations. Store them in a variable and then if the input is valid then use them in the calculation
Dim MinPer As Double, MaxPer As Double, Frmula As String
Dim Data As Worksheet, Results As Worksheet
Dim n1 As Long, n2 As Long
Set Data = Sheets("Data")
Set Results = Sheets("Results")
Application.ScreenUpdating = False
On Error Resume Next
n1 = Application.InputBox(Prompt:="Enter Minimum Threshold Percentage, do not include the % symbol", _
Title:="Minimum?", Type:=1)
On Error GoTo 0
If n1 = False Then
MsgBox "User cancelled"
Exit Sub
End If
On Error Resume Next
n2 = Application.InputBox(Prompt:="Enter Maximum Threshold Percentage, do not include the % symbol", _
Title:="Maximum?", Type:=1)
On Error GoTo 0
If n2 = False Then
MsgBox "User cancelled"
Exit Sub
End If
If n1 = 0 And n2 = 0 Then
MsgBox "STOP!"
End If
MinPer = 1 - (Val(n1) / 100)
MaxPer = 1 + (Val(n2) / 100)
This is because the number "10" has a "0" in the string (second character) so both evaluate to true.
Try this instead:
If (MinPer = "0") And (MaxPer = "0") Then
MsgBox "STOP!"
End If
For additional control save the user input (MinPer , MaxPer) and THEN text them for validity before performing nay mathematical operations on them.
InStr(MinPer, "0") is just checking to see whether the string contains a zero
character.
You need to convert the string value to an integer. Use the IsNumeric and CInt functions
to do that. See this URL:
vba convert string to int if string is a number
Dim minPerINT as Integer
Dim maxPerINT as Integer
If IsNumeric(minPer) Then
minPerINT = CInt(minPer)
Else
minPerINT = 0
End If
If IsNumeric(maxPer) Then
maxPerINT = CInt(maxPer)
Else
maxPerINT = 0
End If
If minPerINT = 0 and maxPerINT=0 Then
MsgBox "STOP!"
End If
Depending on what data can be entered It may also be a good idea to check if the length
of the data is zero using the len() function.