View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
JE McGimpsey JE McGimpsey is offline
external usenet poster
 
Posts: 4,624
Default Apply formula to a range with multiple selections

One way:

Can't tell exactly how your input and output ranges relate to your
Selection, but this should work:

Dim rInput As Range
Dim rOutput As Range
Dim rArea As Range
Dim rConstants As Range

Set rInput = Range(refInput.Value)
Set rOutput = Range(refOutput.Value)

With rInput
rOutput.Resize(.Rows.Count, .Columns.Count).Value = .Value
End With

For Each rArea In Selection.Areas
On Error Resume Next 'In case no constants in area
Set rConstants = rArea.SpecialCells(xlCellTypeConstants)
On Error GoTo 0
If Not rConstants Is Nothing Then
With rConstants
.Formula = "=""hello"""
.Value = .Value
End With
End If
Next rArea



In article .com,
wrote:

I'm trying to apply a formula to a range that a user can select.
Basically I have a user form where the user can select a range of data
to be processed as well as an output cell where there the
converted/processed range is put.

Once the input ranges and output cell has been chosen I need to apply a
formula to every cell in the input range and put it in the output cell.

I have been successful in doing this where the input range is only a
single selection but have been unable to get it to work with more
multiple selections as the input range. Here is my code:

Dim r As Range
Dim r2 As Range

' Input and output ranges in userform
Set r = Range(refInput.Value)
Set r2 = Range(refOutput.Value)

r.Copy
r2.PasteSpecial Paste:=xlPasteValues

Selection.SpecialCells(xlConstants).Formula = "--- formula goes
here ---"

' Get rid of formulas so user does not see them.
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues

Any way to get my code to work with multiple selections.