VBA convert time on Number - excel

I need to convert my time to Number. i need it because letter i'm going to divide this dates by yourself to calculate % (agent productive time ).
I tried something like this
'cells(2,3) = 22:12:2
cells(2,3) / (60*60*1000)
Thanks in advance

You can do like this:
a = "78:19:41"
b = "74:23:58"
ta = (Split(a, ":")(0) / 24) + TimeValue("00:" & Split(a, ":", 2)(1))
tb = (Split(b, ":")(0) / 24) + TimeValue("00:" & Split(b, ":", 2)(1))
p = tb / ta * 100
p -> 94.9844138434859

Related

I want to create a vba userform in excel for my personal calculations. But I'm having trouble using it

Help me I need to create a Userform for my personal calculations. But I ran into a lot of problems. Because I've never written a program before.
When I enter a value 0 or enter a value other than 0 and delete it in the text field PriceCoinBuy, BuyCoin , PriceCoinSell , SellCoin , Vat one of the fields I will get Msg : Run-time error '6'; overflow.
But when I put a number other than 0 in the BuyCoin field, I get Msg : Run-time error '11'; Division by zero.
I've been searching for a solution for 2 days but can't find it in please help.
I have 5 textboxes for input.
Sub SumAll()
Dim A, B, C, D, E, F, G, H, I, J, K, V As Double
A = Val(Order.PriceCoinBuy.Value)
B = Val(Order.BuyCoin.Value)
C = Val(Order.PriceCoinSell.Value)
D = Val(Order.SellCoin.Value)
V = Val(Order.Vat.Value)
'-------------- Math --------------
E = CDbl(B) / A
F = CDbl(E) * (V / 100)
G = CDbl(E) - F
H = CDbl(G) * A
I = CDbl(D) * C
J = CDbl(I) * (V / 100)
K = CDbl(I) - J
'---------------- Show -------------
Order.GetCoin.Text = Format(E, "##,##0.000000")
Order.AfterVatBuy.Text = Format(F, "##,##0.0000")
Order.CoinBalance.Text = Format(G, "##,##0.000000")
Order.ToMoney.Text = Format(H, "##,##0.00")
Order.GetMoney.Text = Format(I, "##,##0.00")
Order.AfterVatSell.Text = Format(J, "##,##0.00")
Order.MoneyBalance.Text = Format(K, "##,##0.00")
End Sub

EXCEL - Convert TON to KG incorrect

I have Excel 365 on MAC.
I want to convert "weight unit" in another "weight unit".
=CONVERT(number; from_unit; to_unit)
Why is 1 ton = 907kg ? This should be 1000kg
Amount Unit Amount Unit
1 ton => 907,18474 kg
1 kg => 1000 g
1 g => 1000 mg
What is the correct way to convert?
Thanks,
Aykut
In Britannica encyclopedia defined ton:
ton, a unit of weight in the avoirdupois system equal to 2,000 pounds (907.18 kg) in the United States (the short ton) and 2,240 pounds (1,016.05 kg) in Britain (the long ton). The metric ton used in most other countries is 1,000 kg, equivalent to 2,204.6 pounds avoirdupois.
Microsoft Excel just support "ton" and "uk_ton"
Excel function did not work for unit conversion from tons (metric) to other weight units.
Excel out of the box function:
=Convert(value; from_unit; to_unit)
I would like to share my (workaround) solution with the same attributes as Excel:
=myConvert(value; from_unit; to_unit)
Public Function myConvert(value As Variant, f_unit As String, to_unit As String) As Variant
Dim conversion As String
Dim position As Integer
Dim unit_array As Variant
unit_array = Array( _
"mg_mg", "mg_g", "mg_kg", "mg_t", _
"g_mg", "g_g", "g_kg", "g_t", _
"kg_mg", "kg_g", "kg_kg", "kg_t", _
"t_mg", "t_g", "t_kg", "t_t" _
)
Dim formula_array As Variant
formula_array = Array( _
"", "/ 1000", "/ 1000000", "/ 1000000000", _
"* 1000", "", "/ 1000", "/ 1000000", _
"* 1000000", "* 1000", "", " / 1000", _
"* 1000000000", "* 1000000", "* 1000", "" _
)
conversion = f_unit & "_" & to_unit
position = array_pos(unit_array, conversion)
value = Replace(value, ",", ".")
If (IsNumeric(value) = False) Then
myConvert = "not a number"
Exit Function
ElseIf (position < 0) Then
myConvert = "unknown unit"
Exit Function
End If
myConvert = Evaluate(value & " " & formula_array(position))
End Function
Function array_pos(my_array, my_value)
array_pos = -1
For ii = LBound(my_array) To UBound(my_array)
If my_array(ii) = my_value Then 'value found
array_pos = ii
Exit For
End If
Next
End Function
Here is the test result:
1 t = 1 t
1 t = 1000 kg
1 t = 1000000 g
1 t = 1000000000 mg
1 kg = 0,001 t
1 g = 0,000001 t
1 mg = 0,000000001 t
1 t = unknown unit x because x as unit is unknown
y t = not a number kg because y is not a number

Rearrange equation to solve for a different variable

I am looking at VBA code (function) written by someone else.
Here is the code:
Function EuropeanDelta(StrikePrice, MarketPrice, Volatility, InterestRate As Double, PC As String, ValueDate, ExpiryDate As Date, Optional PriceOrYield As String = "P") As Double
Rem Declare our working variables
Dim r As Double
Dim d1 As Double
Dim d2 As Double
Dim t As Double
Dim SqT As Double
Rem End of variable declaration
If PriceOrYield = "Y" Then
MarketPrice = 100 - MarketPrice
StrikePrice = 100 - StrikePrice
If PC = "C" Then
PC = "P"
Else
PC = "C"
End If
End If
Rem Initiase our working variables
t = (ExpiryDate - ValueDate) / 365
SqT = Sqr(t)
r = Application.WorksheetFunction.Ln(1 + InterestRate)
d1 = (Application.WorksheetFunction.Ln(MarketPrice / StrikePrice) + (Volatility * Volatility * 0.5) * t) / (Volatility * SqT)
Rem Quick logic to deal with Calls or Puts
If PC = "C" Then
EuropeanDelta = Exp(-r * t) * Application.WorksheetFunction.NormSDist(d1)
Else
EuropeanDelta = -Exp(-r * t) * Application.WorksheetFunction.NormSDist(-d1)
End If
If PriceOrYield = "Y" Then
EuropeanDelta = EuropeanDelta * -1
End If
End Function
The whole problem is based around the line for "d1". I would like to re-organise to solve for "StrikePrice". I have tried writing it out mathematically and then re-arranging, then swapping back to VBA.
#duffymo is correct, but am giving the answer directly in terms of VBA code
' d1 = (Log(MarketPrice / StrikePrice) + (Volatility * Volatility * 0.5) * t) / (Volatility * Sqr(t))
'
' Volatility * Sqr(t) * d1 = Log(MarketPrice / StrikePrice) + Volatility^2 * t/2
'
' Log(MarketPrice / StrikePrice) = Volatility * Sqr(t) * d1 - Volatility^2 * t/2
'
' MarketPrice / StrikePrice = Exp(Volatility * Sqr(t) * d1 - Volatility^2 * t/2)
'
StrikePrice = MarketPrice / Exp(Volatility * Sqr(t) * d1 - Volatility^2 * t/2)
Other Notes :
For brevity replace Application.WorksheetFunction.Ln() with Log()
There is no need cache SqT = Sqr(t) since it is only used once.
For clarity replace Volatility*Volatility with Volatility^2 as internally it does the same thing.
This is just algebra - high school math.
Take it in steps. Make sure you do the same operation to both sides to make sure that equality still holds.
Here's your starting equation:
d = {ln(m/s) + v*v*t/2}/(v*sqrt(t))
Multiply both sides by the denominator of the RHS:
d*v*sqrt(t) = ln(m/s) + v*v*t/2
Subtract v*v*t/2 from both sides:
(d*v*sqrt(t) - v*v*t/2) = ln(m/s)
Apply the exponential function to both sides, noting that exp(ln(x)) = x:
exp(d*v*sqrt(t) - v*v*t/2) = m/s
Multiply both sides by s:
s*exp(d*v*sqrt(t) - v*v*t/2) = m
Divide both sides by exp(d*v*sqrt(t) - v*v*t/2) to get the desired result:
s = m/exp(d*v*sqrt(t) - v*v*t/2)
Let's see if this function makes sense.
At t = 0 the denominator exp(0) = 1, so the strike price is equal to the market price.
As t -> infinity, we hope that the denominator gets large so s -> zero. L'Hospital's Rule will help here.

Absolute Value of the Differences In Two (2) Ranges - PART II

Hopefully this is easier to read than yesterday. Trying to find a way to vary the number of periods "N" that measure "VOLATILITY" The code for the complete function as suggested yesterday is below and fixes "N" at 10. Function works just fine for KAMA with the default value for "N" (N0Addr and N1Addr are not needed in this default version of the KAMA function but are steps to get to a variable "N")
This formula works in Excel:
=SUMPRODUCT((ABS(I26:I36-I25:I35)))
I can also obtain the correct sum of differences in the two ranges but not the absolute value. This VBA Code does that with the named ranges "N0Addr" and "N1Addr":
Rng0 = WorksheetFunction.Sum(Range(N0Addr)) - WorksheetFunction.Sum(Range(N1Addr))
Function nTEST(Price, nPer, mPer, N)
'Variables
Fast = 2 / (nPer + 1)
Slow = 2 / (mPer + 1)
'One(1) Prior Period Calculation
nTEST1 = Application.Caller.Offset(-1)
N0Addr = Application.WorksheetFunction.Concat(Price.Offset(-N, 0).Address & ":" & Price.Address)
N1Addr = Application.WorksheetFunction.Concat(Price.Offset(-(N + 1), 0).Address & ":" & (Price.Offset(-1, 0).Address))
'Change Formula (Y - Yn)
E = Abs(Price - Price.Offset(-N, 0))
'Volatility Formula { =SUM(ABS(Y:Yn)-(Y1:Yn1))) }
'VOLATILITY (N = 10)
'1-10
R = Abs(Price - Price.Offset(-1, 0)) + Abs(Price.Offset(-1, 0) - Price.Offset(-2, 0)) _
+ Abs(Price.Offset(-2, 0) - Price.Offset(-3, 0)) _
+ Abs(Price.Offset(-3, 0) - Price.Offset(-4, 0)) + Abs(Price.Offset(-4, 0) - Price.Offset(-5, 0)) _
+ Abs(Price.Offset(-5, 0) - Price.Offset(-6, 0)) + Abs(Price.Offset(-6, 0) - Price.Offset(-7, 0)) _
+ Abs(Price.Offset(-7, 0) - Price.Offset(-8, 0)) + Abs(Price.Offset(-8, 0) - Price.Offset(-9, 0)) _
+ Abs(Price.Offset(-9, 0) - Price.Offset(-10, 0))
'EFFICIENCY RATIO
ER = E / R
Smooth = (ER * (Fast - Slow) + Slow) ^ 2
'Formula Calculation
nKAMA = Smooth * Price + (1 - Smooth) * nKAMA1
End Function
Looking for an VBA formula or method to input a working formula for "volatility" that can vary over N periods. I can get the sum of differences but not the sum of absolute differences.
Rng0 = WorksheetFunction.Sum(Range(N0Addr)) - WorksheetFunction.Sum(Range(N1Addr))
I can also enter one formula in Excel that accomplishes provides the sum of absolute differences.
=SUMPRODUCT((ABS(I26:I36-I25:I35)))

VBScript string replace with range instead of string?

Replace() already exists, but that function takes strings as parameters. I need range.
In my string there are two "strings" that are 10 characters long.
Greger with 6 chars and 4 spaces and the other string with 10 characters.
"Greger AASSDDFFGG"
I want to replace "Greger " with "googlioa "
What i'm looking for is basically this:
Replace(MyString,1,10) = "googlioa "
Is there any way to achieve this?
If they're always going to be 10 chars, just pad the names.
strNameFind = "Greger"
strNameReplace = "googlioa"
' Pad the names...
strNameFind = Left(strNameFind & Space(10), 10)
strNameReplace = Left(strNameReplace & Space(10), 10)
MyString = Replace(MyString, strNameFind, strNameReplace)
Alternatively, if you don't want to determine the existing name, just pad your new name appropriately and add the remainder of your string:
' Pad the new name to fit in a 10-char column...
strNameReplace = "googlioa"
strNameReplace = Left(strNameReplace & Space(10), 10)
' Update the record...
MyString = strNameReplace & Mid(MyString, 11)
If you want to replace strictly by position, use concatenation of Left(), new, and Mid(). To get you started:
>> Function replByPos(s, f, l, n)
>> replByPos = Left(s, f-1) & n & Mid(s, f + l - 1)
>> End Function
>> s = "Greger AASSDDFFGG"
>> r = replByPos(s, 1, 10, "googlioa ")
>> WScript.Echo s
>> WScript.Echo r
>>
Greger AASSDDFFGG
googlioa AASSDDFFGG
>>
Further enhancements:
safety: f(rom) - 1 is risky - should be checked
padding of new string wrt l(ength)
perhaps you want to search (Instr()) for old ("Greger ") before the concatenation
On second thought (and stealing Bond's padding):
Maybe I should have interpeted the 10 as a to/till/upto value instead of a length/width specification. So see whether
Option Explicit
Function replByPos(src, from, till, ns)
Dim w : w = till - from
replByPos = Left(src, from - 1) & Left(ns & Space(w), w) & Mid(src, till)
End Function
Dim s : s = "Greger AASSDDFFGG"
Dim ns : ns = "googlioa"
WScript.Echo s
WScript.Echo replByPos(s, 1, 10, ns)
s = "Whatever Greger AASSDDFFGG"
ns = "googlioa"
Dim p : p = Instr(s, "Greger")
WScript.Echo s
WScript.Echo replByPos(s, p, p + 10, ns)
output:
cscript 22811896.vbs
Greger AASSDDFFGG
googlioa AASSDDFFGG
Whatever Greger AASSDDFFGG
Whatever googlioa AASSDDFFGG
matches your specs better.

Resources