Why does my script multiply values by 30.48? - scripting
My AutoIt script has users paste text into a field, then click a button to convert the customary units into metric units. It converts not only words, but the numbers preceding them as well.
The text below is the input:
1 foot 1 inch
2 feet 2 inches
3 feet 3 inches
1 inch
2 inches
3 in
1 foot
2 feet
3 ft
This is the expected output:
33.02 centimeters
66.04 centimeters
99.06 centimeters
2.54 centimeters
5.08 centimeters
7.62 cm
30.48 centimeters
60.96 centimeters
91.44 cm
But this is what I get:
1006.4496 centimeters
2012.8992 centimeters
3019.3488 centimeters
2.54 centimeters
5.08 centimeters
7.62 cm
30.48 centimeters
60.96 centimeters
91.44 centimeters
Seems first three lines are multiplied by 30.48. I do not want that. Here is the code:
Case $msg = $CTRL_i
;For unit measurements of feet and inches
$input_str = GUICtrlRead($Textbox)
$WordArray = StringRegExp($input_str, "[\s\.:;,]*([a-zA-Z0-9-_]*\.?[a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
;MsgBox($MB_OK, "Size:", UBound($WordArray))
For $i = 0 To (UBound($WordArray) - 1) Step 1
If (($i + 3) <= UBound($WordArray)) Then
If (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And ($WordArray[$i + 1] = "feet" Or "foot" Or "ft") And (StringIsDigit($WordArray[$i + 2]) Or StringIsFloat($WordArray[$i + 2])) And ($WordArray[$i + 3] = "inches" Or "inch" Or "in") Then
$cm_value = ($WordArray[$i] * 30.48) + ($WordArray[$i + 2] * 2.54)
$old_string = $WordArray[$i] & " " & $WordArray[$i + 1] & " " & $WordArray[$i + 2] & " " & $WordArray[$i + 3]
$new_string = $cm_value & " centimeters"
$input_str = StringReplace($input_str, $old_string, $new_string, 1)
MsgBox($MB_OK, "Feet and inches", $WordArray[$i])
EndIf
EndIf
Next
$WordArray = StringRegExp($input_str, "[\s\.:;,]*([a-zA-Z0-9-_]*\.?[a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
;MsgBox($MB_OK, "Size:", UBound($WordArray))
For $i = 0 To (UBound($WordArray) - 1) Step 1
If (($i + 1) <= UBound($WordArray)) Then
;This is for when a measurement of inches is represented as plural.
If (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And $WordArray[$i + 1] = "inches" Then
$cm_value = $WordArray[$i] * 2.54
$old_string = $WordArray[$i] & " " & $WordArray[$i + 1]
$new_string = $cm_value & " centimeters"
$input_str = StringReplace($input_str, $old_string, $new_string, 1)
MsgBox($MB_OK, "inches", $WordArray[$i])
;This is for when a measurement of inches is represented as singular.
ElseIf (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And $WordArray[$i + 1] = "inch" Then
$cm_value = $WordArray[$i] * 2.54
$old_string = $WordArray[$i] & " " & $WordArray[$i + 1]
$new_string = $cm_value & " centimeters"
$input_str = StringReplace($input_str, $old_string, $new_string, 1)
MsgBox($MB_OK, "inch", $WordArray[$i])
;This is for when a measurement of inches is represented as an abbreviation.
ElseIf (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And $WordArray[$i + 1] = "in" Then
$cm_value = $WordArray[$i] * 2.54
$old_string = $WordArray[$i] & " " & $WordArray[$i + 1]
$new_string = $cm_value & " cm"
$input_str = StringReplace($input_str, $old_string, $new_string, 1)
MsgBox($MB_OK, "in", $WordArray[$i])
EndIf
EndIf
Next
$WordArrayC = StringRegExp($input_str, "[\s\.:;,]*([a-zA-Z0-9-_]*\.?[a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
;MsgBox($MB_OK, "Size:", UBound($WordArray))
For $i = 0 To (UBound($WordArray) - 1) Step 1
If (($i + 1) <= UBound($WordArray)) Then
;This is for when a measurement of feet is represented as plural or singular.
If (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And ($WordArray[$i + 1] = "feet" Or "foot") Then
$cm_value = $WordArray[$i] * 30.48
$old_string = $WordArray[$i] & " " & $WordArray[$i + 1]
$new_string = $cm_value & " centimeters"
$input_str = StringReplace($input_str, $old_string, $new_string, 1)
MsgBox($MB_OK, "feet", $WordArray[$i])
;This is for when a measurement of feet is represented as an abbreviation.
ElseIf (StringIsDigit($WordArray[$i]) Or StringIsFloat($WordArray[$i])) And $WordArray[$i + 1] = "ft" Then
$cm_value = $WordArray[$i] * 30.48
$old_string = $WordArray[$i] & " " & $WordArrayC[$i + 1]
$new_string = $cm_value & " cm"
$input_str = StringReplace($input_str, $old_string, $new_string, 1)
MsgBox($MB_OK, "ft", $WordArray[$i])
EndIf
EndIf
Next
GUICtrlSetData($Textbox, $input_str)
This kind of syntax:
($WordArray[$i+1] = "feet" Or "foot" Or "ft")
is actually interpreted as
(($WordArray[$i+1] = "feet") Or ("foot") Or ("ft"))
which "foot" is always True and "ft" is always True as neither are empty strings.
Use instead this syntax:
($WordArray[$i+1] = "feet" Or $WordArray[$i+1] = "foot" Or $WordArray[$i+1] = "ft")
The later compares $WordArray[$i+1] with each string.
Change all instances of that kind of syntax and your code may work as expected.
this is a little bit error-prone, but it shows an easier way.
#include <Array.au3>
$str = "1 foot 1 inch" & #CR & _
"2 feet 2 inches" & #CR & _
"3 feet 3 inches" & #CR & _
"1 inch" & #CR & _
"2 inches" & #CR & _
"3 in" & #CR & _
"2 feet" & #CR & _
"1 foot" & #CR & _
"3 ft"
$rows_A = StringSplit($str, #CR, 2)
;~ _ArrayDisplay($rows_A, 'Rows')
;~ 33.02 centimeters
;~ 66.04 centimeters
;~ 99.06 centimeters
;~ 2.54 centimeters
;~ 5.08 centimeters
;~ 7.62 centimeters
;~ 30.48 centimeters
;~ 60.96 centimeters
;~ 91.44 centimeters
Local $sum = 0
For $i = 0 To UBound($rows_A) - 1
If StringRegExp($rows_A[$i], '(\d.*?\d.*?)', 0) Then
$splitted_row_A = StringRegExp($rows_A[$i], '(\d)\s*(\w+)', 3)
;~ _ArrayDisplay($splitted_row_A)
$sum = 0
For $y = 0 To UBound($splitted_row_A) - 2
If StringInStr($splitted_row_A[$y + 1], 'in') Then
$sum += $splitted_row_A[$y] * 2.54
ElseIf StringInStr($splitted_row_A[$y + 1], 'f') Then
$sum += $splitted_row_A[$y] * 30.48
EndIf
Next
If $sum > 0 Then ConsoleWrite($rows_A[$i] & ' => ' & $sum & #CRLF)
Else
If StringInStr($rows_A[$i], 'in') Then ConsoleWrite($rows_A[$i] & ' = ' & $rows_A[$i] * 2.54 & #CRLF)
If StringInStr($rows_A[$i], 'f') Then ConsoleWrite($rows_A[$i] & ' = ' & $rows_A[$i] * 30.48 & #CRLF)
EndIf
Next
Related
triangular pyramid, trigonal pyramid offset-3d-Plane-Intersection By vbasolver
triangular pyramid, trigonal pyramid offset-3d-Plane-Intersection By vbasolver x=0.250 y=0.250 z=0.250 Inscribed sphere Center each plane offset 0.25 I challenge vba solver why ALL-Row 0.250 0.250 0.250 I want ALL-Row 0.250 0.250 0.250 why 6row 8row error The result is strange Please tell me how to fix it. Const x0 = 0 Const y0 = 0 Const z0 = 2 Const x1 = 0 Const y1 = 0 Const z1 = 0 Const x2 = 1 Const y2 = 0 Const z2 = 0 Const x3 = 0 Const y3 = 1 Const z3 = 0 Const r0 = 0.25 Const r1 = 0.25 Const r2 = 0.25 Const r3 = 0.25 Function myR1C1toA1(i, j) myR1C1toA1 = Application.ConvertFormula("R" & i & "C" & j, xlR1C1, xlA1) End Function Function myPlane(Ax, Ay, Az, Bx, By, Bz, Cx, Cy, Cz) a = (By - Ay) * (Cz - Az) - (Cy - Ay) * (Bz - Az) b = (Bz - Az) * (Cx - Ax) - (Cz - Az) * (Bx - Ax) c = (Bx - Ax) * (Cy - Ay) - (Cx - Ax) * (By - Ay) d = -(a * Ax + b * Ay + c * Az) myPlane = Array(a, b, c, d) End Function Function myOffset3PlaneIntersection(irow, x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3, r1, r2, r3) Dim v1 As Variant Dim v2 As Variant Dim v3 As Variant ' v1 = myPlane(x0, y0, z0, x2, y2, z2, x3, y3, z3) v2 = myPlane(x0, y0, z0, x3, y3, z3, x1, y1, z1) v3 = myPlane(x0, y0, z0, x1, y1, z1, x2, y2, z2) ' my1 = "(Abs(" & v1(0) & " * " & myR1C1toA1(irow, 1) & "+ " & v1(1) & " * " & myR1C1toA1(irow, 2) & "+ " & v1(2) & " * " & myR1C1toA1(irow, 3) & "+ " & v1(3) & ") / Sqrt(" & v1(0) & " ^ 2 + " & v1(1) & " ^ 2 +" & v1(2) & " ^ 2) - " & myR1C1toA1(irow, 1) & ")" my2 = "(Abs(" & v2(0) & " * " & myR1C1toA1(irow, 1) & "+ " & v2(1) & " * " & myR1C1toA1(irow, 2) & "+ " & v2(2) & " * " & myR1C1toA1(irow, 3) & "+ " & v2(3) & ") / Sqrt(" & v2(0) & " ^ 2 + " & v2(1) & " ^ 2 +" & v2(2) & " ^ 2) - " & myR1C1toA1(irow, 2) & ")" my3 = "(Abs(" & v3(0) & " * " & myR1C1toA1(irow, 1) & "+ " & v3(1) & " * " & myR1C1toA1(irow, 2) & "+ " & v3(2) & " * " & myR1C1toA1(irow, 3) & "+ " & v3(3) & ") / Sqrt(" & v3(0) & " ^ 2 + " & v3(1) & " ^ 2 +" & v3(2) & " ^ 2) - " & myR1C1toA1(irow, 3) & ")" Range(myR1C1toA1(irow, 5)).Formula = "=" & my1 & "^ 2 +" & my2 & "^ 2 +" & my3 & "^ 2" ' Dim ws As Worksheet: Set ws = ActiveSheet SolverReset SolverOk setCell:=ws.Range(myR1C1toA1(irow, 5)), _ MaxMinVal:=3, _ ByChange:=ws.Range(myR1C1toA1(irow, 1) & ":" & myR1C1toA1(irow, 3)), _ EngineDesc:="GRG Nonlinear" SolverSolve UserFinish:=True End Function Sub myFormat() Columns("A:E").Select Selection.NumberFormatLocal = "0.000_ " Range("A5").Select End Sub Sub aaa_Main() Dim myXYZ As Variant ActiveSheet.Cells.Clear MsgBox "Solver Start" Cells(1, 1) = x0 Cells(1, 2) = y0 Cells(1, 3) = z0 Cells(2, 1) = x1 Cells(2, 2) = y1 Cells(2, 3) = z1 Cells(3, 1) = x2 Cells(3, 2) = y2 Cells(3, 3) = z2 Cells(4, 1) = x3 Cells(4, 2) = y3 Cells(4, 3) = z3 '----------------------------------------------------------------------------------------------------- irow = 5 Cells(irow + 0, 4) = r0 Cells(irow + 1, 4) = r1 Cells(irow + 2, 4) = r2 Cells(irow + 3, 4) = r3 myXYZ = myOffset3PlaneIntersection(irow + 0, x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3, r1, r2, r3) myXYZ = myOffset3PlaneIntersection(irow + 1, x1, y1, z1, x2, y2, z2, x3, y3, z3, x0, y0, z0, r2, r3, r0) myXYZ = myOffset3PlaneIntersection(irow + 2, x2, y2, z2, x3, y3, z3, x0, y0, z0, x1, y1, z1, r3, r0, r1) myXYZ = myOffset3PlaneIntersection(irow + 3, x3, y3, z3, x0, y0, z0, x1, y1, z1, x2, y2, z2, r0, r1, r2) myFormat End Sub '1 0.000 0.000 2.000 '2 0.000 0.000 0.000 '3 1.000 0.000 0.000 '4 0.000 1.000 0.000 '5 0.250 0.250 0.250 0.250 0.000 '6 0.000 0.000 0.000 0.250 0.000 ???? I want 0.250 0.250 0.250 '7 0.250 0.250 0.250 0.250 0.000 '8 0.102 0.339 0.102 0.250 0.000 ???? I want 0.250 0.250 0.250 (2021-11-15)I try sympy from sympy import * def myVol(PTO,PTA,PTB,PTC): return Matrix([[PTA.x-PTO.x, PTA.y-PTO.y, PTA.z-PTO.z], [PTB.x-PTO.x, PTB.y-PTO.y, PTB.z-PTO.z], [PTC.x-PTO.x, PTC.y-PTO.y, PTC.z-PTO.z]]).det()/6 def myUnitVector(myPoint3D): myL=myPoint3D.distance((0, 0)) return Point3D(myPoint3D.x/myL,myPoint3D.y/myL,myPoint3D.z/myL) def myHtoP(myHairetu): return Point3D(myHairetu[0],myHairetu[1],myHairetu[2]) def my3PLaneIntersection(PTO,PTA,PTB,PTC,RA,RB,RC): vA =myUnitVector(myHtoP(Plane(PTO, PTB, PTC).normal_vector)) PLA=Plane(PTO + RA * vA, normal_vector=vA) vB =myUnitVector(myHtoP(Plane(PTO, PTC, PTA).normal_vector)) PLB=Plane(PTO + RB * vB, normal_vector=vB) vC =myUnitVector(myHtoP(Plane(PTO, PTA, PTB).normal_vector)) PLC=Plane(PTO + RC * vC, normal_vector=vC) return PLC.intersection(PLB.intersection(PLA)[0]) PTO,PTA,PTB,PTC=Point3D(0,0,0),Point3D(1,0,0),Point3D(0,1,0),Point3D(0,0,2) print("#",myVol(PTO,PTA,PTB,PTC)) myRO,myRA,myRB,myRC=0,0,0,0 print("#",myVol( my3PLaneIntersection(PTO,PTA,PTB,PTC,myRO,myRO,myRO)[0], my3PLaneIntersection(PTA,PTC,PTB,PTO,myRA,myRA,myRA)[0], my3PLaneIntersection(PTB,PTC,PTO,PTA,myRB,myRB,myRB)[0], my3PLaneIntersection(PTC,PTB,PTA,PTO,myRC,myRC,myRC)[0])) myRO,myRA,myRB,myRC=1/4,1/4,1/4,1/4 print("#",myVol( my3PLaneIntersection(PTO,PTA,PTB,PTC,myRO,myRO,myRO)[0], my3PLaneIntersection(PTA,PTC,PTB,PTO,myRA,myRA,myRA)[0], my3PLaneIntersection(PTB,PTC,PTO,PTA,myRB,myRB,myRB)[0], my3PLaneIntersection(PTC,PTB,PTA,PTO,myRC,myRC,myRC)[0])) myRO,myRA,myRB,myRC=-1/4,-1/4,-1/4,-1/4 print("#",myVol( my3PLaneIntersection(PTO,PTA,PTB,PTC,myRO,myRO,myRO)[0], my3PLaneIntersection(PTA,PTC,PTB,PTO,myRA,myRA,myRA)[0], my3PLaneIntersection(PTB,PTC,PTO,PTA,myRB,myRB,myRB)[0], my3PLaneIntersection(PTC,PTB,PTA,PTO,myRC,myRC,myRC)[0])) # 1/3 # 1/3 # 0 # 8/3
I check from sympy import * var('m') def myUnitVector(myPoint3D): myL=myPoint3D.distance((0, 0)) return Point3D(myPoint3D.x/myL,myPoint3D.y/myL,myPoint3D.z/myL) def myHtoP(myHairetu): return Point3D(myHairetu[0],myHairetu[1],myHairetu[2]) def my3PLaneIntersection(PTO,PTA,PTB,PTC,RA,RB,RC): vA =myUnitVector(myHtoP(Plane(PTO, PTB, PTC).normal_vector)) PLA=Plane(PTO + RA * vA, normal_vector=vA) vB =myUnitVector(myHtoP(Plane(PTO, PTC, PTA).normal_vector)) PLB=Plane(PTO + RB * vB, normal_vector=vB) vC =myUnitVector(myHtoP(Plane(PTO, PTA, PTB).normal_vector)) PLC=Plane(PTO + RC * vC, normal_vector=vC) return PLC.intersection(PLB.intersection(PLA)[0]) def myProjection(PTO,PTA,PTC,PTOO): v = myUnitVector(myHtoP(Plane(PTO, PTA, PTC).normal_vector)) return solve(PTOO - Plane(PTO, PTA, PTC).projection(PTOO) - m * v, m)[m] * (-1) PTO,PTA,PTB,PTC=Point3D(0,0,0),Point3D(1,0,0),Point3D(0,1,0),Point3D(0,0,2) myRO,myRA,myRB,myRC=-1/4,-1/4,-1/4,-1/4 PTOO= my3PLaneIntersection(PTO, PTA,PTB,PTC,myRO,myRO,myRO)[0] PTAA= my3PLaneIntersection(PTA, PTC,PTB,PTO,myRA,myRA,myRA)[0] PTBB= my3PLaneIntersection(PTB, PTC,PTO,PTA,myRB,myRB,myRB)[0] PTCC= my3PLaneIntersection(PTC, PTB,PTA,PTO,myRC,myRC,myRC)[0] print("#",PTOO,"\n#",PTAA,"\n#",PTBB,"\n#",PTCC,) print("#",myProjection(PTO,PTB,PTA,PTOO), myProjection(PTO,PTA,PTC,PTOO), myProjection(PTO,PTC,PTB,PTOO)) print("#",myProjection(PTA,PTO,PTB,PTAA), myProjection(PTA,PTC,PTO,PTAA), myProjection(PTA,PTB,PTC,PTAA)) print("#",myProjection(PTB,PTA,PTO,PTBB), myProjection(PTB,PTO,PTC,PTBB), myProjection(PTB,PTC,PTA,PTBB)) print("#",myProjection(PTC,PTO,PTA,PTCC), myProjection(PTC,PTB,PTO,PTCC), myProjection(PTC,PTA,PTB,PTCC)) # Point3D(-1/4, -1/4, -1/4) # Point3D(7/4, -1/4, -1/4) # Point3D(-1/4, 7/4, -1/4) # Point3D(-1/4, -1/4, 15/4) # -1/4 -1/4 -1/4 # -1/4 -1/4 -1/4 # -1/4 -1/4 -1/4 # -1/4 -1/4 -1/4
Time Remaining in Process
I've looked at other posts about this, but I must be missing something. The time remaining isn't right and I'm a bit stuck on this. TIA mTimeSpan = Now - mStartTime If (mThisRec > 10) Then txt_StatusBox.Text = txt_StatusBox.Text & "Time remaining about " Dim mRate As Double = (CDec(mThisRec) / CDec(mTimeSpan.TotalSeconds)) * (CDec(mMaxRecs) - CDec(mThisRec)) / 1000 mTimeSpan = Now.AddSeconds(mRate) - Now If mTimeSpan.Hours > 0 Then If mTimeSpan.Hours > 0 Then txt_StatusBox.Text = txt_StatusBox.Text & mTimeSpan.Hours.ToString & " hours, " End If If mTimeSpan.Minutes > 0 Then txt_StatusBox.Text = txt_StatusBox.Text & mTimeSpan.Minutes.ToString & " minutes." Else txt_StatusBox.Text = txt_StatusBox.Text & mTimeSpan.Seconds.ToString & " seconds." End If End If
Error 1004 Macro Excel
I'm currently having some issues with VBA and the error 1004. I checked on the Internet but, unfortunately, couldn't find the solution. I'm working on data transfer between Excel and an other base. I'm currently on the first part of the work : the shape of the new document before the transfer. In fact, I can have text with more than 250 characters on my first database while the other one wants me to split all of the documents each 250 characters (as you can check on the program) and add a number associated to help to gather the information. It works very well until I reach the line Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("B" & ligneNC), 250 * (j - 1) + 1, 250) with j = 2, LigneLib = 3899 and a text with 257 characters. This code already worked for texts with more than 500 characters that is why I don't understand the issue. Moreover, When I delete all of the lines to start the macro again, I still have the error on the same line on the first loop. However, it works again only when I restart Excel. Please find below the details of the macro : Option Explicit Sub Libelle() Dim ligneLib As Integer Dim ligneNC As Integer Dim endLoop As Integer Dim i As Integer Dim j As Integer ligneNC = 3 ligneLib = 1 For i = 1 To 3003 endLoop = Round_Up(Len(Sheets("NCXL").Range("B" & ligneNC)) / 250) For j = 1 To endLoop 'Texte description Sheets("LibImport").Range("A" & ligneLib) = "100" Sheets("LibImport").Range("B" & ligneLib) = Sheets("NCXL").Range("A" & ligneNC) & "-DESC" Sheets("LibImport").Range("C" & ligneLib) = "NONCONFO" Sheets("LibImport").Range("D" & ligneLib) = j Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("B" & ligneNC), 250 * (j - 1) + 1, 250) ligneLib = ligneLib + 1 Next endLoop = Round_Up(Len(Sheets("NCXL").Range("C" & ligneNC)) / 250) For j = 1 To endLoop 'Texte cause Sheets("LibImport").Range("A" & ligneLib) = "100" Sheets("LibImport").Range("B" & ligneLib) = Sheets("NCXL").Range("A" & ligneNC) & "-CAUSE" Sheets("LibImport").Range("C" & ligneLib) = "NONCONFO" Sheets("LibImport").Range("D" & ligneLib) = j Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("C" & ligneNC), 250 * (j - 1) + 1, 250) ligneLib = ligneLib + 1 Next endLoop = Round_Up(Len(Sheets("NCXL").Range("E" & ligneNC)) / 250) For j = 1 To endLoop 'Texte action corrective Sheets("LibImport").Range("A" & ligneLib) = "100" Sheets("LibImport").Range("B" & ligneLib) = Sheets("NCXL").Range("A" & ligneNC) & "-DSCCOR" Sheets("LibImport").Range("C" & ligneLib) = "NONCONFO" Sheets("LibImport").Range("D" & ligneLib) = j Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("E" & ligneNC), 250 * (j - 1) + 1, 250) ligneLib = ligneLib + 1 Next endLoop = Round_Up(Len(Sheets("NCXL").Range("D" & ligneNC)) / 250) For j = 1 To endLoop 'Texte action curative Sheets("LibImport").Range("A" & ligneLib) = "100" Sheets("LibImport").Range("B" & ligneLib) = Sheets("NCXL").Range("A" & ligneNC) & "-DECIS" Sheets("LibImport").Range("C" & ligneLib) = "NONCONFO" Sheets("LibImport").Range("D" & ligneLib) = j Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("D" & ligneNC), 250 * (j - 1) + 1, 250) ligneLib = ligneLib + 1 Next ligneNC = ligneNC + 1 Next End Sub Function Round_Up(ByVal val As Double) As Integer Dim result As Integer result = Round(val) If result >= val Then Round_Up = result Else Round_Up = result + 1 End If End Function Thanks, Cédric.
The 251st character of your example text is an = symbol. When you write that to a cell, Excel assumes you're entering a formula. To work around this issue, precede the text with a ' as you would if you were keying it manually: Sheets("LibImport").Range("F" & ligneLib) = "'" & Mid(Sheets("NCXL").Range("B" & ligneNC), 250 * (j - 1) + 1, 250) They won't be visible in Excel, but you might need to account for them if you're doing other things later with the contents of that cell.
Run Time Error '1004', have researched and tried several recommended solutions to no avail
I am trying to fill a range with a formula and continue to get a runtime error '1004'. The error occurs at the line I have starred Sheets("Forecast").Range("H125").Formula = formulaTest. The code in my Sub is as follows: Sub FirmShareFill() Dim RampUp As Range Dim RampBas As Range Dim RampDn As Range Dim Numbering As Range Dim Approval As Range Dim PeakShare As Range Dim tcount As Byte Dim bcount As Byte Dim ubdcount As Byte Dim yearRange2 As Byte year = Worksheets("Inputs").Range("B6").Value cntry = Worksheets("Inputs").Range("B5").Value bnd = Worksheets("Inputs").Range("B3").Value typ = Worksheets("Inputs").Range("B2").Value cat = Worksheets("Inputs").Range("B4").Value tcount = bnd * cat + bnd ubdcount = tcount * 2 + 1 yearCount = year * 4 - 1 For ubd = 1 To 3 For t = 1 To typ For b = 1 To bnd For c = 1 To cat For i = 1 To cntry Set RampUp = Columns(7).Find(What:="Ramp_Up" & i, MatchCase:=True).Offset(0, 1) Set RampBas = Columns(7).Find(What:="Ramp_Bas" & i, MatchCase:=True).Offset(0, 1) Set RampDn = Columns(7).Find(What:="Ramp_Dn" & i, MatchCase:=True).Offset(0, 1) Set Numbering = Sheets("Inputs").Range("B13") Set Approval = Columns(6).Find(What:="Approval", MatchCase:=True).Offset(i, 2 + ubd) bcount = c + (cat + 1) * (b - 1) If t = 1 And b = 1 And ubd = 1 Then Set PeakShare = Columns(5).Find(What:="Peak Share", MatchCase:=True).Offset(4 + i, c) ElseIf t = 1 And b > 1 And ubd = 1 Then Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, bcount) ElseIf t > 1 And b = 1 And ubd = 1 Then Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, c + tcount) ElseIf t > 1 And b > 1 And ubd = 1 Then Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, tcount + bcount) ElseIf t = 1 And b = 1 And ubd = 2 Then Set PeakShare = Columns(5).Find(What:="Peak Share", MatchCase:=True).Offset(4 + i, c + ubdcount) ElseIf t = 1 And b > 1 And ubd = 1 Then Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, bcount + ubdcount) ElseIf t > 1 And b = 1 And ubd = 1 Then Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, c + tcount + ubdcount) ElseIf t > 1 And b > 1 And ubd = 1 Then Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, tcount + bcount + ubdcount) ElseIf t = 1 And b = 1 And ubd = 3 Then Set PeakShare = Columns(5).Find(What:="Peak Share", MatchCase:=True).Offset(4 + i, c + 2 * ubdcount) ElseIf t = 1 And b > 1 And ubd = 3 Then Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, bcount + 2 * ubdcount) ElseIf t > 1 And b = 1 And ubd = 3 Then Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, c + tcount + 2 * ubdcount) ElseIf t > 1 And b > 1 And ubd = 3 Then Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, tcount + bcount + 2 * ubdcount) End If Dim formulaTest As String formulaTest = "=IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ","", " & PeakShare.Address & " * " & RampUp.Address & ")" If ubd = 1 Then **Sheets("Forecast").Range("H125").Formula = formulaTest** ActiveCell.Offset(1, 0).Select ElseIf ubd = 2 Then Range(ActiveCell, ActiveCell.Offset(0, yearRange2)).Formula = "=IF(" & Numbering.Address(False, False) & " < " & Approval.Offset(1, 0).Address & ","", " & PeakShare.Address & " * " & RampBas.Address & "" ElseIf ubd = 3 Then Range(ActiveCell, ActiveCell.Offset(0, yearRange2)).Formula = "=IF(" & Numbering.Address(False, False) & " < " & Approval.Offset(1, 0).Address & ","", " & PeakShare.Address & " * " & RampDn.Address & "" End If Next i ActiveCell.Offset(1, 0).Select Next c Next b Next t Next ubd End Sub I believe the error may have something to do with how I declared the range "numbering" range, but as of yet I have been unable to figure it out. I have used this code on the same sheet many times, the only difference being that I have set a range, numbering, on a different sheet.
This should work: formulaTest = "=IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ",""""," & PeakShare.Address & "*" & RampUp.Address & ")" As #Comintern pointed out, you need to use """" to include double empty speech marks in your formula. I also removed the spaces either side of the *
change IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ","", " & PeakShare.Address & " * " & RampUp.Address & ")" to IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ","""", " & PeakShare.Address & " * " & RampUp.Address & ")"
Instead of counting how many " you have, you can use Chr(34) inside " to have a Formula check for ". In your case, use: "=IF(" & Numbering.Address(False, False) & "<" & Approval.Address & "," & Chr(34) & ", " & PeakShare.Address & " * " & RampUp.Address & ")"
Loop within loops vba
I'm trying to create a spreadsheet that could execute a BDH bloomberg code for each equity and within each equity id want it to go through all of the criteria such as "PX_Last", VOL_90D, PX_BID and etc. Essentially, the ideal output I'd like would be: Eg: 0665 HK Equity -> PX_Last, PX_BID, PX_ASK, VOL_90D, then, 1098 HK Equity -> PX_Last, PX_BID, PX_ASK, VOL_90D, then, etc. Here is a code that I've recently created for my spreadsheet. Would appreciate any insights about my code! For x = 1 To i If x = 1 Then equity = Worksheets(1).Cells(x + 2, 2) Worksheets(2).Cells(2, 2).Value = equity For y = 1 To j If y = 1 Then reutersticker = Worksheets(1).Cells(y + 2, 1) Worksheets(2).Cells(1, 2).Value = reutersticker For Z = 1 To k If Z = 1 Then LastPrice = Worksheets(1).Cells(Z + 1, 8) Worksheets(2).Cells(3, 1).FormulaR1C1 = "=BDH(""" & equity & """," & LastPrice & ",""" & StartDate & """,""" & EndDate & """," & Weekdays & "," & Fill & ")" ElseIf Z > 1 Then LastPrice = Worksheets(1).Cells(Z + 1, 8) Worksheets(2).Cells(3, 1).Offset(, (2 * Z) - 2).FormulaR1C1 = "=BDH(""" & equity & """," & LastPrice & ",""" & StartDate & """,""" & EndDate & """," & Weekdays & "," & Fill & ")" End If Next Z ElseIf y > 1 Then reutersticker = Worksheets(1).Cells(y + 2, 1) Worksheets(2).Cells(1, 2).Offset(, (4 * y) - 2).Value = reutersticker End If Next y ElseIf x > 1 Then equity = Worksheets(1).Cells(x + 2, 2) Worksheets(2).Cells(2, 2).Offset(, (8 * x) - 2).Value = equity End If Next x End Sub