I want to return the row number where column A has val1 and column B has val2.
Currently, for testing purposes, I have an array formula in a cell, and it works fine.
A2 ={MATCH(1,(Data!A:A="val1")*(Data!B:B="val2"),0)}
I now want to use that formula in VBA with the result (the row number) being in a variable.
val1 and val2 are 2 string variables I would pass to the formula.
I cant seem to find how to do that.
Application.evaluate does the job:
Public Sub arrfunc()
val1 = 21
val2 = 12
a = Application.Evaluate("MATCH(1,(A:A=" & val1 & ")*(B:B=" & val2 & "),0)")
End Sub
Related
I have a dynamic column of strings and want to perform both a transpose and sum. An example of the column and the ultimate goal is below:
**VARIABLE COLUMN TITLE**
VPPTest1
VPPTest2
VPPTest3
VPPTest4
VPPTest5
VPPTest6
I am trying to get a formula (or series of formula) to create the following. Could also be done with VBA if that's easiest.
VPPTotal = VPPTest1 + VPPTest2 + VPPTest3 + VPPTest4 + VPPTest5 + VPPTest6
where everything is a string and output the summation formula into a cell.
NOTE The output is a STRING not an integer of number value. The actual output will be concatenation of strings that make the VPPTotal above.
Any help appreciated.
Thanks!
Looks like a basic loop in vba,
This snippet sums all the values between "A2" and the last non-empty cell in column "A", and appends the sum to the end of column "A"
Dim row As Integer
Dim col as String
Dim total as Integer
col = "A"
row = 2
While Range(col & row) <> ""
total = total + Range(col & row)
row = row + 1
Wend
Range(col & row) = total
I hope it's helpful
I have a requirement where I need to write a formula dynamically using VBA.
I am able to find the Column Name and Row Number. Now how do I apply it in a formula ?
Colm = WorksheetFunction.Match("Name", Sheets("Sheet1").Rows(5), 0)
BuName = GetColumnName(Colm)
Row = WorksheetFunction.Match("Name", Sheets("Sheet1").Columns(Colm), 0)
The BuName returns a String which is "A". The Row Returns the number which is 2.
Now I tried to Apply it in a formula using VBA like this,
Worksheets("Sheet1").Range("AQ1").Formula = "=$'BuName'$Row&$A$10"
Does anyone Know how to write the formula for that?
This works for me:
Dim Colm, Row As Long
Dim BuName As String
With Worksheets("Sheet1")
Colm = WorksheetFunction.Match("Name", .Rows(5), 0)
BuName = GetColumnName(Colm)
Row = WorksheetFunction.Match("Name", .Columns(Colm), 0)
.Range("AQ1").Formula = "=$" & BuName & "$" & Row & "&$A$10"
End With
I am trying to find a way to look for "Unknown" value across multiple columns. When it is present, I would like to return all of the header columns that the value appears in into one cell for each row in column B.
For example for row 2 (the first row below my header row), I want it to return every column name that "Unknown" appears from Column F to Column Y for row 2 only and put the column name in B2. I want to repeat that process for all of my 9064 rows.
I am using Excel 2010. I looked up Match Index but was not able to find a way to do what I wanted to do. Is there a way to look for a value across multiple columns and return every column header that values appears in for that row and put all of the column headers into one cell?
Data View
Here's a custom function. First place the following code in a regular module (Alt+F11 >> Insert >> Module >> Copy/paste >> Alt+Q)...
Function AConcat(a As Variant, Optional Sep As String = "") As String
' Harlan Grove, Mar 2002
Dim Y As Variant
If TypeOf a Is Range Then
For Each Y In a.Cells
AConcat = AConcat & Y.Value & Sep
Next Y
ElseIf IsArray(a) Then
For Each Y In a
AConcat = AConcat & Y & Sep
Next Y
Else
AConcat = AConcat & a & Sep
End If
AConcat = Left(AConcat, Len(AConcat) - Len(Sep))
End Function
Then enter the following formula in AA2, confirm with CONTROL+SHIFT+ENTER, and copy down:
=SUBSTITUTE(AConcat(IF(LEFT(F2:Y2,3)="Unk",", "&$F$1:$Y$1,"")),", ","",1)
I have a value in Column A, which I wnat to compare with multiple values of corresponding cell column B and put the answer in column C. Say it searching in column B for values which are less than or equal to 12 and put the answer in same order in column C.
Column A #### Column B #### Column C
12 #### 10,12,13,14 #### Yes, Yes, No, No
101 #### 101,102,103,104 #### Yes, No, No, No
Assuming, as you described in your problem, that the cells in Column B and C are strings, separated by commas, then the easiest way is to do this using a user-defined function (called StringComp) in VBA.
The function takes two arguments, one for the list of comma separated numbers in Column B, and the second points to the cell with the number you wish to use as a comparison to your list, Column A.
I think this can be done using array functions, but the VBA solution is much simpler.
After first copying and pasting the below VBA into your workbook VBA Module, for your example above, assuming your data starts from Row 1, you would use the StringComp function by putting =StringComp(B1,A1) in cell C1, and this will return Yes,Yes,No,No, as per your example.
Public Function StringComp(a As Variant, b As Variant) As String
Dim strOUT, strOUT_temp, strArray() As String
Dim intCount As Integer
strArray = Split(a.Text, ",")
For intCount = LBound(strArray) To UBound(strArray)
If (Val(strArray(intCount)) <= Val(b)) Then
strOUT_temp = "Yes"
Else
strOUT_temp = "No"
End If
If (intCount = LBound(strArray)) Then
strOUT = strOUT_temp
Else
strOUT = strOUT & "," & strOUT_temp
End If
Next
StringComp = strOUT
End Function
I've got a workbook where I have one worksheet which contains a lot of data.
My goal is to create a macro that inserts a formula in a separate sheet to copy the data from the first sheet. Lets call the first sheet "Numbers1" and the second sheet "TidyNumbers1".
In the sheet "TidyNumbers1" I want to loop through each cell from column A to M and rows 1 to 60. So I've got a macro that so far looks like this:
Sub updateFormulasForNamedRange()
Dim row, col, fieldCount As Integer
colCount = 13
RowCount = 60
For col = 1 To colCount
For row = 1 To RowCount
Dim strColCharacter
If col > 26 Then
strColCharacter = Chr(Int((row - 1) / 26) + 64) & Chr(((row - 1) Mod 26) + 65)
Else
strColCharacter = Chr(row + 64)
End If
Worksheets("TidyNumbers1").Cells(row, col).Formula = "=IF(Numbers1!E" & col & "<>0;Numbers1!" & strColCharacter & row & ";"")"
Next row
Next col
End Sub
But the formula is supposed to looks like this for Column A, row 2:
IF(Numbers1!E2<>0;Numbers1!A2;"")"
And the formula in Column A, row 3 should look like this:
IF(Numbers1!E3<>0;Numbers1!A3;"")"
Formula in Column B, row 2 should look like this:
IF(Numbers1!E2<>0;Numbers1!B2;"")"
In other words, the formula looks to see if the value in Column E, row % is anything but 0 and copies it if conditions are met.
But, I see that I need to translate my integer variable Row with letters, because the formula probably needs "A" instead of 1. Also, I get a 1004 error (Application-defined or object-defined error) if I just try to use:
Worksheets("Numbers1").Cells(row, col).Formula = "=IF(Numbers1!E" & row & "<>0;Numbers1!" & col & row & ";"")"
I clearly see that the integer row should be translated to letters, if that's possible. Or if anyone has any other suggestions that might work. Also, the 1004 error is unclear to me why happens. I can define a string variable and set the exact same value to it, and there's no error. So it's probably the formula bar that whines about it I guess?
Here is a former post of mine containing functions for conversion of column numbers to letters and vice versa:
VBA Finding the next column based on an input value
EDIT: to your 1004 error: Try something like this:
=IF(Numbers1!E" & row & "<>0,Numbers1!A" & row & ","""")"
(use ; instead of ,, and "" for one quotation mark in a basic string, """" for two quotation marks).
Would not it be easier to get the cell address with the Cells.Address function?
For example:
MsgBox Cells(1, 5).Address
Shows "$E$1"
Best Regards