Excel VBA Percentile using Application Function with multiple criteria - vba

I have the below formula working fine over large ranges of data in Excel 2007.
In all cases Range1, Range2 and ArrayRange are the same start and end rows.
Working:
=PERCENTILE(IF(((Range1=Value)*(Range2=Value2)),ArrayRange),0.9)
When I'm updating the formula within a macro, I can't seem to figure out the correct way to formulate the above using VBA.
Can anyone assist with the below?
Not Working:
90thPercentile = Application.Percentile(((Range1 = Value) * (Range2 = Value2), ArrayRange), 0.9)
90thPercentile = Application.Percentile(If(Range1 = Value,IF(Range2 = Value2, ArrayRange))), 0.9)
many thanks
Nik

You can't use arrays like that in VBA. You'd have to use Evaluate and pass the formula string:
90thPercentile = activesheet.evaluate("PERCENTILE(IF(((Range1=Value)*(Range2=Value2)),ArrayRange),0.9)"

Related

Write multiple cells at once in Excel VBA

In Excel VBA I am using some code to update the cells. Like:
for i = 1 to 1000
for j = 1 to 1000
cells(j,i)=<whatever_different_in_each_cell>
next
next
Is there a way to update all cells at once, instead of updating each cell individually ? Reason is that updating one cell in Excel is time consuming and when there are many of them, then it quickly becomes endless.
Range(<...>).Value = Array (value1, value2, <...>, valueN)
The number of values must be exactly the same as the number of cells in the Range, otherwise, the range will be filled with #VALUE!s.
range(cells(1,1),cells(1000,1000))="whatever"
Try this:
Sub WriteMultipleCells()
x = "whatever"
ThisWorkbook.Worksheets("Sheet1").Range("A1:X1") = x
End Sub

How to apply bitwise formula to a range using VBA

I am looking for a way to apply a bitwise AND on a value in excel and apply it on range. Here is a picture to illustrate :
I want to apply a bitwise AND on the Column "Status" with a fixed value, lets say (1001b).
If Status & 1001b = 1001b, then report the "Value" field in "Output". Else, put 0.
Both Excel and VBA can be used, but as the table can be huge, the solution may not use loops (Too slow, time efficient solution would be great). I am using excel 2010, and therefore the excel BITAND function is not available for me (only available since Excel 2013 version)
I search on SO and internet and didn't find a correct approach to a similar problem.
I try to optimize my initial solution which use recursivity, to be more time efficient, here is what I have done :
For cptDonnee_s32 = 0 To JeuDonnee_rs.RecordCount - 2 ' Loop through all my data
If (Value And 8) = 8) And (Value And 1) = 1) Then ' Compare bitfields one by one
Output = Value ' Replace output by value here
Else
Output = 0 ' put 0 in output there
End If
next
You could use a simple UDF:
Function BitAnd(dIn As Double, lMatch As Long) As Boolean
BitAnd = (dIn And lMatch) = lMatch
End Function
and enter:
=if(BitAnd(K2,9),L2,0)
and copy down.

How to get the number of the first row of a range in OpenOffice Calc (BASIC)

So I have been converting a VBA code to OpenOffice BASIC and a simple task as getting the number of the first row of the range has been consuming hours of research.
in VBA:
Cells(3,2) or "mr" is a cell with an input of the kind "A5:G7", so first I splitted the string in a array with A5 and G7.
Rangesplit = Split(Cells(3, 2), ":")
FirstRow = Range(Rangesplit(0)).Row
So far in OpenOfficeBasic:
Rangesplit(mr,":")
I have not been able so far to determine the row and I want to know which function allows me to do it.
The following functions will probably help you:
Row_int = oCell.getCellAddress.Row
Col_int = oCell.getCell

Easiest way to link multiple cells to a combobox selection

I have a combo box with 100 item numbers. I want my user to be able to select a item number, and have multiple cells from a table input into cells on a different worksheet. I could create a massive if/then statement but that would be exhausting. I was hoping someone knew of a more elegant solution.
For example, I could write a nested if/then statement like this:
If ItemNum.Value = "1001" Then
Sheets(10).Range("A2").Value = Sheets(11).Range("F2").Value
Sheets(10).Range("A3").Value = Sheets(11).Range("F3").Value
Sheets(10).Range("A4").Value = Sheets(11).Range("F4").Value
Sheets(10).Range("A5").Value = Sheets(11).Range("F5").Value
elseif ItemNum.Value = "1002" Then
Sheets(10).Range("B2").Value = Sheets(11).Range("G2").Value
Sheets(10).Range("B3").Value = Sheets(11).Range("G3").Value
Sheets(10).Range("B4").Value = Sheets(11).Range("G4").Value
Sheets(10).Range("B5").Value = Sheets(11).Range("G5").Value
Etc. 100 times
You don't need VBA. Use Excel formulas: look into MATCH/INDEX, in paticular.

Find and count the number of occurrences in a sheet

I need to find out how many times a sequence of numbers occurs in a sheet using VBA. For example:
201-1-55-8799
301-5-55-8799
202-1-55-8799
201-1-55-8799
999-5-55-8799
001-2-55-8799
I want to find out how many times 201-1 occur in this sheet. When you do FindAll in Excel it tells you at the bottom how many cells found.
I have experimented with CountIf but that only works if the cell just contains exactly 201-1.
The answer to the above search should be 2 instances of 201-1 found.
Then I need to write the number of occurances in a differnt sheet.
Thanks
Use the COUNTIF formula
=COUNTIF(A2:A12,"201-1*")
You could do this, using VBA
Dim tab_input as Variant
tab_input = ~your range~
specific_counter = 0
For i = 1 to Ubound(tab_input)
If Left(tab_input(i,1),5) = "201-1" Then
specific_counter = specific_counter + 1
End If
Next
msgbox specific_counter
That will count how many cells that have left text starting with 201-1 and and show in a box the amount.
I would recommend using Excel's own find function programmatically. Something like this:
http://www.ozgrid.com/VBA/find-method.htm