Round off 4.54545454545455 to an integer [closed] - vba

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a to sum the values which are adjacent to "Leave" valued cell.These values are like 4.54545454545455 and on.
Since "leave" comes intermittently I use condition and accordingly store values from adjacent cell in redefined Integer array(Redim based on the number of occurrences of "Leave". Now I am summing that array here lies the problem as sum is coming up something like 13.409091E-02 and only ULong(which does not work in my VBA editor) can store values like this as much I researched and know. So, Datatypes like Long, Double are not able to store such a value.Which is giving a sum not asked. Is there any way this could work or Is there any other way such that I can check intermittent adjacent values to"Leave"and sum them.
Dim leavearray() As Long <br/>dim xsum as long<br/>dim counter3 as Integer<br/>dim colum as Integer<br/>dim ro as Integer<br/>dim ctr as Integer<br/>dim change as Integer
For counter3 = colum To ro<br/>
If Cells(counter3, 29) = "Leave" Then<br/>
ctr = ctr + 1<br/>
ReDim leavearray(ctr - 1)<br/>
Else<br/>
End If<br/>
Next counter3<br/><br/>For counter = colum To ro<br/>
If Cells(counter, 29) = "Leave" Then<br/>
change = change + 1<br/>
leavearray(change - 1) = Cells(counter, 30).Value 'stores value like 3.409091E-02 in array as and when "leave" occurs in its adjacent cell.<br/>
Else<br/>
End If<br/>
If counter = ro Then<br/>
xsum = WorksheetFunction.Sum(leavearray)'It Stores 0 where the problem lies.

If you have a number and wants only the integer part of it, you should use the command Int(), if you want it to be rounded, use CInt(), as shown in the code below:
Dim integerResult As Integer
Dim singleVariable As Single
singleVariable = 4.65
integerResult = Int(singleVariable)
MsgBox (integerResult)
integerResult = CInt(singleVariable)
MsgBox (integerResult)
The first MessageBox will show you 4, and the second MessageBox will show you 5.

Related

How to sum variables in VBA to check input data is correct?

May seem like a stupid question, but how do you sum variables in VBA to check input data is correct?
I'm trying to check the user has inputted data correctly into a UserForm before they continue to the next page. To do this I want to sum some of their input variables. If they don't input it correctly, they get a message box telling them to revise the numbers. My code is:
Dim BinQnt As Double
Dim FillQnt As Double
Dim FineQnt As Double
Dim CoarQnt As Double
Dim RAPQnt As Double
Dim CRQnt As Double
If BinQnt + FillQnt + FineQnt + CoarQnt + RAPQnt + CRQnt = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
Else
MsgBox "Error, please check mixture design sums to 100%."
End If
When I'm testing it, it always goes to the error message box and I'm not sure why. Very sure I am adding variables which sum to 100 (haha). I first tried it without defining the variables, and now I have it still doesn't work.
Am I missing something obvious?
Do you want to round it or not?
In your question, you have the variables as Doubles, and in the answer as integers. Having them as integers will make it easier to hit 100%, but it will round to the nearest integer (2,4 = 2 | 2,6 = 3)
I will assume you are using all texboxes in the form, and as such use this code that is universal:
Dim tot As Double
tot = 0
For Each c In Me.Controls
If TypeName(c) = "TextBox" And IsNumeric(c.Value) Then
tot = tot + c.Value
End If
Next c
If tot = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
Else
MsgBox "Error, please check mixture design sums to 100%. Currently at " & tot & "%."
End If
This would give us something like this:
However, using Integer or Long, gives us this:
Also, the reason as to why I'm using IsNumeric(c.Value), is to stop the code from failing in case of empty boxes (or filled with invalid entries).
You could also place a check here in case no boxes are allowed to be empty.

VBA Count maximum occurance of a value in a row [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to write a macro that searches a specified column and counts all the cells that contain a specified string, such as character "p" and character "q" then associate this in another column i.e the total column, indicating the character which has occurred maximum number of times in the corresponding row
Have attached a sample screen shot of the same.
Does anyone have any ideas?
Thank you in advance.
Based on your additional criteria of having to exclude certain columns in the row I think it may indeed be easier to use VBA and create a user defined function that you can then enter into the cells in your spreadsheet in the same way as any other function.
I've shown my attempt below which basically checks the column of each cell in the range to ensure it has a header of "Symbol" and if so adds the value of that cell to an Array (after being converted to a number value). There is then another function that gets the mode from that array (this only works on numeric values which is why it was converted in the previous step). Finally that is converted back to a letter.
It's quite a roundabout way and there may be an easier approach but hopefully this will work for now and give you some idea's of how to create these kind of functions for yourself.
Create a new module in your VBA project and copy all 4 of the below procedures into it:
Option Explicit
Public Function MostFrequentValue(RNG As Range) As String
Dim HeaderRow As Integer
Dim a As Range
Dim arr As Variant
HeaderRow = 1 'Change this to whatever row your headers are in
For Each a In RNG.Cells
If Cells(HeaderRow, a.Column) = "Symbol" Then
If IsEmpty(arr) Then
arr = Array(ConvertLetterToNumber(a.Value))
Else
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = ConvertLetterToNumber(a.Value)
End If
End If
Next
MostFrequentValue = ConvertNumberToLetter(ArrayMode(arr))
End Function
.
Function ConvertNumberToLetter(ByVal strSource As Integer) As String
ConvertNumberToLetter = LCase(Chr(strSource + 64))
End Function
.
Function ConvertLetterToNumber(ByVal strSource As String) As Integer
Dim i As Integer
Dim strResult As String
strSource = UCase(strSource)
For i = 1 To Len(strSource)
Select Case Asc(Mid(strSource, i, 1))
Case 65 To 90:
strResult = strResult & Asc(Mid(strSource, i, 1)) - 64
Case Else
strResult = strResult & Mid(strSource, i, 1)
End Select
Next
ConvertLetterToNumber = strResult
End Function
.
Function ArrayMode(Ray As Variant) As Integer
With Application
ArrayMode = .Mode(Ray)
End With
End Function
You would then enter the function into a cell like so =MostFrequentValue("A2:C2")
P.S. This assumes that the symbol in each value in the Symbol column is a lowercase letter of the alphabet (a-z). This appears to be the case from your example
You don't need a macro. The below formula will give you what you need. The range being counted appears in the formula 3 times. You would need to adjust this for the range you want to check
=INDEX(A1:C1,MODE(MATCH(A1:C1,A1:C1,0)))
Note: this will return an error if no single character appears more times than any other. In this case you could wrap the above formula in an IFERROR function to return whatever value you would want to see when this happens.
If you have any blank cells in the row, you can use the following array formula, which adds an IF statement to test for empty cells:
=INDEX(A1:C1,MODE(IF(A1:C1<>"",MATCH(A1:C1,A1:C1,0))))
When entering this formula you will need to press Ctrl + Shift + Enter

VBA to return nth row number from a filtered table in excel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can anyone please help me with finding the absolute row number of nth element after filter is applied in an excel table.
For example, I have filter on and have a visible range of data element. Now 20th (nth) row in this filtered range could be 60th row (absolute sense) when no filters are on. Is there a way to find the absolute row number using VBA?
Simplest method is special cells. See below:
Sub test()
Dim i As Integer, j As Integer, k As Integer
Dim Report As Worksheet
Set Report = Excel.ActiveSheet ' Store the current worksheet in a variable (always a good idea)
Dim visRng As Range ' Creating a range variable to store our table, excluding any rows that are filtered out.
Set visRng = Report.UsedRange.SpecialCells(xlCellTypeVisible) ' Select only rows within the used range that are visible.
Dim r As Range
For Each r In visRng.Rows ' Loop through each row in our visible range ...
MsgBox (r.Row) ' ... and retrieve the "absolute" row number.
Next
End Sub
EDIT
Tom claims this method will not work, but I'm pretty sure it does what you ask. Example:
Here is my original test table--unfiltered so you can see what we're doing.
And then we filter a couple values somewhere in the middle of the table...
Now when we run the script I posted above, our message box will show the "absolute" row number for each unfiltered row. Results are 1,3,4,5, and 7.
As a function I suggest
Function RowNum(Target As Range) As Long
RowNum = Target.Row
End Function
use by entering in a cell =RowNum(E9). If you want the line relative to the table start and your table starts in -say- row 21, just subtract this from the result (you can use the same function to find the row of table start or course) ... e.g. =rownum(A2)-rownum($A$1) ... mind the absolute notation of table header!
If you don't like this as a function, you could use the SelectionChange event to display the row number of the selected cell in the message line (optionally depending on a "debug flag" somewhere in your sheet).
Tne non-VBA approach would be to use the CELL formula ... e.g. =CELL("row",A1)
The answer brought up by #Lopsided wont work, if there are other hidden cells after the nth entry. They would then be added to the absolute position.
This'll work, you have to change n by yourself in the script. Changing that shouldn't be to hard. If you have any questions regarding that, feel free to ask. (;
Sub absoluteRowID()
Dim RowCount, hiddenRows As Integer
'relative position n
n = 5
i = 0
Do While i < n
i = i + 1
If ThisWorkbook.Sheets(1).Rows(i).EntireRow.Hidden Then
'if there is a hidden row, position is incremented
n = n + 1
End If
'if there is no hidden row, nothing happens
Loop
MsgBox (i)
End Sub
HTH

How to find the standard deviation of alternative cells in excel 2007 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to calculate the standard deviation of alternative cells in a 10000 rows in excel 2007, how can i do that ? whether i need a formula or a VBA ?. Can some help me.
It's also possible with a formula. This "array formula" gives you standard deviation of every other cell starting at A1
=STDEV(IF(MOD(ROW(A1:A10000)-ROW(A1),2)=0,A1:A10000))
confirm with CTRL+SHIFT+ENTER
I would use both, the following code splits/sorts your rows and selects every second row and pastes it into another column, beginning with the first cell:
Sub splitcells()
Dim rows As Long
Dim i, j As Long
j = 0
rows = ?1?
For i = ?2? To rows Step 2
Cells(i - j, ?4?).Value = Cells(i, ?3?).Value
j = j + 1
Next
End Sub
Then you have to replace:
?1? - with the end row number of your list
?2? - with the beginning row number of your list
?3? - the column number of your list to be sorted
?4? - the column number of your destination column (the one the sorted data is sent to)
Once you have completed this, just run a normal standard deviation function on your new column and you'll have your answer.

EXCEL: Automating spreadsheet data input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am currently doing some data entry for a spreadsheet which contains hundreds on entries and want to automate the process, I have a good idea of what I want it to do but have little experience with Excel or VBA.
The idea behind it is that I have a code in one column and in the next column there is another code which is unique to the value in the former column. To give an example:
So for every cell that contains 123, the column next to it will be "ABC".
The sort of solution I would like is a macro that will work its way down Column A, storing the value of each cell (or something of that effect) and then working its way down to check for values that match that stored one. If a match is found, the macro will then copy the code from column B, the cell that is next to the stored cell and copy it into the cell in column B, next to the match.
EXAMPLE:
It will store the "123" value in A, work its way down Column A to find other cells matching "123" and when it finds them copy "ABC" into the column B cells next to the matches.
Hope this is easy to understand and someone can help me with coming up with a solution, would make this whole process alot easier as the spreadsheet is growing by the day and manual input is taking far to much time
Give this macro a try:
Sub FillInTheBlanks()
Dim rA As Range
Dim rB As Range
Dim r As Range, rr As Range
Dim N As Long
Dim va As Variant
N = Cells(Rows.Count, "A").End(xlUp).Row
Set rA = Range("A1:A" & N)
Set rB = rA.Offset(0, 1).Cells.SpecialCells(xlCellTypeBlanks)
If rB Is Nothing Then Exit Sub
For Each r In rB
va = r.Offset(0, -1).Value
For Each rr In rA
If rr.Value = va And rr.Offset(0, 1) <> "" Then
r.Value = rr.Offset(0, 1).Value
End If
Next rr
Next r
End Sub