Dynamic SUM Range Formula [duplicate] - excel

Can anyone solve this?
Sub test
Dim i as integer
For I = 1 to 10
ActiveCell.Offset(0, 2).Formula = "=Sum(E15,&i&)"
Next I
End Sub

your actual goal is unclear
you may want to start form this code
Sub test()
Dim i As Integer
For i = 1 To 10
cells(i, 4).Formula = "=Sum(E" & i & ":E15)"
Next
End Sub
and adjust it to your needs, knowing that:
it currently writes in cells "D1:D10"
since cells(i, 4) references a cell in 4th column (i.e.: column "D") 4 and i row, and we're inside a loop where i is looping through 1 to 10
so if:
you want to reference a different column then just change 4 to the proper column index
you want to reference a different row then just change i to the proper row index (may be some i+2 if you need to iterate through 1 to 10 but start writing from row 3)
the formula written in those cells is:
=SUM(E1:E15) in D1,
=SUM(E2:E15) in D2,
....
=SUM(E10:E15) in D10.
so just change "=Sum(E" & i & ":E15)" to your actual needs

You're close, trying to use ampersands (&) to concatenate strings.
ActiveCell.Offset(0, 2).Formula = "=Sum(E15," & i & ")"
Use the ampersands between strings to merge them, not inside strings.

Related

Compute countif in column Range & combine if with count if

I have two formulas that I need to transfer to VBA.
On Excel, my formula would be =countif(A$2:A2,A2) so I transferred that using this formula but everything is returning to 1. The rows didn't become dynamic and I want only the values to be displayed.
For a = 2 To lrow
ws.Range("T" & a).Formula = "=CountIf(A$2&"":""&A2)"",""&A2)"
Next a
Next formula that I use in Excel is
=IF(COUNTIF(A:A,A2)>Q2,"Check","Ok")
I tried this formula in VBA:
For i = 2 to lrow
If Countif(ws.Range("A2:A" & lrow), "A2") > ws.Range("Q2:Q", & lrow) Then
ws.Range("T" & i).Value = "Check"
Else
ws.Range("T" & i).Value = "Ok"
End If
Next i
You could populate column T with your first formula with this line of code:
ws.Range("T2:T" & lrow).FormulaR1C1 = "=COUNTIF(R2C[-19]:RC[-19],RC[-19])"
I can't advise on your second formula unless you clarify where you want to write it...

Combining CONCAT functions using variables [duplicate]

Can anyone solve this?
Sub test
Dim i as integer
For I = 1 to 10
ActiveCell.Offset(0, 2).Formula = "=Sum(E15,&i&)"
Next I
End Sub
your actual goal is unclear
you may want to start form this code
Sub test()
Dim i As Integer
For i = 1 To 10
cells(i, 4).Formula = "=Sum(E" & i & ":E15)"
Next
End Sub
and adjust it to your needs, knowing that:
it currently writes in cells "D1:D10"
since cells(i, 4) references a cell in 4th column (i.e.: column "D") 4 and i row, and we're inside a loop where i is looping through 1 to 10
so if:
you want to reference a different column then just change 4 to the proper column index
you want to reference a different row then just change i to the proper row index (may be some i+2 if you need to iterate through 1 to 10 but start writing from row 3)
the formula written in those cells is:
=SUM(E1:E15) in D1,
=SUM(E2:E15) in D2,
....
=SUM(E10:E15) in D10.
so just change "=Sum(E" & i & ":E15)" to your actual needs
You're close, trying to use ampersands (&) to concatenate strings.
ActiveCell.Offset(0, 2).Formula = "=Sum(E15," & i & ")"
Use the ampersands between strings to merge them, not inside strings.

Lookup using dynamic filename in VBA [duplicate]

Can anyone solve this?
Sub test
Dim i as integer
For I = 1 to 10
ActiveCell.Offset(0, 2).Formula = "=Sum(E15,&i&)"
Next I
End Sub
your actual goal is unclear
you may want to start form this code
Sub test()
Dim i As Integer
For i = 1 To 10
cells(i, 4).Formula = "=Sum(E" & i & ":E15)"
Next
End Sub
and adjust it to your needs, knowing that:
it currently writes in cells "D1:D10"
since cells(i, 4) references a cell in 4th column (i.e.: column "D") 4 and i row, and we're inside a loop where i is looping through 1 to 10
so if:
you want to reference a different column then just change 4 to the proper column index
you want to reference a different row then just change i to the proper row index (may be some i+2 if you need to iterate through 1 to 10 but start writing from row 3)
the formula written in those cells is:
=SUM(E1:E15) in D1,
=SUM(E2:E15) in D2,
....
=SUM(E10:E15) in D10.
so just change "=Sum(E" & i & ":E15)" to your actual needs
You're close, trying to use ampersands (&) to concatenate strings.
ActiveCell.Offset(0, 2).Formula = "=Sum(E15," & i & ")"
Use the ampersands between strings to merge them, not inside strings.

Vlookup with dynamic col num index [duplicate]

Can anyone solve this?
Sub test
Dim i as integer
For I = 1 to 10
ActiveCell.Offset(0, 2).Formula = "=Sum(E15,&i&)"
Next I
End Sub
your actual goal is unclear
you may want to start form this code
Sub test()
Dim i As Integer
For i = 1 To 10
cells(i, 4).Formula = "=Sum(E" & i & ":E15)"
Next
End Sub
and adjust it to your needs, knowing that:
it currently writes in cells "D1:D10"
since cells(i, 4) references a cell in 4th column (i.e.: column "D") 4 and i row, and we're inside a loop where i is looping through 1 to 10
so if:
you want to reference a different column then just change 4 to the proper column index
you want to reference a different row then just change i to the proper row index (may be some i+2 if you need to iterate through 1 to 10 but start writing from row 3)
the formula written in those cells is:
=SUM(E1:E15) in D1,
=SUM(E2:E15) in D2,
....
=SUM(E10:E15) in D10.
so just change "=Sum(E" & i & ":E15)" to your actual needs
You're close, trying to use ampersands (&) to concatenate strings.
ActiveCell.Offset(0, 2).Formula = "=Sum(E15," & i & ")"
Use the ampersands between strings to merge them, not inside strings.

VBA- Concatenate Cells with If function

I have a code that returns all the data I want from a query. The problem is that the csv format separates the data, even account names with commas in them. For each row, column A is the account name followed by 11 blocks of integer data (making a total of 12 cells used per row). Fortunately, for the accounts that have commas, the result is only one additional cell (making a total of 13 cells used per row).
I need an IF-THEN formula that will concatenate Column A & Column B if there are 13 used cells in that row, otherwise leaving things alone. Being new to VBA concatenate is giving me huge problems.
Any suggestions? Thanks in advance.
Well, just from a term point of view, Excel Formulas and Excel VBA are not the same thing.
In fact, there are a lot of people on here that are formula pros that know nothing about VBA and visa versa.
That being said, you could use something like:
=IF(COUNTBLANK(C1:N1)=0,A1&" "&B1,A1)
Of course, assuming your data looks like this:
It would be easier to use:
=IF(N1="",A1,A1&" "&B1)
Concatenating in Excel formulas and VBA is pretty simple.
Simply separate the strings you wish to concatenate with an amperstand &
If I want to concatenate A1 and B1, I can use =A1&B1 as shown in the formula.
If I want to add a space, or even a word, I can just add it between them as a string:
=A1&" is way cooler than "&B1
Edit:
VBA:
Sub ConcatenateCells()
Application.ScreenUpdating = False
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
If InStr(c, ",") > 0 Then c = c & c.Offset(0, 1)
'You could also add a space between the two using c = c & " " & c.Offset(0, 1)
Next c
Application.ScreenUpdating = True
End Sub
I don't know if you want to scoot the data over after you have concatenated the values, but if you do, you can use this:
Sub ConcatenateAndScootCells()
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
If InStr(c, ",") > 0 Then
c = c & c.Offset(0, 1)
c.Offset(0, 1).Delete xlToLeft
End If
Next c
End Sub
Still assuming I got your data format right, here is what the before and after looks like:
Final Edit:
If you absolutely insist on counting the number of used rows instead of simply looking for a coma in column A, then use this:
Sub ConcatenateAndScootCells()
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
If Cells(c.row, Columns.Count).End(xlToLeft).Column = 13 Then
c = c & " " & c.Offset(0, 1)
c.Offset(0, 1).Delete xlToLeft
End If
Next c
End Sub

Resources