Combining two formulas in one cell - vba

I have two cells in my excel workbook that contain vba to perform a formula. What I'm trying to do is put two formulas in one cell instead of two separate ones. I have provided the code below for what I have so far. The outcome I want in cell C1 is to say "AMTS:(count) Cleaners:(count)" both together not separate. I tried just putting the & between the formulas and it kept returning a false value lol.
Thanks!
ws.Range("$C$1").Formula = "=""AMTs:("" & COUNTIF(E:E,""AMT"")&"")"""
ws.Range("$D$1").Formula = "=""Cleaners:("" & COUNTIF(E:E,""Cleaner"")&"")"""

ws.Range("$C$1").Value = "=""AMTs:("" & COUNTIF(E:E,""AMT"")&"")""" & "="" Cleaners:("" & COUNTIF(E:E,""Cleaner"")&"")"""
This is the output from the macro.
or
ws.Range("$C$1").Formula = "=""AMTs:("" & COUNTIF(E:E,""AMT"")&"") Cleaners:("" & COUNTIF(E:E,""Cleaner"")&"")"""

Related

How do I set a formula in a cell using R1C1 notation from variables?

I've got a series of IF/ELSE clauses to see if items fit into one of three categories. If an item doesn't fit into any of them, its category field should simply read "SELECT".
Now, with simple text strings it works fine. But I also want to include a simple formula (as in most of the other autopopulated fields in the sheet) to blank the field if it's on an empty line. Based on the code I've used in other cases, I would want to set the cell's formula to
=IF(ISBLANK($A2, "", "SELECT")
(...where the cell itself is B2)
Problem is, the cell to look at will depend on which line it's on; A2, A3, A4 and so on. The column number remains constant, but the row number is defined by the variable iImportCounter.
I've tried both .Value and .Formula, both """ and Chr(34), and it still doesn't seem to work. The full code for that line as it stands is
Else:
wsCalc.Cells(iImportCounter, 3).Formula = "=IF(ISBLANK($" & Application.ConvertFormula(Cells(iImportCounter, 2), xlA1) & ", " & Chr(34) & Chr(34) & ", " & Chr(34) & "SELECT" & Chr(34) & ")"
Is there something terribly obvious I'm missing?
I've seen comments elsewhere that the fact that it's in a module rather than a sheet-based macro could make some of that code malfunction, but I'm not sure what or how.
It depends on what the iImportCounter starts at. If it is 3 (row 3 in B3) then the formula would be,
with wsCalc.Cells(iImportCounter, 3)
.FormulaR1C1 = "=IF(ISBLANK(R[-1]C1), """", ""SELECT"")
end with
The R in RC1 means the same row that you are putting the formula on so if you are putting on row 2 and you want to reference A2 then R is all you need. If the formula is going into B3 then you need to reference the current row minus 1 with R[-1]C1.
The C1 in RC1 means a 'locked' (absolute) column A (the first column). This doesn't seem absolutely necessary as the formula does not travel laterally.
Assuming wsCalc is a reference to a range of cells:
wscalc.FormulaR1C1 = "=IF(ISBLANK(RC1),"""",""SELECT"")"
As a working example:
Sub Test()
ThisWorkbook.Worksheets("Sheet1").Range("B1:B10").FormulaR1C1 = "=IF(ISBLANK(RC1),"""",""SELECT"")"
End Sub

putting a string from a cell into the middle of my index-match VBa script

I am trying to use the index-match formula to reorganize data such that all of the names in column J that have a matching value in column A will be placed in the same spot. I'm going to do this for 5 different columns so that the 5 names on a team will be in the same row as the name of the corresponding client.
My issue is that the index-match formula needs to be able to dynamically shorten or lengthen the size the arrays it uses based on how many clients there are when the VBA script is run.
I can dynamically determine what numbers I need in the formula using COUNTA, but the code will not compile when I try to put it in my formula. My formula is below
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & ",MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0)"
As you can see I need the strings in cells J3 and J1 to be used as the arrays for the index match. J3 = $J$2:$J$2369 and J1 = $A$2:$A$1113
When I run the code it gives me a "Application-Defined or Object-defined error."
You need to use the Range member of worksheet
so use 'test sheet two'!Range("J2:J2369") rather than 'test sheet two'!("J2:J2369").
The following runs
ActiveCell.Formula = _
"=INDEX('test sheet two'!Range(""" & Range("J3") & """) _
,MATCH(Sheet1!A5,'test sheet two'!Range(""" & Range("J1") & """),0))"
Your formula was not including the column criteria for the INDEX Function.
Try:
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & "," & _
"MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0), 1)"
Notice the additional , 1)" on the end of the formula.
Also, you do not have to first Select the cell which you want to enter the formula in, you could just use:
Range("B7").Formula =

Excel Listobject Table Insert Rows Causes Cell Formula Error

Background:
I've created a Word template that contains a routine that creates an array which holds a Project title in the first dimension and a Goal title in the second dimension. This array is transposed to a table in Excel to be used in creating a timeline/gantt chart.
Problem:
The transpose places the array information appropriately into an Excel table and expands the size of that table as desired. Cell B5 is the beginning of the DataBodyRange and the start of where I want to paste the array information.
' paste headings from array into excel
xlWS.Range("B5:C" & UBound(gHeadings, 2)) = xlApp.Transpose(gHeadings)
What does not occur in the proper manner is in subsequent data cells within the table. Timeline cells have the following formula:
=IF(AND(COLUMNS($H$5:H10)>=$E5,COLUMNS($H$5:H10)<=$F5),IF(COLUMNS($H$5:H10)-$E5<ROUND(($F5-$E5+1)*$G5,0),fillblock,""),"")
When the transpose is finish, the row of cells containing the referenced formula errors on the row of the table that was the original last row of the DataBodyRange. On that row, row 10, the formula gets changed from what's displayed above to:
=IF(AND(COLUMNS($H$5:H119)>=$E10,COLUMNS($H$5:H119)<=$F10),IF(COLUMNS($H$5:H119)-$E10<ROUND(($F10-$E10+1)*$G10,0),fillblock,""),"")
Any thoughts or understanding of why this happens, and how to prevent it?
The overall answer as to why Excel Tables behave in the fashion I've described is not answered. But, here is my workaround. The speed hit is negligible.
' variable to hold the formula and fix the double-quote ("") issue in vba
sEmpty = Chr(34) & Chr(34)
sFormula = "=IF(AND(COLUMNS($H$5:H5)>=$E5,COLUMNS($H$5:H5)<=$F5),IF(COLUMNS($H$5:H5)-$E5<ROUND(($F5-$E5+1)*$G5,0),fillblock," & sEmpty & ")," & sEmpty & ")"
' paste cell formula into the new worksheet
lTblRows = xlLO.DataBodyRange.Rows.Count
xlWS.Range("H5:AK" & lTblRows + 4).Formula = sFormula
This effectively copies the formula into each cell referenced within the table, copying over the cells that got mis-referenced during the row insert.

VBA vlookup not changing values

Hello I am new to the form and am having trouble with creating a VBA that performs a vlookup on a user selected case in A2. In the attached example the values change in some cells (D2-F2) based on the selection, but the formula results, so I can not perform a select case later on because there is no value, only a formula. The value does appear in B2 and C2 but does not change when the user selects a different case. Thank you.
https://app.box.com/s/qrs0r5gf0vth6wpbj1w9
' Step 3 Add additional selection information
Dim LastRowNo As Long
LastRowNo = Cells(Rows.Count, "CA").End(xlUp).Row
Worksheets("annovar").Range("B2").Value = Evaluate("=VLOOKUP(A2,CA5:CB" & LastRowNo & ",2,0)")
Worksheets("annovar").Range("C2").Value = Evaluate("=VLOOKUP(A2,CA5:CC" & LastRowNo & ",3,0)")
Worksheets("annovar").Range("D2").Formula = "=IFERROR(VLookup(A2,CA5:CD" & LastRowNo & ",4,0),"""")"
Worksheets("annovar").Range("E2").Formula = "=IFERROR(VLookup(A2,CA5:CE" & LastRowNo & ",5,0),"""")"
Worksheets("annovar").Range("F2").Formula = "=IFERROR(VLookup(A2,CA5:CF" & LastRowNo & ",6,0),"""")"
Check the ranges that you're using in the VLOOKUP. I think you may be looking in the wrong column. There isn't anything in the Column("CA") so LastRowNo = 1 and Range("CA5:CB1") is empty. The information that you're looking for I think was in Range(BP8:BQ12") because of the way that you're inserting and deleting rows every time you run it.
Would it not be better to create a new sheet or clear a different sheet every time you run the macros? That way the code wont be so awkward?

Remove single quote VBA function

To simplify, I have two sheets in an Excel workbook.
If I write =sheet2!R3C2 in one of the cells of sheet1, the cell takes the value of the cell R3C2 of sheet2 correctly.
I would like to generalize that for many rows in VBA, so my code is:
row = XX a long
temp = "=sheet2!R" & row - 1 & "C2"
Range("c" & row).Value = temp
But when I use this code, the value of the cell(row,3) is =sheet2!'RXXC2'
How can I remove the single quotes ??
Range("C" & row).Formula = temp
would produce the correct formula in your cell.
What you should consider doing instead of looping is
Range("A1").Formula = "=Sheet2!$B1"
Range("A1").Resize(100, 1).Formula = Range("A1").Formula
The first line inserts a formula =Sheet2!$B1 in cell A1 of your active sheet. The $ dollar sign assures that the column will not be incremented (same applies with numbers)
Then the second line duplicates the formula across 100 rows down from A1 replacing the number after the B column
So now
A1 =Sheet2!B1
A2 =Sheet2!B2
A3 =Sheet2!B3
...
Also, it's a bit unclear what you're trying to actually do so consider another option which is saving the value of formula into another range using the Evaluate() function
Range("c" & row).Value = Evaluate(temp) Or Range("C" & Row).Value = [temp]
try to write Range("c" & row).FormulaR1C1=temp
You want to set the formula, not the value:
Range("c"&row).FormulaR1C1 = temp