Coreldraw VBA adding strings together in color assignment - vba

Im sure im doing something stupid here but I cant get the following to work. I Have the following line of code
ActiveSelectionRange.ApplyCustomHatchFill ANG.value, LS.value, 0, 10, 0, LT.value, CreateRGBColor(255,255,255), Style:=OutlineStyles(TextBox5.value), DashDotLength:=(TextBox6.value), PenWidth:=(TextBox7.value), BackColor:=CreateRGBColor(255, 255, 255)
I am trying to replace the following part with variables
CreateRGBColor(255,255,255)
with the following
a = colbut.BackColor
R = a Mod 256
G = Int(a / 256) Mod 256
B = Int(a / 256 / 256) Mod 256
ActiveSelectionRange.ApplyCustomHatchFill ANG.value, LS.value, 0, 10, 0, LT.value, CreateRGBColor(" & R & ", " & G & ", " & B & ", " & "), Style:=OutlineStyles(TextBox5.value), DashDotLength:=(TextBox6.value), PenWidth:=(TextBox7.value), BackColor:=CreateRGBColor(255, 255, 255)
It keeps throwing the error "Wrong number of arguments or invalid property assignment"
I have tried all variations of adding the strings but to no avail
Any help is greatly appreciated
Mark

Related

To fill color on a number of selected shapes by user choice

Dear Stackoverflow experts,
I am trying to write a code that will let me select a number of shapes, then prompt me what color should I fill up for each selected shape.
While the following code work well for me
Sub ChangeColorBasedonInput()
Dim myColor(1 To 10) As Long
Dim x As Integer
Dim z As Integer
Dim colorChoice As Integer
myColor(1) = RGB(77, 60, 47)
myColor(2) = RGB(207, 189, 155)
myColor(3) = RGB(192, 113, 86)
myColor(4) = RGB(232, 199, 103)
myColor(5) = RGB(174, 176, 179)
myColor(6) = RGB(164, 55, 37)
myColor(7) = RGB(237, 215, 157)
myColor(8) = RGB(123, 125, 128)
myColor(9) = RGB(230, 182, 164)
myColor(10) = RGB(70, 71, 73)
On Error Resume Next
z = ActiveWindow.Selection.ShapeRange.Count
For x = 1 To z
With ActiveWindow.Selection.ShapeRange(x)
colorChoice = InputBox("Please select the color you want for Shape " & x & ", from 1 - 10")
.Fill.ForeColor.RGB = myColor(colorChoice)
End With
Next x
End Sub
I realize its quite hassle to have to enter the color choice one by one, so I try to write the code below so that I only need to enter my fill color selection once (from the above array and it will automatically fill up the shapes for me. eg when I select 7, I hope it fills the shapes from myColor(7) onwards. But somehow the following codes doesn't work. Wonder if anyone could point out my mistake here.
Sub ChangeColorBasedon_EnterOnceOnly()
Dim myColor(1 To 10) As Long
Dim x As Integer
Dim z As Integer
Dim colorChoice As Integer
myColor(1) = RGB(77, 60, 47)
myColor(2) = RGB(207, 189, 155)
myColor(3) = RGB(192, 113, 86)
myColor(4) = RGB(232, 199, 103)
myColor(5) = RGB(174, 176, 179)
myColor(6) = RGB(164, 55, 37)
myColor(7) = RGB(237, 215, 157)
myColor(8) = RGB(123, 125, 128)
myColor(9) = RGB(230, 182, 164)
myColor(10) = RGB(70, 71, 73)
On Error Resume Next
z = ActiveWindow.Selection.ShapeRange.Count
colorChoice = InputBox("Please select the color you want for Shape " & x + 1 & ", from 1 - 10")
For x = 1 To z
With ActiveWindow.Selection.ShapeRange(x)
.Fill.ForeColor.RGB = myColor(colorChoice)
End With
colorChoice = colorChoice + x
Next x
End Sub
Just before Next x add
If colorChoice > UBound(myColor) Then colorChoice = LBound(myColor)
or you may exceed the bounds of the array if too many shapes are selected

How to loop multiple condition in vb.net

Can i loop in 2 condition vb net?
Dim s As New DirectoryInfo("C:/ProgramFiles")
Dim files As FileInfo() = s.GetFiles("*.jpg")
For i As Integer = 1 To files.Count 'condition 1 get loop Number of Files
For Each f As FileInfo In files 'condition 2 get loop Files
Dim bmp As New Bitmap(f.FullName)
If bmp.Width >= 1000 Then
Console.WriteLine(i & ". True--> Name : " & f.FullName & " Width: " & bmp.Width.ToString() + " > Height: " & bmp.Height.ToString())
Else
Console.WriteLine(i & ". False--> Name : " & f.FullName & " Width: " & bmp.Width.ToString() + " > Height: " & bmp.Height.ToString())
End If
Next
Next
Console.Read()
How to get number in each result?
Example number: 1, 2, 3, ...
1. True--> Name : a.jpg Width:1000 > Height:500
2. False--> Name : b.jpg Width:800 > Height:400
3. True--> Name : c.jpg Width:1200 > Height:600
4...
but the code result is:
1. True--> Name : a.jpg Width:1000 > Height:500
1. False--> Name : b.jpg Width:800 > Height:400
1. True--> Name : c.jpg Width:1200 > Height:600
2. True--> Name : a.jpg Width:1000 > Height:500
2. False--> Name : b.jpg Width:800 > Height:400
2. True--> Name : c.jpg Width:1200 > Height:600
3...
I'm very beginner coding in vb.net
Thanks for your advice,.
Remove the outer loop, you don't need it. In order to ouput an index, just use a variable and increment it in the loop:
Dim s As New DirectoryInfo("C:/ProgramFiles")
Dim files As FileInfo() = s.GetFiles("*.jpg")
Dim count As Integer = 1
For Each f As FileInfo In files
Dim bmp As New Bitmap(f.FullName)
If bmp.Width >= 1000 Then
Console.WriteLine(count & ". True--> Name : " & f.FullName & " Width: " & bmp.Width.ToString() + " > Height: " & bmp.Height.ToString())
Else
Console.WriteLine(count & ". False--> Name : " & f.FullName & " Width: " & bmp.Width.ToString() + " > Height: " & bmp.Height.ToString())
End If
count += 1
Next
Console.Read()
/* Simple*/
Dim Count As Integer = 1;
End of the I loop increase it with count++;
and replace i with this variable so you can get proper indexing

How do I add probability to a random number generator

I'm trying to add probability to my random number generator.
Here is the random number generator code.
'Generate 3 random numbers'
Dim rn As New Random
Dim result1, result2, result3 As Integer
result1 = rn.Next(1, 12)
result2 = rn.Next(1, 12)
result3 = rn.Next(1, 12)
So this code will generate 3 random numbers and save them into separate variables.
I would like the following to happen.
1 has a 20% chance of being selected
2 has a 28% chance of being selected
values need to be between 1 and 12 and I need to select 3 numbers
Something along that line.
I found this but it seems to be a little off topic?
Could this be used for what I want?
Make an array with 100 items. Put the value 1 into 20 spots in the array, the value 2 into 28 spots in the array, as so on. Then pick a random array index. Depending on your values and probabilities you could simplify or adjust the size of the array.
Alternatively, you can just store boundary pairs ( {1, 20}, {2, 48}, ... ), grab a random number less than your highest boundary value, and find the number for the smallest pair where your boundary value is greater than or equal to the random result. Here's an example:
Private rnd As New Random()
Public Function GetValue() As Integer
'max value is 100
Dim boundaries = {
{20, 48, 56, 60, 69, 74, 77, 82, 84, 88, 92, 100},
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
}
Dim r As Integer = rnd.Next(1, 101) 'upper range is exclusive
For i As Integer = 0 To boundaries.GetUpperBound(1)
If boundaries(0, i) >= r Then Return boundaries(1, i)
Next
Throw New Exception() 'code should not be able to get here
End Function
Dim result1 As Integer = GetValue()
Dim result2 As Integer = GetValue()
Dim result3 As Integer = GetValue()
I while ago I was looking into generating a random Name list that mimicked he national USA population bases; the several thousand names needed mimicked the repetitive usage seen in the USA population. To do this I needed a list of First names along with Sur names and their frequency of usage. So I pulled the seed data from the Social Security Administration to obtain the top 1000 First and Sur names, along with their frequency of usage.
Think of three columns. The first column is a list of names, the second their frequency seen in the population bases, and the third is the rolling totals of their frequency added row to row:
Marry, 57, 10
John, 40, 60
Lloyd, 2, 62
Zac, 1, 61
Read a seed file of any size into an area, along with the weight value for each name (or number). This script assigns the rolling total weight values, then generates a random number between 1 and the sum of all weights. Checks that random number against the rolling sum of weights to locate the number associated with that weight.
Looking at the above example you have a 57% chance to generate Marry, and a 1% chance to generate Zac.
The below script is a more robust example of the general idea. It will generate random numbers between 1 – 25, based on the probability of their weights. The portion of the script that generates the random numbers between 1 – 25 loops 128 times to give you 128 numbers based on their probability of being selected.
I basically used something like this to generate thousands of random names to mimic a population bases that mirrored the USA.
Dim iVar001, iVar002, iVar003
Dim iMin, iMax, iRand, iRandN
Dim iRow, iCol
Dim aName
Dim aList()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' iMin is the lower range of random numbers, iMax is the upper
' range of random numbers being generated
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
iMin = 1
iMax = 25
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Create a dinamic sized 2D area used to generate a random number
' based on weighted Probability. Array values array(0, 0) threw
' array(x, 0), where “x” is the upper range value of the number
' to be generated, contains the range of random numbers you will
' be generating. Array values array(0, 1) to array(x, 1) contains
' the weight value for each number. Array values array(0,2) to
' array(x,2) contains the rolling sun of the weight values.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ReDim aList(iMax - 1, 2)
iVar001 = 0
Do While iVar001 <= (iMax - 1)
Randomize
aList(iVar001, 0) = iVar001 + 1
aList(iVar001, 1) = Int(((20 * iMax) - iMin + 1)* Rnd + iMin)
If iVar001 = 0 Then
aList(iVar001, 2) = aList(iVar001, 1)
Else
aList(iVar001, 2) = aList(iVar001, 1) + aList(iVar001 - 1, 2)
End If
aName = aName & aList(iVar001, 0) & " - " & aList(iVar001, 1) & _
" - " & aList(iVar001, 2) & vbCrLf
iVar001 = iVar001 + 1
Loop
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Generate a message box containing the array values.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
MsgBox ("List of numbers," & vbCrLf& "weight values and" & _
vbCrLf & "totals of weights." & vbCrLf & vbCrLf & aName)
aName = ""
iCount000 = 1
iCount001 = 1
iCol001 = 0
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Generate a randon number between 1 and the total of weight
' values. Then step threw the array values until the the random
' value is nolonger less than or equal to the running wieght value,
' and record the number associated with that running weight value.
' The count of random =numbers generates is controled by the
' following Do While iCount000 <= 198.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''
Do While iCount000 <= 198
Randomize
iRand = Int((aList(UBound(aList,1),UBound(aList,2)) - iMin + 1)* Rnd + iMin)
iVar001 = 0
Do While iRand >= aList(iVar001, 2)
iVar001 = iVar001 + 1
iRandN = aList(iVar001, 0)
Loop
iCount000 = iCount000 + 1
If iCount001 = "19" Then
aName = vbCrLf & aName
iCount001 = 0
Else
iCount001 = iCount001 + 1
End If
aName = iRandN & ", " & aName
Loop
MsgBox "List of random numbers in range " & iMin & " to " & iMax & "," & vbCrLf & _
"based on weighted probability." & vbCrLf & vbCrLf & aName

.NET Greater than/less than not working

I am having the issue of my greater than/less than comparison not working.
Here is what is happening:
Let's say I have a square, and I'm getting the minimum and maximum points of this square in space, ex. minPoint = (0,0,0) and maxPoint = (1,1,1)
I am placing text on this face but I need the text to be inside the square face, if it isn't then I reduce the size until it fits.
Here is my code after properly getting the distance of both the minPt to the maxPt, and also the minPt to the text location.
Here is my line of code that is not working:
If sizeYPt <= sizeY And sizeXPt <= sizeX Then Exit Do
It works for most situations but I have had situations where it won't work. One thing I noticed is that sizeYPt is the same as sizeY, and sizeXPt is smaller than sizeX (which should all be true!!! argh)
Anyone know what the issue could be? Here is my full code:
Dim sizeY, sizeX, sizeYPt, sizeXPt As Double
Dim yVect, xVect As New VISIVector
yVect.Put(0, 1, 0)
xVect.Put(1, 0, 0)
sizeY = Util.dist2pts_alongAxis(centerPoint, p2, yVect)
sizeYPt = Util.dist2pts_alongAxis(centerPoint, txtPt, yVect)
sizeX = Util.dist2pts_alongAxis(p1, p2, xVect)
sizeXPt = Util.dist2pts_alongAxis(p1, txtPt, xVect)
If sizeY.ToString.Contains("-") Then sizeY = sizeY * -1
If sizeYPt.ToString.Contains("-") Then sizeYPt = sizeYPt * -1
If sizeX.ToString.Contains("-") Then sizeX = sizeX * -1
If sizeXPt.ToString.Contains("-") Then sizeXPt = sizeXPt * -1
MsgBox(sizeYPt & " | " & sizeY & vbNewLine & sizeXPt & " | " & sizeX)
If sizeYPt <= sizeY And sizeXPt <= sizeX Then Exit Do
Floating point math is inexact, you need a "close enough" check. Use, say, If SizeYPt - SizeY <= 1E-12

How can I convert a decimal to a fraction?

How do I convert a indefinite decimal (i.e. .333333333...) to a string fraction representation (i.e. "1/3"). I am using VBA and the following is the code I used (i get an overflow error at the line "b = a Mod b":
Function GetFraction(ByVal Num As Double) As String
If Num = 0# Then
GetFraction = "None"
Else
Dim WholeNumber As Integer
Dim DecimalNumber As Double
Dim Numerator As Double
Dim Denomenator As Double
Dim a, b, t As Double
WholeNumber = Fix(Num)
DecimalNumber = Num - Fix(Num)
Numerator = DecimalNumber * 10 ^ (Len(CStr(DecimalNumber)) - 2)
Denomenator = 10 ^ (Len(CStr(DecimalNumber)) - 2)
If Numerator = 0 Then
GetFraction = WholeNumber
Else
a = Numerator
b = Denomenator
t = 0
While b <> 0
t = b
b = a Mod b
a = t
Wend
If WholeNumber = 0 Then
GetFraction = CStr(Numerator / a) & "/" & CStr(Denomenator / a)
Else
GetFraction = CStr(WholeNumber) & " " & CStr(Numerator / a) & "/" & CStr(Denomenator / a)
End If
End If
End If
End Function
As .333333333 is not 1/3 you will never get 1/3 but instead 333333333/1000000000 if you do not add some clever "un-rounding" logic.
Here is a solution for handling numbers with periodic decimal representation I remember from school.
A number 0.abcdabcd... equals abcd/9999. So 0.23572357... equals 2357/9999 exactly. Just take that many 9s as your pattern is long. 0.11111... equals 1/9, 0.121212... equals 12/99, and so on. So try just searching a pattern and setting the denominator to the corresponding number. Of course you have to stop after some digits because you will never know if the pattern is repeated for ever or just many times. And you will hit the rounding error in the last digit, so you still need some clever logic.
This only works in Excel-VBA but since you had it tagged "VBA" I will suggest it. Excel has a custom "fraction" format that you can access via "Format Cells" (or ctrl-1 if you prefer). This particular number format is Excel-Specific and so does not work with the VBA.Format function. It does however work with the Excel Formula TEXT(). (Which is the Excel equivalent of VBA.Format. This can be accessed like So:
Sub Example()
MsgBox Excel.WorksheetFunction.Text(.3333,"# ?/?")
End Sub
To show more than one digit (Example 5/12) just up the number of question marks.
Google for "decimal to fraction" and you'll get about a gazillion results.
I really like this one, because it's simple, has source code (in RPL, similar to Forth, ~25 lines), and is pretty fast (it's written to run on a 4-bit, 4MHz CPU). The docs say:
In a book called Textbook of Algebra by G. Chrystal, 1st
edition in 1889, in Part II, Chapter 32, this improved continued fraction
algorithm is presented and proven. Odd to tell, Chrystal speaks of it as if it
were ancient knowledge.
This site seem to have a really nice implementation of this in JavaScript.
I would multiply by 10000000(or whatever you want depending on the precision), then simplify the resulting fraction (ie n*10000000/10000000)
You can approximate it. Essentially cycle through all numerators and denominators until you reach a fraction that is close to what you want.
int num = 1;
int den = 1;
double limit == 0.1;
double fraction = num / den;
while(den < 1000000 ) // some arbitrary large denominator
{
den = den + 1;
for(num = 0; num <= den; num++)
{
fraction = num / den;
if(fraction < n + limit && fraction > n - limit)
return (num + "/" + den);
}
}
This is slow and a brute force algorithm, but you should get the general idea.
In general, it'll be easier if you find the repeating part of the rational number. If you can't find that, you'll have a tough time. Let's say the number if 8.45735735735...
The answer is 8 + 45/100 + 735/999/100 = 8 1523/3330.
The whole number is 8.
Add 45/100 - which is .45, the part before the repeating part.
The repeating part is 735/999. In general, take the repeating part. Make it the numerator. The denominator is 10^(number of repeating digits) - 1.
Take the repeating part and shift it the appropriate number of digits. In this case, two, which means divide by 100, so 735/999/100.
Once you figure those parts out, you just need some code that adds and reduces fractions using greatest common fractions ...
Similar to CookieOfFortune's, but it's in VB and doesn't use as much brute force.
Dim tolerance As Double = 0.1 'Fraction has to be at least this close'
Dim decimalValue As Double = 0.125 'Original value to convert'
Dim highestDenominator = 100 'Highest denominator you`re willing to accept'
For denominator As Integer = 2 To highestDenominator - 1
'Find the closest numerator'
Dim numerator As Integer = Math.Round(denominator * decimalValue)
'Check if the fraction`s close enough'
If Abs(numerator / denominator - decimalValue) <= tolerance Then
Return numerator & "/" & denominator
End If
Next
'Didn't find one. Use the highest possible denominator'
Return Math.Round(denominator * decimalValue) & "/" & highestDenominator
...Let me know if it needs to account for values greater than 1, and I can adjust it.
EDIT: Sorry for the goofed up syntax highlighting. I can't figure out why it's all wrong. If someone knows how I can make it better, please let me know.
Python has a nice routine in its fractions module. Here is the working portion that converts a n/d into the closest approximation N/D where D <= some maximum value. e.g. if you want to find the closest fraction to 0.347, let n=347,d=1000 and max_denominator be 100 and you will obtain (17, 49) which is as close as you can get for denominators less than or equal to 100. The '//' operator is integer division so that 2//3 gives 0, i.e. a//b = int(a/b).
def approxFrac(n,d,max_denominator):
#give a representation of n/d as N/D where D<=max_denominator
#from python 2.6 fractions.py
#
# reduce by gcd and only run algorithm if d>maxdenominator
g, b = n, d
while b:
g, b = b, g%b
n, d = n/g, d/g
if d <= max_denominator:
return (n,d)
nn, dd = n, d
p0, q0, p1, q1 = 0, 1, 1, 0
while True:
a = nn//dd
q2 = q0+a*q1
if q2 > max_denominator:
break
p0, q0, p1, q1 = p1, q1, p0+a*p1, q2
nn, dd = dd, nn-a*dd
k = (max_denominator-q0)//q1
bound1 = (p0+k*p1, q0+k*q1)
bound2 = (p1, q1)
if abs(bound2[0]*d - bound2[1]*n) <= abs(bound1[0]*d - bound1[1]*n):
return bound2
else:
return bound1
1/ .3333333333 = 3 because 1/3 = .3333333333333, so whatever number you get do this,
double x = 1 / yourDecimal;
int y = Math.Ceil(x);
and now Display "1/" + y
It is not allways resoluble, since not all decimals are fractions (for example PI or e).
Also, you have to round up to some length your decimal before converting.
I know this is an old thread, but I came across this problem in Word VBA. There are so many limitations due to the 8 bit (16 digit) rounding, as well as Word VBA making decimals into scientific notation etc.. but after working around all these problems, I have a nice function I'd like to share that offers a few extra features you may find helpful.
The strategy is along the lines of what Daniel Buckner wrote. Basically:
1st) decide if it's a terminating decimal or not
2nd) If yes, just set the decimal tail / 10^n and reduce the fraction.
3rd) If it doesn't terminate, try to find a repeating pattern including cases where the repetition doesn't start right away
Before I post the function, here are a few of my observations of the risks and limitations, as well as some notes that may help you understand my approach.
Risks, limitations, explanations:
-> Optional parameter "denom" allows you to specify the denominator of the fraction, if you'd like it rounded. i.e. for inches you may want 16ths used. The fractions will still be reduced, however, so 3.746 --> 3 12/16 --> 3 3/4
-> Optional parameter "buildup" set to True will build up the fraction using the equation editor, typing the text right into the active document. If you prefer to have the function simply return a flat string representation of the fraction so you can store it programmatically etc. set this to False.
-> A decimal could terminate after a bunch of repetitions... this function would assume an infinite repetition.
-> Variable type Double trades off whole number digit for decimal digits, only allowing 16 digits total (from my observations anyway!). This function assumes that if a number is using all 16 of the available digits then it must be a repeating decimal. A large number such as 123456789876.25 would be mistaken for a repeating decimal, then returned as decimal number upon failing to find a pattern.
-> To express really large terminating decimal out of 10^n, VB can only handle 10^8 is seems. I round the origninal number to 8 decimal places, losing some accuracy perhaps.
-> For the math behind converting repeating patterns to fractions check this link
-> Use Euclidean Algorithm to reduce the fraction
Ok, here it is, written as a Word Macro:
Function as_fraction(number_, Optional denom As Integer = -1, Optional buildup As Boolean = True) As String
'Selection.TypeText Text:="Received: " & CStr(number_) & vbCrLf
Dim number As Double
Dim repeat_digits As Integer, delay_digits As Integer, E_position As Integer, exponent As Integer
Dim tail_string_test As String, tail_string_original As String, num_removed As String, tail_string_removed As String, removed As String, num As String, output As String
output = "" 'string variable to build into the fraction answer
number = CDbl(number_)
'Get rid of scientific notation since this makes the string longer, fooling the function length = digits
If InStr(CStr(number_), "E+") > 0 Then 'no gigantic numbers! Return that scientific notation junk
output = CStr(number_)
GoTo all_done
End If
E_position = InStr(CStr(number), "E") 'E- since postives were handled
If E_position > 0 Then
exponent = Abs(CInt(Mid(CStr(number), E_position + 1)))
num = Mid(CStr(number_), 1, E_position) 'axe the exponent
decimalposition = InStr(num, ".") 'note the decimal position
For i_move = 1 To exponent
'move the decimal over, and insert a zero if the start of the number is reached
If InStr(num, "-") > 0 And decimalposition = 3 Then 'negative sign in front
num = "-0." & Mid(num, InStr(num, ".") - 1, 1) & Mid(num, InStr(num, ".") + 1) 'insert a zero after the negative
ElseIf decimalposition = 2 Then
num = "0." & Mid(num, InStr(num, ".") - 1, 1) & Mid(num, InStr(num, ".") + 1) 'insert in front
Else 'move the decimal over, there are digits left
num = Mid(num, 1, decimalposition - 2) & "." & Mid(num, decimalposition - 1, 1) & Mid(num, decimalposition + 1)
decimalposition = decimalposition - 1
End If
Next
Else
num = CStr(number_)
End If
'trim the digits to 15, since VB rounds the last digit which ruins the pattern. i.e. 0.5555555555555556 etc.
If Len(num) >= 16 Then
num = Mid(num, 1, 15)
End If
number = CDbl(num) 'num is a string representation of the decimal number, just to avoid cstr() everywhere
'Selection.TypeText Text:="number = " & CStr(number) & vbCrLf
'is it a whole number?
If Fix(number) = number Then 'whole number
output = CStr(number)
GoTo all_done
End If
decimalposition = InStr(CStr(num), ".")
'Selection.TypeText Text:="Attempting to find a fraction equivalent for " & num & vbCrLf
'is it a repeating decimal? It will have 16 digits
If denom = -1 And Len(num) >= 15 Then 'repeating decimal, unspecified denominator
tail_string_original = Mid(num, decimalposition + 1) 'digits after the decimal
delay_digits = -1 'the number of decimal place values removed from the tail, in case the repetition is delayed. i.e. 0.567777777...
Do 'loop through start points for the repeating digits
delay_digits = delay_digits + 1
If delay_digits >= Fix(Len(tail_string_original) / 2) Then
'Selection.TypeText Text:="Tried all starting points for the pattern, up to half way through the tail. None was found. I'll treat it as a terminating decimal." & vbCrLf
GoTo treat_as_terminating
End If
num_removed = Mid(num, 1, decimalposition) & Mid(num, decimalposition + 1 + delay_digits) 'original number with decimal values removed
tail_string_removed = Mid(num_removed, InStr(CStr(num_removed), ".") + 1)
repeat_digits = 0 'exponent on 10 for moving the decimal place over
'Selection.TypeText Text:="Searching " & num_removed & " for a pattern:" & vbCrLf
Do
repeat_digits = repeat_digits + 1
If repeat_digits = Len(tail_string_removed) - 1 Or repeat_digits >= 9 Then 'try removing a digit, incase the pattern is delayed
Exit Do
End If
tail_string_test = Mid(num_removed, decimalposition + 1 + repeat_digits)
'Selection.TypeText Text:=vbTab & "Comparing " & Mid(tail_string_removed, 1, Len(tail_string_removed) - repeat_digits) & " to " & tail_string_test & vbCrLf
If Mid(tail_string_removed, 1, Len(tail_string_removed) - repeat_digits) = tail_string_test Then
'Selection.TypeText Text:=num & ", " & Mid(tail_string_removed, 1, Len(tail_string_removed) - repeat_digits) & " vs " & tail_string_test & vbCrLf
GoTo foundpattern
End If
Loop
Loop 'next starting point for pattern
foundpattern:
If delay_digits = 0 Then 'found pattern right away
numerator = CLng(Mid(CStr(number), decimalposition + 1 + delay_digits, CInt(repeat_digits)))
'generate the denominator nines, same number of digits as the numerator
bottom = ""
For i_loop = 1 To repeat_digits
bottom = bottom & "9"
Next
denominator = CLng(bottom)
Else 'there were numbers before the pattern began
numerator = CLng(Mid(num, decimalposition + 1, delay_digits + repeat_digits)) - CLng(Mid(num, decimalposition + 1, delay_digits))
'i.e. x = 2.73232323232... delay_digits = 1, repeat_digits = 2, so numerator = 732 - 7 = 725
bottom = ""
For i_loop = 1 To repeat_digits
bottom = bottom & "9"
Next
For i_loop = 1 To delay_digits
bottom = bottom & "0"
Next
denominator = CLng(bottom)
'i.e. 990... 725/990 = 145/198 = 0.7323232...
End If
Else ' terminating decimal
treat_as_terminating:
'grab just the decimal trail
If denom = -1 Then
number = Math.Round(number, 8) 'reduce to fewer decimal places to avoid overload
'is it a whole number now?
If Fix(number) = number Then 'whole number
output = CStr(number)
GoTo all_done
End If
num = CStr(number)
numerator = CLng(Mid(num, decimalposition + 1))
denominator = 10 ^ (Len(num) - InStr(num, "."))
Else 'express as a fraction rounded to the nearest denom'th reduced
numerator1 = CDbl("0" & Mid(CStr(num), decimalposition))
numerator = CInt(Math.Round(numerator1 * denom))
denominator = CInt(denom)
End If
End If
'reduce the fraction if possible using Euclidean Algorithm
a = CLng(numerator)
b = CLng(denominator)
Dim t As Long
Do While b <> 0
t = b
b = a Mod b
a = t
Loop
gcd_ = a
numerator = numerator / gcd_
denominator = denominator / gcd_
whole_part = CLng(Mid(num, 1, decimalposition - 1))
'only write a whole number if the number is absolutely greater than zero, or will round to be so.
If whole_part <> 0 Or (whole_part = 0 And numerator = denominator) Then
'case where fraction rounds to whole
If numerator = denominator Then
'increase the whole by 1 absolutely
whole_part = (whole_part / Abs(whole_part)) * (Abs(whole_part) + 1)
End If
output = CStr(whole_part) & " "
End If
'if fraction rounded to a whole, it is already included in the whole number
If numerator <> 0 And numerator <> denominator Then
'negative sign may have been missed, if whole number was -0
If whole_part = 0 And number_ < 0 Then
numerator = -numerator
End If
output = output & CStr(numerator) & "/" & CStr(denominator) & " "
End If
If whole_part = 0 And numerator = 0 Then
output = "0"
End If
all_done:
If buildup = True Then 'build up the equation with a pretty fraction at the current selection range
Dim objRange As Range
Dim objEq As OMath
Dim AC As OMathAutoCorrectEntry
Application.OMathAutoCorrect.UseOutsideOMath = True
Set objRange = Selection.Range
objRange.Text = output
For Each AC In Application.OMathAutoCorrect.Entries
With objRange
If InStr(.Text, AC.Name) > 0 Then
.Text = Replace(.Text, AC.Name, AC.Value)
End If
End With
Next AC
Set objRange = Selection.OMaths.Add(objRange)
Set objEq = objRange.OMaths(1)
objEq.buildup
'Place the cursor at the end of the equation, outside of the OMaths object
objRange.OMaths(1).Range.Select
Selection.Collapse direction:=wdCollapseEnd
Selection.MoveRight Unit:=wdCharacter, count:=1
as_fraction = "" 'just a dummy return to make the function happy
Else 'just return a flat string value
as_fraction = output
End If
End Function
I shared an answer at this link : https://stackoverflow.com/a/57517128/11933717
It's also an iterative function, but unlike finding numerator and denominator in a nested loop, it just tests numerators only and so, should be faster.
Here is how it works :
It assumes that, based on the user input x, you want to find 2 integers n / m .
n/m = x , meaning that
n/x should give an almost integer m
Say one needs to find a fraction for x = 2.428571. Putting the int 2 aside for later, the algo starts by setting n and x and iterates n :
// n / x = m ( we need m to be an integer )
// n = 1 ; x = .428571 ;
1 / .428571 = 2.333335 (not close to an integer, n++)
2 / .428571 = 4.666671 (not close to an integer, n++)
3 / .428571 = 7.000007
At this point n = 3, we consider that m = 7.000007 is integer enough --based on some kind of accuracy the programmer decides-- and we reply the user
2.428571 = 2 + 3/7
= 14/7 + 3/7
= 17/7