Insert columns and values programmatically with IF statement
Dim i As Integer
With Sheet1
For i = 1 To 4
.Columns(2).Insert shift:=xlToRight
Next i
.Range("B1").Resize(1, 4).Value = _
Array("SUM", "PRODUCT", "QUOTIENT", "SUMPRODUCT")
.Range("B2").Resize(1, 4).Value = _
Array("=SUM(1,1)", "=PRODUCT(1,1)", "=QUOTIENT(1,1)",
"=SUMPRODUCT(1,1)")
End With
On Oct 16, 6:06*am, "kittronald" wrote:
* * I'm trying to perform the following with the least amount of code..
* * 1) Test the value of "x" and if TRUE, do the following ...
* * 2) On Sheet1, select column B and insert 4 columns to the right
* * 3) On Sheet1, enter the following text values:
* * * * * * B1 = "SUM"
* * * * * * C1 = "PRODUCT"
* * * * * * D1 = "QUOTIENT"
* * * * * * E1 = "SUMPRODUCT"
* * 4) On Sheet1, enter the following formulas:
* * * * * * B2 * *=SUM(1,1)
* * * * * * C2 * *=PRODUCT(1,1)
* * * * * * D2 * *=QUOTIENT(1,1)
* * * * * * E2 * *=SUMPRODUCT(1,1)
* * With Sheet1
* * * * If x = "SUM" Then
* * * * * * Columns($B:$B).Select
* * * * * * Selection.Insert Shift:=xlToRight
* * * * * * Selection.Insert Shift:=xlToRight
* * * * * * Selection.Insert Shift:=xlToRight
* * * * * * Selection.Insert Shift:=xlToRight
* * * * * * Range("B1").Select
* * * * * * ActiveCell.FormulaR1C1 = "SUM"
* * * * * * Range("C1").Select
* * * * * * ActiveCell.FormulaR1C1 = "PRODUCT"
* * * * * * Range("C1").Select
* * * * * * ActiveCell.FormulaR1C1 = "QUOTIENT"
* * * * * * Range("D1").Select
* * * * * * ActiveCell.FormulaR1C1 = "SUMPRODUCT"
* * * * * * Range("B2").Select
* * * * * * ActiveCell.FormulaR1C1 = "=SUM(1,1)"
* * * * * * Range("C2").Select
* * * * * * ActiveCell.FormulaR1C1 = "=PRODUCT(1,1)"
* * * * * * Range("D2").Select
* * * * * * ActiveCell.FormulaR1C1 = "=QUOTIENT(1,1)"
* * * * * * Range("E2").Select
* * * * * * ActiveCell.FormulaR1C1 = "=SUMPRODUCT(1,1)"
* * * * Else
* * End With
* * Is there any way to do this with less code ?
- Ronald K.
|