Dim PtDnr As Single
Dim TxDnr As Single
GrsDnr AsDim Single
Dim AmntDnr As Single
Dim Jour As Integer
Dim I As Integer
Jour = 7
PtDnr = 2256.03 'trhough 2 loops, the amount is correct
PtDnr = 0
For I = 35 To 40
PtDnr = PtDnr + Sheets(1).Cells(Jour, I)
Next
For I = 47 To 54
PtDnr = PtDnr + Sheets(1).Cells(Jour, I)
Next
TxDnr = Sheets(1).Cells(Jour, 32) ' This Cell has a Value of 167.11 "
NtDnr = Sheets(1).Cells(Jour, 33) ' This Cell Has a value of 2088.92
GrsDnr = TxDnr + NtDnr ' Give me 2256.03, this amount is correct
AmntDnr = GrsDnr - PtDnr ' Give me 2.441406E.04 wich is wrong
I checked the Cells one by one and make them with 12 decimals, after the first 2, all are at "0" ex: 167.110000000000
What I do wrong, I pass all night and tried several possibility but cannot figure out.
Thank you for your help
Jean
Try using data type Double instead of Single.
Although I don't understand how anyone is expected to know what is going wrong if we can not see the values that are getting fed into these calculations.
Related
I have data. Can be in list or just in rows and colums or anything really.
I have data entries like 10, 1010, 11, 111,2,201,3210, 300100 etc 6 numbers is max.
I would like to have data be sorted
10
1010
11
111
2
201
300100
3210
And not like
2
10
11
111
201
1010
3210
300100
Any neat way do to this. Can make it work with filters, so guess I need some code and cant figure it out.
Can make it work with filters if i add "." or something between every number. Then just add filter ?.?.?.?.?.? and sort accending. Havent even tried any code.
No VBA, but formulae; you could try:
Formula in B1:
=SORTBY(A1:A8,LEFT(A1:A8&"00000",6))
If you have the values in VBA, you can sort them alphabetically by forcing them to be strings and then using the < or > operator. Variant values and numbers will need to be converted to strings with CStr or an equivalent operation. You could also pass the values into a String variable, which will force it to become a String value.
Sub Example()
Const VALUES As String = "2 10 11 111 201 1010 3210 300100"
'Value Type is explicitly forced to be String
Dim ValueArr() As String
ValueArr = Split(VALUES, " ")
'Bubble Sort - Loop until no further changes are neccesary
Dim IsFinished As Boolean
While Not IsFinished
IsFinished = True
Dim i As Long
For i = LBound(ValueArr) To UBound(ValueArr) - 1
' The > operator is comparing strings alphabetically because the two values are String
If ValueArr(i) > ValueArr(i + 1) Then
'Swap
Dim tmp As String
tmp = ValueArr(i)
ValueArr(i) = ValueArr(i + 1)
ValueArr(i + 1) = tmp
IsFinished = False
End If
Next
Wend
Debug.Print Join(ValueArr, " ")
'Outputs "10 1010 11 111 2 201 300100 3210"
End Sub
Sort Delimited Cells
=LET(Data,A2:C14,delSplit,",",delJoin," ",
MAP(Data,LAMBDA(cell,
IFERROR(TEXTJOIN(delJoin,1,SORT(TRIM(TEXTSPLIT(cell,delSplit,,1)),,,1)),""))))
To create your own LAMBDA function, e.g. SortDelimited, in the Name Manager you could use the following...
=LAMBDA(Data,delSplit,delJoin,
MAP(Data,LAMBDA(cell,
IFERROR(TEXTJOIN(delJoin,1,SORT(TRIM(TEXTSPLIT(cell,delSplit)),,,1)),""))))
when you would use it anywhere in the workbook with e.g. the simple...
=SortDelimited(A2:C14,","," ")
Hi everyone I hope you're doing fine.
This is my 1st request here in the forum, as I didn't have the chance to find what I'm looking for, I've been searching for this for months but didn't get lucky, and I couldn't figure out how to code it since I'm a beginner.
So basically I have these multiple Excel files that contain columns (long,latitude,altitude, containers,spill, and volume of non contained garbage)
The problem is in the container range, it contains the number of container(num) and the type of container(char) and the volume of the container ( num) separated by "/" character and if we have more than a container in one place we put + sign then number of the second container, type, volume, separated also by "/"
What I'm really looking to do is to sum the number of container and multiply it by the sum of volumes for each cell and then add it to the spill if there is any and then output it in a total column
If there is one type of container then there is no need to do a sum , only multiplication and then add the spill value then output it to the total column
And I'm counting to use "/" sign and + sign as a wildcards to extract the numeric values if this possible
For the volume column there is no math required, only copy paste to the total column.
I apologize for the bad English, you can leave a comment in case it's hard to understand, and I'm looking forward to your help.
Example:
Another example:
You could use a User Defined Function with the code located in a module.
Option Explicit
Function CalcVolume(s As String) As Variant
Dim ar1 As Variant, ar2 As Variant
Dim i As Integer, num As Double, vol As Double, bError As Boolean
bError = False
' split into array
ar1 = Split(s, "+")
For i = LBound(ar1) To UBound(ar1)
ar2 = Split(ar1(i), "/")
If UBound(ar2) = 3 And IsNumeric(ar2(0)) And IsNumeric(ar2(3)) Then
num = num + ar2(0)
vol = vol + ar2(3)
Else
bError = True
End If
Next
If bError = False Then
CalcVolume = vol * num
Else
CalcVolume = CVErr(xlErrNA)
End If
End Function
Sub test()
Debug.Print CalcVolume("2/FUT/p/50+1/FUT/m/100")
End Sub
Would anyone know the Excel formula, VBA, or SPSS syntax to do the following:
Create a new variable/column in a dataset or spreadsheet which is populated by the column number (or column title) of a randomly selected column (from a range of 1-42 columns), provided the value in that column for a given row does not contain 99.
In Excel I can do the first step and create random numbers and match these to columns, but I don't know how (or if possible) to 're-roll' a new random number if the initial matched column contains the value 99.
My formula for generating a random number between 1 and 42 to identify a column:
AQ=RANDBETWEEN(1,3)
For a row in Excel using 9-row dummy data: =HLOOKUP(AQ,$A$1:$AP$9,2,FALSE)
Here's an example of how you can re-roll... for the given row, I chose 10 but you can change this however you need
EDIT - now looping thru givenRow:
Sub test()
Dim randCol As Integer
Dim givenRow As Long
Dim saveCol As Integer: saveCol = 44 ' where to store results
With ThisWorkbook.Worksheets("your sheet name")
For givenRow = 1 To 100
Do While True
' get column between 1 and 42
randCol = Int(42 * Rnd + 1)
' if not 99 exit
If .Cells(givenRow, randCol).Value <> 99 Then Exit Do
Loop
' store results in saveCol for givenRow
.Cells(givenRow, saveCol).Value = randCol
Next
End With
End Sub
Heres how you could go about it in SPSS using Python:
begin program.
import spss, spssaux
import random
# get variable list
vars = spssaux.VariableDict().expand(spss.GetVariableName(0) + " to " + spss.GetVariableName(spss.GetVariableCount()-1))
proceed = True
breakcount = 0
while proceed:
# generate random integer between 0 and variable count -1, get random variable's
# name and index-position in dataset
rng = random. randint(0,spss.GetVariableCount() - 1)
ranvar = spss.GetVariableName(rng)
ind = int(vars.index(ranvar))
# read data from random variable, if value 99 is stored in the variable, go back to the top. if not, compute variable
# random_column = column number (index +1 NOT index)
randat = spss.Cursor([ind])
d = randat.fetchall()
randat.close()
data = [str(x).strip('(),') for x in d]
breakcount += 1
if "99.0" not in data:
spss.Submit("compute random_column = %s." %(ind + 1))
proceed = False
elif breakcount == 42:
break
end program.
it iterates through random variables until it finds one without the value 99 in it, then computes the new variable containing the comlumn number.
Edit: Added a break condition so that it doesnt loop infinitely just in case every variable contains a 99
Good day everyone,
I am trying to find a smart solution of extracting 8 digits from a cell (unique ID). The problem here occurs that it might look like this:
112, 65478411, sale
746, id65478411, sale 12.50
999, 65478411
999, id65478411
Thats most of the cases, and probably all mentioned, so I basically need to find the 8 digits in the cell and extract them into different cell. Does anyone have any ideas? I though of eliminating the first characted, then check if the cell is starting with the id, eliminate it further but I understood that this is not the smart way..
Thank you for the insights.
Try this formula:
=--TEXT(LOOKUP(10^8,MID(SUBSTITUTE(A1," ","x"),ROW(INDIRECT("1:"&LEN(A1)-7)),8)+0),"00000000")
This will return the 8 digit number in the string.
To return just the text then:
=TEXT(LOOKUP(10^8,MID(SUBSTITUTE(A1," ","x"),ROW(INDIRECT("1:"&LEN(A1)-7)),8)+0),"00000000")
You can also write a UDF to accomplish this task, example below
Public Function GetMy8Digits(cell As Range)
Dim s As String
Dim i As Integer
Dim answer
Dim counter As Integer
'get cell value
s = cell.Value
'set the counter
counter = 0
'loop through the entire string
For i = 1 To Len(s)
'check to see if the character is a numeric one
If IsNumeric(Mid(s, i, 1)) = True Then
'add it to the answer
answer = answer + Mid(s, i, 1)
counter = counter + 1
'check to see if we have reached 8 digits
If counter = 8 Then
GetMy8Digits = answer
Exit Function
End If
Else
'was not numeric so reset counter and answer
counter = 0
answer = ""
End If
Next i
End Function
Here is an alternative:
=RIGHT(TRIM(MID(SUBSTITUTE(A1,",",REPT(" ",LEN(A1))),LEN(A4),LEN(A1))),8)
Replace all commas with spaces repeated the length of the string,
Then take the mid point starting from the length of the original string for the length of the string (ie second word in new string)
Trim out the spaces
take the right 8 chars to trim out any extra chars (like id)
Wondering if anyone could help me. I'm stumped. It's been ages since I used excel....
I have 9 columns with different values in each cell, different numbers of cells per column.
I need a formula/macro to spit out all combinations of the cells and yet still remain in the exact same order of the columns.
For example
Columns:
D / 003 / 23 / 3 / 3R / C / VFX
... / 005 / 48 / 3 / 12 / .. / VDF
... / 007 / ... / 1 / ... /... / HSF
And it spits out like this:
D0032333RCVFX
D0032333RCVDF
D0032333RCHSF
D0034833RCVFX
D0034833RCVDF
and so on....
and so on.....
Presumably you will want to call this function with a "serial number" - so that you can call "the Nth combination". The problem then breaks into two parts:
Part 1: figure out, for a given "serial number", which element of each column you need. If you had the same number of elements E in each column it would be simple: it's like writing N in base E. When the number of elements in each column is different, it's a little bit trickier - something like this:
Option Base 1
Option Explicit
Function combinationNo(r As Range, serialNumber As Integer)
' find the number of entries in each column in range r
' and pick the Nth combination - where serialNumber = 0
' gives the top row
' assumes not all columns are same length
' but are filled starting with the first row
Dim ePerRow()
Dim columnIndex As Integer
Dim totalElements As Integer
Dim i, col
Dim tempString As String
ReDim ePerRow(r.Columns.Count)
totalElements = 1
i = 0
For Each col In r.Columns
i = i + 1
ePerRow(i) = Application.WorksheetFunction.CountA(col)
totalElements = totalElements * ePerRow(i)
Next
If serialNumber >= totalElements Then
combinationNo = "Serial number too large"
Exit Function
End If
tempString = ""
For i = 1 To UBound(ePerRow)
totalElements = totalElements / ePerRow(i)
columnIndex = Int(serialNumber / totalElements)
tempString = tempString & r.Cells(columnIndex + 1, i).Value
serialNumber = serialNumber - columnIndex * totalElements
Next i
combinationNo = tempString
End Function
You call this function with the range where your columns are, and a serial number (starting at 0 for "top row only"). It assumes that any blank space is at the bottom of each column. Otherwise, it will return a string that is the concatenation of combinations of values in each column, just as you described.
EDIT perhaps the following picture, which shows how this is used and what it actually does, helps. Note that the first reference (to the table of columns of different length) is an absolute reference (using the $ sign, so when you copy it from one cell to another, it keeps referring to the same range) while the second parameter is relative (so it points to 0, 1, 2, 3 etc in turn).