finding a cell with variable data inside - vba

This might be too easy for you but I can not find a solution.
I have a value in column O2 which has a variable number inside. I am trying to use the vlookup to see in which column i have the duplicate of the value inside of O2.
If the the value in O2 would be the same, then it would be easy to use vlookup. But the value inside of O2 varies as following.
O2= Company (variable value)is not defined
So how can i put this in a formula to be able to find matching cell with O2?

If you try to use value from a range in a VBA VLookup formula, try something like this:
With Activesheet
.Range("C1") = WorksheetFunction.VLookup(.Range("O2").value,.Range("A1:B10"),‌​1)
End with
The With Activesheet, couldbe changed to the sheet you need to use. If you are using only one sheet, try this:
Range("C1") = WorksheetFunction.VLookup(range("O2").value,range("A1:B10"),‌​1)

Related

VBA Excel - Update Cell value based on other cell value

I am trying to write a formula in a Range of cells based on other range cells values from another worksheet. The formula is shown below:
ActiveSheet.Range("G14:G43").Formula = "=Worksheets("1ºperíodo").Range("V14:V43").Value"
But the code doesn't work and I get a syntax error. That must be related with the strings but I don't know how to fix it.
The value at V14 must be equal to the value at G14 until the last cell, i.e., the value at G14 equals the value at V43. Besides the syntax error is the formula correct based on what I expect to have?
"=Worksheets("1ºperíodo").Range("V14:V43").Value"
is not a formula. It is a value.
If you only want the static values then just assign the values:
ActiveSheet.Range("G14:G43").Value = Worksheets("1ºperíodo").Range("V14:V43").Value
If you want a live formula you need to pull vba from the string and use the .Address function:
ActiveSheet.Range("G14:G43").Formula = "=" & Worksheets("1ºperíodo").Range("V14").Address(0,0,,1)
But the above can be simplified to:
ActiveSheet.Range("G14:G43").Formula = "='1ºperíodo'!V14"
With the formula, we only need to refer to the first cell with a relative reference and vba will make the changes to each row.

String value from a cell to access worksheet of same name with a COUNTIF Function?

I was looking at this:
Use string value from a cell to access worksheet of same name
But I want to know how to do that with a CountIF function.
My WorkSheets are named by the Codes in column A3-A12, and that could increase in time.
=COUNTIF('100'!H:H,$B$2)
is how I'm counting all records on Worksheet 100 with the same value as in B2 (it's a date).
How do I use =INDIRECT and CountIF together? something like,
=INDIRECT(COUNTIF("'"&A4&"'!H:H,$B$2))
That doesn't work, but hopefully you understand what I wish to do.
I'm not calculating this function on each worksheet, then using Indirect to grab the values, because everyday those worksheets are blanked. So the functions wouldn't stay as well.
But that might be what I have to do, unless I can think of a macro.
Thanks!
Make a string that looks like the argument. If:
=COUNTIF('100'!H:H,$B$2)
works, then so will:
=COUNTIF(INDIRECT("'100'!H:H"),$B$2)
If we put the sheet-name in, say cell D4, then so will:
=COUNTIF(INDIRECT("'" & D4 & "'!H:H"),$B$2)

get multiple column names (header) in table associated with particular value in to a cell

i need to get multiple column names (header) in table associated with particular value in to a cell
as i explained, i need to get the heading names corresponding to value "n" to column E.
i used the formula
=INDEX((A$1:D$1),MATCH("n",A2:D2,0))
here. but it only give one column name.
i am open to vba scripts also. but i think it doesn't need vba. just improve the the above formula, may be. i tried and failed. any help. thank you guys
if you are really "open" to vba, I'll use one simple UDF like:
Function HeatherNames(rg As Range, rf As String) As String
For Each cell In rg
If cell = rf Then HeatherNames = HeatherNames & Cells(1, cell.Column).Value & "-"
Next cell
HeatherNames = Left(HeatherNames, Len(HeatherNames) - 1)
End Function
you can use it in the column E `=HeatherNames(A2:D2;"n") now you can select the arg.1 (range) and type (or referring to another cell) the arg.2
Assuming you have Excel 2010 or later, in E2:
=IF(COLUMNS($A:A)>COUNTIF($A2:$D2,"n"),"",INDEX($1:$1,AGGREGATE(15,6,COLUMN($A2:$D2)/($A2:$D2="n"),COLUMNS($A:A))))
Copy to the right and down as required.
It would actually be slightly more efficient (and certainly if your dataset in reality is quite large) to have the initial IF clause held within its own cell, such that it is calculated for each row only once, rather than for each instance of the formula within that row. So a better set-up would be, in E2:
=COUNTIF($A2:$D2,"n")
copied down. Then, in F2:
=IF(COLUMNS($A:A)>$E2,"",INDEX($1:$1,AGGREGATE(15,6,COLUMN($A2:$D2)/($A2:$D2="n"),COLUMNS($A:A))))
copied to the right and down again.
Regards

Convert Excel Formula to VBA

I have this formula that looks at various criteria across multiple columns and checks to see that if all the all the criteria match, it will paste data from one column to another. I've tried a couple ways to get it into VBA, but I can't seem to get anything to work. Thanks!
=INDEX($D$2:$D$1112,MATCH(1,($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3),0))
You are not going to be able to use that array formula to directly return a value to a cell. VBA does not process an array formula the way that the worksheet can. The best method is to use the worksheet's processing or one of the Application Evaluate methods.
Your lack of a worksheet to reference troubles me. When a formula is in a worksheet cell, it knows what worksheet it is on. When using formulas within VBA, the parent worksheet is a 'best guess' without explicit worksheet referencing.
Here are three methods to put the results from that array formula into Z2:Z4 on the active worksheet. Remember that these cell references should be modified to include the worksheet name.
With ActiveSheet
'this simply puts the formula into the worksheet then reverts the cell from the formula to the returned formula value
.Range("Z2").FormulaArray = "=INDEX($D$2:$D$1112, MATCH(1, ($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3), 0))"
.Range("Z2") = .Range("Z2").Value
'this uses the 'square bracket' method of evaluating a formula on-the-fly
'the formula being evaluated can be array or non-array
'this method is does not like building a formula string from pieces of text
.Range("Z3") = [INDEX($D$2:$D$1112, MATCH(1, ($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3), 0))]
'similar to the method directly above, Application.Evaluate does just that.
'the formula being evaluated can be array or non-array
'this method is easier to build a formula string from pieces of text
.Range("Z4") = Application.Evaluate("INDEX($D$2:$D$1112, MATCH(1, ($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3), 0))")
End With
You need 2 changes:
(1) To use a function in VBA when it is available in native Excel, you need to preface each function with Application.WorksheetFunction. ie:
x = Application.WorksheetFunction.Sum(y,z)
(2) To reference a cell within a sheet, in VBA, you need to access it specifically, in one of a few ways. The simplest for our purposes is the RANGE property, as follows:
x = Application.WorksheetFunction.Sum(Range("A1:A2"))
So to put those two changes together, your formula would look like this:
=Application.WorksheetFunction.INDEX(Range("$D$2:$D$1112",Application.WorksheetFunction.MATCH(1,(RANGE("$A$2:$A$1112"=RANGE("$U$7")*(Range("$C$2:$C$1112"=Range("$W$7")*(Range("$B$2:$B$1112"=Range("F3"),0))
Although I see now having gone through this that you seem to be using an Array Formula - not sure if any special jigging is required to get that to work.

VBA Variable in range

Hey is it possible to use a variable within a range object?
My goal is to keep the formula that is in the excel cell, but the value of the cell itself is to be deleted.
The procedure for a better understanding:
1. search for a value, where "i" is indicating the row, where the value is found
2. delete this value, BUT: save the formula
3. another value is entered in this cell and the formula is still active and is checking some condition
Searching is no problem, but I cant delete the value of the cell without deleting the formula aswell.
This is what i tried:
rangeA = "D" & i
Set rConstants = Sheet1.range(rangeA).SpecialCells(xlCellTypeConstants)
rConstants.ClearContents
Any ideas?
You should set the constant equal to rangeA.Formula. You can then use the constant wherever you would like within your module or VBA project.