VBA Formula to cell that contains string and another formula - excel

I need to paste into a cell, using VBA, 3 connected VLOOKUP searches
TableIndex = 1
i = 1
ShiftColumn = 20
ShiftCount = 20
Cells(TableIndex, 43).FormulaR1C1 = "=CONCATENATE(VLOOKUP(R" & i & "C" & ShiftColumn & "R7C2:R" & ShiftCount & "C10,9),TEXT(VLOOKUP(R" & i & "C" & ShiftColumn & "R7C2:R" & ShiftCount & "C10,3),""hhmm""),TEXT(VLOOKUP(R" & i & "C" & ShiftColumn & "R7C2:R" & ShiftCount & "C10,4),""hhmm""))"
But it gives an 1004 error

Related

How to achieve group by like functionality in excel

Please see below an image to demonstrate this problem.
Is there any functionality within excel such that if the attribute column is set to 'total', the value column will be populated automatically, by summing attribute1 and attribute2 for the given id?
Any tips would be much appreciated.
Put this in your sheet module:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RngAttributes As Range
Dim RngIDs
Dim RngValues
Dim RngFocus As Range
Dim StrTrigger As String
Dim StrAttribute1 As String
Dim StrAttribute2 As String
Dim DlbFirstRow As Double
Set RngIDs = Range("A:A")
Set RngAttributes = Range("B:B")
Set RngValues = Range("C:C")
StrTrigger = "total"
StrAttribute1 = "attribute1"
StrAttribute2 = "attribute2"
Set RngAttributes = Intersect(Target, RngAttributes)
DlbFirstRow = RngIDs.Row
If Not (RngAttributes Is Nothing) Then
For Each RngFocus In RngAttributes
If RngFocus.Value = VBA.Strings.LCase(StrTrigger) Or _
RngFocus.Value = VBA.Strings.UCase(StrTrigger) Or _
RngFocus.Value = Excel.WorksheetFunction.Proper(StrTrigger) Then
Cells(RngFocus.Row, RngValues.Column).FormulaR1C1 = "=SUMIFS(R" & DlbFirstRow & "C:R" & RngFocus.Row - 1 & "C,R" & DlbFirstRow & "C" & RngIDs.Column & ":R" & RngFocus.Row - 1 & "C" & RngIDs.Column & ",RC" & RngIDs.Column & ",R" & DlbFirstRow & "C" & RngAttributes.Column & ":R" & RngFocus.Row - 1 & "C" & RngAttributes.Column & ",""" & StrAttribute1 & """) + SUMIFS(R" & DlbFirstRow & "C:R" & RngFocus.Row - 1 & "C,R" & DlbFirstRow & "C" & RngIDs.Column & ":R" & RngFocus.Row - 1 & "C" & RngIDs.Column & ",RC" & RngIDs.Column & ",R" & DlbFirstRow & "C" & RngAttributes.Column & ":R" & RngFocus.Row - 1 & "C" & RngAttributes.Column & ",""" & StrAttribute2 & """)"
End If
Next
End If
End Sub

Excel VBA IF OR

Please help to run this formula if the cell C2 value is saturday
Range("F2").Formula = "=IF(AND(D2>=TIME(9,16,0),D2<=TIME(11,59,0)),D2-TIME(9,0,0),IF(AND(D2>=TIME(14,16,0),D2>=TIME(12,00,0)),D2-TIME(14,0,0)*1,0))"
and if Cell C2 value is Sunday to Friday
Range("F2").Formula = "=IF(AND(D2>=TIME(8,16,0),D2<=TIME(10,30,0)),D2-TIME(8,0,0),IF(AND(D2>=TIME(12,16,0),D2>=TIME(10,31,0)),D2-TIME(12,0,0)*1,0))"
Until the last row which has data in the sheet.
In short check the value in C2, if = saturday formula 1 or formula 2.
From C2 onwards Column C contains date. eg
05/03/2019
Sample Excel file Screenshot
Try this:
Option Explicit
Sub Test()
Dim i As Long
For i = 2 To Cells(Rows.Count, "C").End(xlUp).Row
If Weekday(Range("C" & i)) = 7 Then
Range("F" & i).Formula = "=IF(AND(D" & i & ">=TIME(9,16,0),D" & i & "<=TIME(11,59,0)),D" & i & "-TIME(9,0,0),IF(AND(D" & i & ">=TIME(14,16,0),D" & i & ">=TIME(12,00,0)),D" & i & "-TIME(14,0,0)*1,0))"
Else
Range("F" & i).Formula = "=IF(AND(D" & i & ">=TIME(8,16,0),D" & i & "<=TIME(10,30,0)),D" & i & "-TIME(8,0,0),IF(AND(D" & i & ">=TIME(12,16,0),D" & i & ">=TIME(10,31,0)),D" & i & "-TIME(12,0,0)*1,0))"
End If
Next i
End Sub

VBA range.formula issue (LEN, RIGHT & LEFT)

I'm trying to do a VBA code to accomplish 2 things as follows:
Count how many characters there is on cell A1, using the formula LEN(A1) and one the last line, I'm trying to have the formula RIGHT(LEFT(A1;Q1-2);6) on cell J1
Please follow down my VBA code so far:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("P" & i).Formula = "=LEN(" & cel & ")"
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-" & 2 & ")," & 6 & ")"
Next i
It seems something silly what is missing, however, I couldnt manage to solve it so far
Thanks in advance
You’re missing a Right, and some other things
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-2), 6)"

#Value! error when writing formula

I have a piece of code that loops down a column and inserts a formula into each cell in that column. The code runs, the only problems is that each cell the formula is inserted into displays #Value! Does anyone know how to fix this?
Here is the piece of code with the problem:
Dim j As Long
'Loop down the rows in mainfile
For j = 2 To lastFullRow2
' Make each argument a string, then concatenate it all all into large string
Dim firstArgument As String
firstArgument = "ws_multidax.Range(" & valuecolumnLetter & "2:" & valuecolumnLetter & lastFullRow1 & ")"
Dim secondArgument As String
secondArgument = "ws_multidax.Range(" & parameter1columnLetter & "2:" & parameter1columnLetter & lastFullRow1 & ")"
Dim thirdArgument As String
thirdArgument = "ws_multidax.Range(" & parameter2columnLetter & "2:" & parameter2columnLetter & lastFullRow1 & ")"
Dim fourthArgument As String
fourthArgument = "ws_multidax.Range(" & parameter2columnLetter & "2:" & parameter2columnLetter & lastFullRow1 & ")"
Dim condition3 As String
condition3 = "ws_mainfile.Range(" & "D2:" & D & j & ")"
Dim patid1 As String
patid1 = "ws_multidax.Range(" & "D2:" & D & lastFullRow2 & ")"
With ws_mainfile
Dim commandstring As String
'The formula we want is a concatenated string
commandstring = "{=INDEX(" & firstArgument & ",MATCH(1,(" & secondArgument & "=" & condition1 & ")*(" & thirdArgument & "=" & condition2 & ")*(" & patid1 & "=" & condition3 & "),0))}"
'Insert the formula into each cell as the loop goes down the rows
ws_mainfile.Range("AN" & j).Formula = Eval(commandstring)
'ws_mainfile.Range("AN" & j).Formula = commandstring
End With
Next j
The #Value! gets put into the cells when I run the line with the Eval(commandstring). When I run the line with just formula = commandstring, the formula gets put in each cell, but it does not solve.
Your code has 2 problems:
You are putting in the curly braces of an array formula manually, don't do this
You are putting in an array formula as a regular formula
So to correct, do the following:
'1. Change this line:
commandstring = "{=INDEX(....)}"
'And simply remove the curly braces {} from its beginning and end
commandstring = "=INDEX(....)"
'2. Change this line:
ws_mainfile.Range("AN" & j).Formula = Eval(commandstring)
'To use the .FormulaArray property instead of just .Formula:
ws_mainfile.Range("AN" & j).FormulaArray = commandstring
You're on the right track. There's no need to use the eval function, as you have built a string formula in code. The problem is you have surrounded it with the braces, which excel treats as if you had entered text.
Try this instead:
commandstring = "=INDEX(" & firstArgument & ",MATCH(1,(" & secondArgument & "=" & condition1 & ")*(" & thirdArgument & "=" & condition2 & ")*(" & patid1 & "=" & condition3 & "),0))"
'Insert the formula into each cell as the loop goes down the rows
ws_mainfile.Range("AN" & j).Formula = commandstring

VBA: Using & inside of an equation

I want to make a formula with a & inside it but VBA thinks its a concatenate symbol
""&"" does not work, are there any other tricks?
My formula:
Sheets("Elasticity").Cells(iRow, 38).Formula = "=SUMIFS(" & "All_Models!$W$2:$W$" & nrow & ",All_Models!$G$2:$G$" _ & nrow & ",Elasticity!L" & iRow & ",All_Models!$AL$2:$AL$" & nrow & ",Elasticity!AK" & iRow & _ ",All_Models!$B$2:$B$" & nrow & "," & "" <= "" & "&" & "ElasticityA" & iRow & ")"
I want to transform:
"" <= "" & "&" & "Elasticity!A" & iRow & ")"
into:
"<="&Elasticity!A2)
How about just this:
dim s as string
s = "my_complicated_formula" & "&" & "and_another_formula"
...All_Models!$B$2:$B$" & nrow & ",""<=""&" & "Elasticity!A" & iRow & ")"
s="foo" & Chr(38) & "bar"
Where chr(38) = &

Resources