Combining CONCAT functions using variables [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

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.

Dynamic SUM Range Formula [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

excel: is it possible to put a FOR statement as a formula within a cell?

can i do this =(For i=1 to 100, print i)
is there a way to put a FOR statement inside a cell WITHOUT USING VBA?
You can use an array to get the numbers 1 through 100, but you're limited on what you can do with them. You can't, for instance, concatenate in an array formula (which your pseudo code suggests). But you could SUM, AVERAGE or many other operations.
{=SUM(ROW(1:100))}
{=AVERAGE(ROW(1:100))}
{=MAX(ROW(1:100))}
The braces means enter with control+shift+enter, not just enter.
The VBA for this isn't fancy at all :-)
Option Explicit
Sub SimpleForLoop()
Dim i As Integer
For i = 1 To 100 Step 1
With ActiveWorkbook.Sheets(1).Cells(1, 1)
.Value = .Value + i
End With
Next
End Sub
The simple code above puts the value 5050 in cell A1.
If you want to concatenate a string instead, slap this code into your for-loop:
With ActiveWorkbook.Sheets(1).Cells(2, 1)
If .Value = "" Then
.Value = CStr(i)
Else
.Value = .Value & "," & CStr(i)
End If
End With
Which will print the following into cell A2:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100

Resources