View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Insert Column w/ Equation - Fill to bottom

Hi mkerstei,

You could try one of these procedures, depending on what you're doing. They
are self-explanatory, but modify them as required with your columns and
formulas.


Sub InsertFormulas1()
' Populates cells adjacent to 4 columns (A,C,E,G) of data with the same
formula
Dim c As Variant
Const sCols As String = "B,D,F,H"

For Each c In Split(sCols, ",")
Range(Cells(2, c).Offset(, -1), Cells(2, c).Offset(,
-1).End(xlDown)).Offset(, 1).Formula = "=row()*2"
Next
End Sub

Sub InsertFormulas2()
' Populates cells adjacent to 4 columns (A,C,E,G) of data with different
formulas
' Column positions and formulas are put into string variables
' Loads ad trims the variables into a 2D array

Dim va(1 To 4, 1 To 4) As String
Dim sCols As String, sFormulas As String, c As String
Dim i As Integer

sCols = "B,D,F,H,"
sFormulas = "=row()*1,=row()*2,=row()*3,=row()*4,"
'The trailing comma prevents an error on the last InStr() when trimming
the strings

'Load the array
For i = LBound(va) To UBound(va)
va(i, 1) = Left$(sCols, InStr(1, sCols, ",") - 1)
sCols = Right$(sCols, Len(sCols) - InStr(1, sCols, ","))
va(i, 2) = Left$(sFormulas, InStr(1, sFormulas, ",") - 1)
sFormulas = Right$(sFormulas, Len(sFormulas) - InStr(1, sFormulas, ","))
Next

'Populate the cells
For i = LBound(va) To UBound(va)
c = va(i, 1)
Range(Cells(2, c).Offset(, -1), Cells(2, c).Offset(,
-1).End(xlDown)).Offset(, 1).Formula = va(i, 2)
Next
End Sub
---

HTH
Regards,
Garry