Functions not actualizing - vba

I execute a VBA code that takes a database, treats it and export it into a sheet. This is working fine. However, I have a sheet that produces graphs depending on the data in the particular sheet. The datas does not actualize. I have to enter the cell and click enter to actualize it. I'm pretty sure there is an easier way to do this. Calculation is set to automatic but that doesn't seem to change anything.
In my cell, I have my own vba function that needs to be updated once the report is done. When I click the cell and then enter, the result is updated but I would like this to be done automatically. I hope this is clearer !
Thanks in advance,
Etienne NOEL
HEre is the code of my function
Public Function number_of_appearances(term As String, sheet As String, column As Integer) As Integer
Application.Volatile
Dim number_of_rows As Integer
Dim appearances As Integer
Dim row As Integer
appearances = 0
row = 1
number_of_rows = Worksheets(sheet).UsedRange.Rows.Count
Do While row <= number_of_rows
If Worksheets(sheet).Cells(row, column).Value = term Then
appearances = appearances + 1
End If
row = row + 1
Loop
number_of_appearances = appearances
End Function
A cell example of a user of the function
=number_of_appearances('test';'sheet1'; 3)

Sounds like your UDF might not depend on any cells that change value when your DB is processed.
See This MSDN Link
Post your UDF (or just its header if you prefer) and an example of its use...
EDIT:
Yes, none of the parameters to the UDF are cell references, therefore the UDF is not triggered to recalculate when data on the shet changes.
You have two choices:
1. rewrite your UDF to include parameter(s) that reference cells that change value when the DB is processed
2. make your UDF volitile (include Application.Volatile in the UDF code) WARNING: this can be very inefficient, depending on how many time the UDF is used and how intensive its calculation is
EDIT 2:
Heres a refactor of your udf using the first option mentioned:
Public Function number_of_appearances(term As String, rng As Range) As Integer
Dim v As Variant
Dim i As Long, j As Long
Dim appearances As Long
v = Intersect(rng, rng.Worksheet.UsedRange)
For j = LBound(v, 2) To UBound(v, 2)
For i = LBound(v, 1) To UBound(v, 1)
If v(i, j) = term Then
appearances = appearances + 1
End If
Next i, j
number_of_appearances = appearances
End Function
use like
=number_of_appearances("test";Sheet1!C:C)
EDIT 3:
If all you are doing is counting number of occurances of a string in a range, consider using
=COUNTIF(Sheet1!C:C;"test")

Related

compare two sheets values in excel

How can I compare one column's all values of a sheet-1 to another column values with different sheet-2 and if match then returns the value corresponding one of the columns of sheet-1 to another column of sheet-2 in excel?
Let's assume your values are in columns A of sheets named Sheet1 and Sheet2. Then, you can place the following formula into B1 of Sheet2 and drag down enough to cover you desired range: =IF(Sheet1!A1=Sheet2!A1,Sheet2!A1,"")
or, if you'e rather use VBA, place this code into a module:
Sub columnCompare()
Dim sh1 As Worksheet, sh2 As Worksheet, r1 As Range, r2 As Range
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
Set r1 = sh1.Range("A1")
Set r2 = sh2.Range("A1")
While r1 <> "" And r2 <> ""
If r1 = r2 Then r2.Offset(0, 1) = r1
Set r1 = r1.Offset(1, 0)
Set r2 = r2.Offset(1, 0)
Wend
End Sub
If am understanding correctly, this is what you want,
Sheet1
Sheet2
Enter the below formula in B2 of sheet2 and drag down as in the image,
=INDEX(Sheet1!B:B,MATCH(A2,Sheet1!A:A,0),1)
I can only answer part of your question: comparing two columns and detecting that they differ.
You have an excellent tutorial answer for that in Tony M's answer, above.
However, this will perform very slowly on a large data set, because:
Reading a range one cell at a time is very slow;
Comparing values pair-by-pair is inefficient, especially for strings, when the number of values gets into the tens of thousands,
Point(1) is the important one: it takes the same amount of time for VBA to pick up a single cell using var = Range("A1") as it does to pick up the entire range in one go using var = Range("A1:Z1024"); and every interaction with the sheet takes four times as much time as a string comparison in VBA, and twenty times longer than an comparison between floating-point decimals; and that, in turn, is three times longer than an integer comparison.
So your code is probably four times faster, and possibly a hundred times faster, if you read the entire range in one go, and work on the Range.Value2 array in VBA.
That's in Office 2010 and 2013 (I tested them); for older version of Excel, you'll see quoted times between 1/50th and 1/500th of a second, for each VBA interaction with a cell or range of cells. That'll be *way** slower because, in both old and new versions of Excel, the VBA actions will still be in single-digit numbers of microseconds: your code will run at least a hundred times faster, and probably thousands of times faster, if you avoid cell-by-cell reads from the sheet in older versions of Excel.
So big gains are there to be made - an interval perceptible to the user - in picking up the ranges in a single 'hit' and then performing the comparison on each item of an array in VBA.
arr1 = Range1.Values
arr2 = Range2.Values
' Consider checking that the two ranges are the same size
For i = LBound(arr1, 1) To Ubound(arr1, 2)
For j = LBound(arr1, 2) To Ubound(arr1, 2)
If arr1(i, j) <> arr2(i, j) Then
bMatchFail = True
Exit For
End If
Next j
If bMatchFail Then Exit For
Next i
Erase arr1
Erase arr2
You'll notice that this code sample is generic, for two ranges of the same size taken from anywhere - even from separate workbooks. If you're comparing two adjacent columns, loading a single array of two columns and comparing IF arrX(i, 1) <> arrX(i,2) Then is going to halve the runtime.
Your next challenge is only relevant if you're picking up tens of thousands of values from large ranges: there's no performance gain in this extended answer for anything smaller than that.
What we're doing is:
Using a hash function to compare the values of two large ranges
The idea is very simple, although the underlying mathematics is quite challenging for non-mathematicians: rather than comparing one value at a time, we run a mathematical function that 'hashes' the values into a short identifier for easy comparison.
If you're comparing ranges against a 'reference' copy, you can store the 'reference' hash, and this halves the workload.
There are some fast and reliable hashing functions out there, and they are available in Windows as part of the security and cryptography API. There is a slight problem in that they run on strings, and we have an array to work on; but you can easily find a fast 'Join2D' function that gets a string from the 2D arrays returned by a range's .Value2 property.
So a fast comparison function for two large ranges will look like this:
Public Function RangeCompare(Range1 as Excel.Range, Range2 As Excel.Range) AS Boolean
' Returns TRUE if the ranges are identical.
' This function is case-sensitive.
' For ranges with fewer than ~1000 cells, cell-by-cell comparison is faster
' WARNING: This function will fail if your range contains error values.
RangeCompare = False
If Range1.Cells.Count &LT;&GT; Range2.Cells.Count Then
RangeCompare = False
ElseIf Range1.Cells.Count = 1 then
RangeCompare = Range1.Value2 = Range2.Value2
Else
RangeCompare = MD5(Join2D(Range1.Value2)) = MD5(Join2D(Range2.Value2))
Endif
End Function
I've wrapped the Windows System.Security MD5 hash in this VBA function:
Public Function MD5(arrBytes() As Byte) As String
' Return an MD5 hash for any string
' Author: Nigel Heffernan Excellerando.Blogspot.com
' Note the type pun: you can pass in a string, there's no type conversion or cast
' because a string is stored as a Byte array and VBA recognises this.
Dim oMD5 As Object 'Set a reference to mscorlib 4.0 to use early binding
Dim HashBytes() As Byte
Dim i As Integer
Set oMD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
HashBytes = oMD5.ComputeHash_2((arrBytes))
For i = LBound(HashBytes) To UBound(HashBytes)
MD5 = MD5 & Right("00" & Hex(HashBytes(i)), 2)
Next i
Set oMD5 = Nothing ' if you're doing this repeatedly, declare at module level and persist
Erase HashBytes
End Function
There are other VBA implementations out there, but nobody seems to know about the Byte Array / String type pun - they are not equivalent, they are identical - so everyone codes up unnecessary type conversions.
A fast and simple Join2D function was posted by Dick Kusleika on Daily Dose of Excel in 2015:
Public Function Join2D(ByVal vArray As Variant, Optional ByVal sWordDelim As String = " ", Optional ByVal sLineDelim As String = vbNewLine) As String
Dim i As Long, j As Long
Dim aReturn() As String
Dim aLine() As String
ReDim aReturn(LBound(vArray, 1) To UBound(vArray, 1))
ReDim aLine(LBound(vArray, 2) To UBound(vArray, 2))
For i = LBound(vArray, 1) To UBound(vArray, 1)
For j = LBound(vArray, 2) To UBound(vArray, 2)
'Put the current line into a 1d array
aLine(j) = vArray(i, j)
Next j
'Join the current line into a 1d array
aReturn(i) = Join(aLine, sWordDelim)
Next i
Join2D = Join(aReturn, sLineDelim)
End Function
If you need to excise blank rows before you make the comparison, you'll need the Join2D function I posted in StackOverflow back in 2012.
The most common application of this type of hash comparison is for spreadsheet control - change monitoring - and you'll see Range1.Formula used instead of Range1.Value2: but your question is about comparing values, not formulae.

Count Number of Rows based on Parameter. (Excel VBA)

I need to count the number of rows depending on the week and type of the data. I have the excel formula but I want to make it as a VB code yet I don't have that much idea and it is not working.
=IF(AND($N$4="All",$N$5="All"),SUM(('SD'!$I$2:$I$99538='Source'!$B6)*('SD'!$A$2:$A$99538='Source'!C$5)),IF(AND($N$4="All",$N$5<>"All"),SUM(('SD'!$I$2:$I$99538='Source'!$B6)*('SD'!$A$2:$A$99538='Source'!C$5)*('SD'!$B$2:$B$99538='Source'!$N$5)),IF(AND($N$4<>"All",$N$5="All"),SUM(('SD'!$I$2:$I$99538='Source'!$B6)*('SD'!$A$2:$A$99538='Source'!C$5)*('SD'!$K$2:$K$99538='Source'!$N$4)),IF(AND($N$4<>"All",$N$5<>"All"),SUM(('SD'!$I$2:$I$99538='Source'!$B6)*('SD'!$A$2:$A$99538='Source'!C$5)*('SD Raised'!$B$2:$B$99538='Source'!$N$5)*('SD'!$K$2:$K$1048576='Source'!$N$4))))))
I have a sheet where in all datas are captured (SD) and the second one will be the sheet(Source) where i need to count the number of rows available based on the parameter as follow; The week where data belongs and the category of the data.
Edit:
This formula* does not count the data i needed to count. And if possible I want to make it as a VBA code.
This is where the counted data should go. "Weeks are changing depending on the dropdown iput (Max of 4 weeks below from the selected week)"
This image shows the data where i need to capture and count the number of category based on the weeks and category. (Sample only)
I guess, if it's the right point you're hitting DoktorOSwaldo, better use the Range().Rows.Count property rather than scrolling through allRows.
Hope this helps.
Hadi
so i have to guess a bit what you want, but if you want to Count specific rows in Excel vba you can use something like this:
Dim allRows As Variant
Dim i As Long
Dim count as Long
count = 0
allRows = Tabelle5.Range("A" & start_row, end_column & last_row)
For i = 1 To UBound(allRows)
If allRows(i, 1) = *category* and allRows(i,2) = *week* Then
count = count + 1
End If
Next
To find right range, there are multiple possible solution. I use this, maybe it is not the best, but works fine for me:
Private Function last_row() As Integer
Dim rangeObj As Range
Set rangeObj = Tabelle5.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If rangeObj Is Nothing Then
last_row = start_row
Else
last_row = rangeObj.row
End If
End Function
Public Function start_row() As Integer
start_row = 2
End Function

Formula using Cells in the spreadsheet

I am looking for a way to set a variable equal to the number of non-empty cells in Column A using Excel VBA.
So pseudo code
Dim j As Integer
j = CountA(A:A)
This however does not work. Neither does j = "=CountA(A:A)"
Something like this will do the trick.
The VBA functions don't work exactly the same as in the spreadsheets themselves. You need to first select the range of the active worksheet and then call the counta function.
Dim j = Application.WorksheetFunction.counta(activeworksheet.range("A:A"))
Paste this into a module in Excel VBA.
Function CountNonEmptyCells(ColId As Integer) As Integer
Dim r As Range
Dim Count As Integer
Set r = Sheet1.Columns(ColId)
For Each cell In r.Cells
If cell.Value <> "" Then
Count = Count + 1
End If
Next
CountNonEmptyCells = Count
End Function
My end result:
You can also use Evaluate Function like this:
Dim j As Long
j = [CountA(A:A)] 'brackets are shortcut for Evaluate
or explicitly like this:
j = Evaluate("CountA(A:A)")
Essentially, you can either Evaluate the formula as it would appear on the worksheet or you can adapt the syntax to use as an adopted VBA command. Here are a couple variations of each. Note that I am explicitly including a reference to the parent worksheet. This is particularly important for the first two evaluate methods and desirable for all four variations in order that you are not counting column A from the wrong worksheet.
Dim j As Long
j = [COUNTA(Sheet1!A:A)]
Debug.Print j
j = Evaluate("COUNTA(Sheet1!A:A)")
Debug.Print j
j = Application.CountA(Sheets("Sheet1").Columns(1))
Debug.Print j
j = Application.CountA(Range("Sheet1!A:A"))
Debug.Print j
The first simply uses [ and ] as wrappers around the COUNTA formula as it would appear on the worksheet. This forces evaluation of the formula to a result. The second is another evaluation of the formula but using the .Evaluate command allows you the option to construct the formula as a string using concatenation, replacement and other text parsing methods. You can include an equals sign (e.g. =) as a prefix if that makes more sense to you, (e.g. j = [=COUNTA(Sheet1!A:A)]) but it is not necessary.
In the last two, VBA adopts the native worksheet COUNTA function by prefacing it with either Application.Worksheetfunction. or (as above) just Application.. The cell range also moves from worksheet cell notation to VBA style cell notation.

Macro to run through 3 conditions and provide value

This is my first time using VBA for Excel (I usually code Java and C++), and I was hoping to get some tips to start out.
I want to write a macro for a large data set that will proceed through the following list of conditions to provide a dollar result:
Collect unit size from column A (Possible values 0-8)
Determine whether single or family unit from Column B (Single- 1, Family- 0)
Collect utility code from Column C (code for type of product being assessed)
From this information, a new value will be placed in the row which determines utility costs by taking into account unit size, type of unit, and the product in question. I have thought about using nested Select Case or nested conditionals in a loop, but overall I am pretty lost.
It seems like a worksheet formula might do the trick, but it's hard to tell without knowing what the calculation is. Below is a user-defined function (UDF) that you would put in a standard module. You would call it from a cell like:
=computecosts(A2,B2,C2)
Obviously the code would change depending on how your data is laid out and what your calculation is.
Public Function ComputeCosts(rSize As Range, rFamily As Range, rCode As Range) As Double
Dim lSizeFactor As Long
Dim lFamilyFactor As Long
Dim dCodeFactor As Double
Dim rFound As Range
Const lFAMILY As Long = 0
'Size factor is a function of 0-8, namely adding 1
lSizeFactor = rSize.Value + 1
'Family factor is computed in code
If rFamily.Value = lFAMILY Then
lFamilyFactor = 3
Else
lFamilyFactor = 2
End If
'Code factor is looked up in a different sheet
Set rFound = Worksheets("Sheet2").Columns(1).Cells.Find(rCode.Value, , xlValues, xlWhole)
If Not rFound Is Nothing Then
dCodeFactor = rFound.Offset(0, 1).Value
End If
'do the math
ComputeCosts = lSizeFactor * lFamilyFactor * dCodeFactor
End Function
Thanks for the responses, they were helpful in understanding VBA for Excel. I just ended up putting possible values in a table and then using Match functions within an Index function to pick out the right value.

Excel VBA Length-1 In a Range

I recently got into Excel macro development after a long time of not having the need to.
I have one column with two-hundred rows where each row has a value. I wrote a loop to iterate to each row value, read the current value and then write the value back minus the last character.
Here is some actual (and pseudo) code of what I wrote.
Dim theRow as Long
Dim totRow as Long
Dim fooStr as String
theRow = 2 'we begin on the second row of the colummn
totRow = 201 'there are 200 values
For theRow = 2 to totRow
fooStr = WorkSheets(DestSheet).Cells(theRow,"A").Formula 'read the cell value
fooStr = Left(fooStr,Len(fooStr)-1 'subtract the last character from the value
Cells(theRow,1).Value = fooStr 'write the value back
Next theRow
After I did some reading I learned that it is best practice to read and write values using a Range. Is it possible to rewrite what I am doing using a Range so it willl go faster.
Here is what I came up with so far.
Range("A2:A201").Value = Len(Range.Left("A2:A201").Value)-1
However, this doesn't work.
Any clues on how to do this if this is indeed possible?
Thanks for any tips.
If you want maximum performance (you don't need it for 200 rows, but...) you have to minimize the number of reads and writes (mostly writes) to ranges. That means reading the whole range into an array, manipulating the array, then writing it back to the range. That's one read and one write compared to 200 in a loop. Here's an example.
Sub RemoveLastChar()
Dim vaValues As Variant
Dim i As Long
vaValues = Sheet1.Range("A2").Resize(200).Value
For i = LBound(vaValues, 1) To UBound(vaValues, 1)
vaValues(i, 1) = Left$(vaValues(i, 1), Len(vaValues(i, 1)) - 1)
Next i
Sheet1.Range("A2").Resize(UBound(vaValues, 1), UBound(vaValues, 2)).Value = vaValues
End Sub
You could do something like
Sub StringTrim()
Dim xCell as Range
Range("A1:A201").Select
For Each xCell in Selection
xCell.Value = Left(xCell.Value, Len(xCell.Value) - 1)
Next
End Sub
I don't know what kind of speed improvements you are seeking, but that would also do the job.
You might know this already but putting Application.ScreenUpdating = False at the top of your code can speed it up significantly (unless you like to watch everything flash by as the script works). You should reset the value to True at the end of your code.