Concatenate 2 Columns in VBA - vba

I have 2 columns (column F and G), let's say they both go up to row 10. In row S, I want a concatenate statement: F(G)
I've tried:
SHEET.range("S2:S2" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"
and
SHEET.column(18).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"
The problem with either is that after the 10th row, the formula keeps running such that every cell goes "()". How do I edit the macro to end when there's no more data in column F and G?

with:
range("S2:S2" & LastRow)
if LastRow is 10 then the string will be S2:S210 as you have the 2 in the second reference. You want to remove that:
range("S2:S" & LastRow)
So:
SHEET.range("S2:S" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"

Related

How to loop through rows starting in row 2 and find out if row is empty from column E to column AE

Basically I need to figure out how to loop through rows starting with row 2 column E to column AE and to the end of rows, find which ones are empty from column E over to column AE then place a letter code in that rows cell column d and place a different letter code if info is found in any cell in that selection (E thru AE) then of course move down one row and do the same until know info left in rows with column A as a reference on when to stop looping
If anyone can help I'd appreciate it.
Everything I have tried keeps blowing up, can't find a way to assign a value to multiple cell selections, at least that is how I have tried to attack this.
Hope someone can help
This will loop through Column E and if all cells in the active row to Column AE are empty it will put "E" in Column D of the active row, or a "F" in any cell has a value.
For Each cell In Range("E2:E" & Cells(Rows.Count, "E").End(xlUp).Row)
Dim rng As Range
Set rng = Range("E" & cell.Row & ":" & "AE" & cell.Row)
If Application.WorksheetFunction.CountA(rng) = 0 Then
Range("D" & cell.Row).Value = "E"
Else
Range("D" & cell.Row).Value = "F"
End If
Next cell

coloring cells which have same value

I need to color cells green in column A if their cell value matches cells in column E. cells start from second row. values aren't sorted, so they don't match in rows. I need to check every cell in column A and compare them to every cell in column E.
Please help.
Try following code:
For i = 2 To 100
If ((Worksheets("Table1").Range("A" & i).Value <> "") And (Worksheets("Table1").Range("A" & i).Value = Worksheets("Table1").Range("E" & i).Value)) Then
Range("A" & i).Interior.ColorIndex = 50
Else
Range("A" & i).Interior.ColorIndex = 2
End If
Next i
Just make sure you insert in the second line of the code the name of your table
You can also change the number 100 to the number of rows you want to compare.

How to Vlookup where column has value, not blank cells?

I'm trying to lookup an ID from column I, in column A.
Dim x As Long
lr = Worksheets("Risk Explorer greeks").Cells(Rows.Count, "I").End(xlUp).Row
Range("J2:J2" & lr).FormulaR1C1 = "=VLOOKUP(RC[-1], R1C1:R50000C1, 1, False)"
I have ~40,000 values in column J, however when I run this code it populates down to cell 237,000.
How can I look up column J where it has a value, and not lookup loads of blank cells?
Alternatively is there a faster way to do this lookup rather than the above formula?
You're appending the number 37000 to the string "J2:J2", which gives you "J2:J237000".
Replace Range("J2:J2" & lr) with Range("J2:J" & lr). You should be good to go.

Loop Through Columns to Insert SUM Formula

I have a program where I need to find the sum of columns 7 through 30 (G through AD). What I am trying to do is loop through and insert the formula (=SUM(Columns(i)2:Columns(i)1000)) but obviously Columns(i) is not the letter but the number. This is not complying with the format required by SUM so I'm wondering what I can do instead.
I have a program where old sheets will be deleted and new ones added holding configuration data for a product. This means formulas cannot be in the sheet inself or any other sheet referring to it. Prices will be held in columns G through P and U through AD which I need to find the total for and place it in row 1 above the corresponding data. When I try:
For i = 7 To 30
wsNewSheet.Cells(1, i).Value = "=SUM(" & Columns(i).Select & "2:" & Columns(i).Select & "1000)"
Next i
Columns(i) is returning as "True" for some reason. I have also tried to place the totals in a different sheet as the new sheets (wsNewSheet) were being created.
For i = 2 To 30
For f = 7 To 30
wsTotals.Cells(1, i).Value = "=SUM(" & wsNewSheet & "!" & Columns(f).Select & "2:" & Columns(f).Select & "1000)"
Next f
Next i
However, this did not work either. This statement returned "Run-time error '438.' Object doesn't support this property or method." I attemped to do research on this error, but could not fix my situation. Thanks for any help.
You don't need loop here, use single line instead:
Range("G1:AD1").Formula = "=SUM(G2:G1000)"
Excel automatically adjust formulas:
in G1 you'd have =SUM(G2:G1000)
in H1 you'd have =SUM(H2:H1000)
....
in AD1 you'd have =SUM(AD2:AD1000)

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