D
Dennis
The following code works except for the IF clause.
The idea is to select a range of cells on the active worksheet.
Those selected cells contain formulas that are to be encapsulated by another
formula.
Using the VBA Debugger in Excel 2003, I noted that "Range" in the Dim
statement is not in Blue print as are the others like "String."
The point of my concern is that the value for myCellRange is "Nothing"
according to the debugger.
Therefore, the MsgBox and Exit occur.
If I comment out the If clause, the Macro works unless I do not select cells
with formulas in them.
My idea was to trap that error with the IF clause.
Why is the value of myCellRange always "Nothing" even when cells are selected?
How can I trap the non selection of cells error yet still have the macro work?
TIA Dennis
Sub ReplaceFormula()
Dim CellFormula As String
Dim myCellRange As Range
Dim Placeholder As String
Dim NewFormula As String
If myCellRange Is Nothing Then MsgBox "No Cells were selected!"
If myCellRange Is Nothing Then
Exit Sub
Else
NewFormula = Application.InputBox("Enter the New Formula _
with a Placeholder for the Cell Formula. _
(Start with a single quote like '=X+Y)")
Placeholder = Application.InputBox("What variable in the New _
Formula, entered in the previous Screen, is to be replaced _
with Cell Formula?")
For Each myCellRange In Intersect(Selection, _
Selection.SpecialCells(xlCellTypeFormulas))
CellFormula = Mid(myCellRange.Formula, 2, _
Len(myCellRange.Formula)) _
myCellRange.Formula = Mid(Replace(NewFormula, _
Placeholder, CellFormula), 2)
Next myCellRange
End If
End Sub
The idea is to select a range of cells on the active worksheet.
Those selected cells contain formulas that are to be encapsulated by another
formula.
Using the VBA Debugger in Excel 2003, I noted that "Range" in the Dim
statement is not in Blue print as are the others like "String."
The point of my concern is that the value for myCellRange is "Nothing"
according to the debugger.
Therefore, the MsgBox and Exit occur.
If I comment out the If clause, the Macro works unless I do not select cells
with formulas in them.
My idea was to trap that error with the IF clause.
Why is the value of myCellRange always "Nothing" even when cells are selected?
How can I trap the non selection of cells error yet still have the macro work?
TIA Dennis
Sub ReplaceFormula()
Dim CellFormula As String
Dim myCellRange As Range
Dim Placeholder As String
Dim NewFormula As String
If myCellRange Is Nothing Then MsgBox "No Cells were selected!"
If myCellRange Is Nothing Then
Exit Sub
Else
NewFormula = Application.InputBox("Enter the New Formula _
with a Placeholder for the Cell Formula. _
(Start with a single quote like '=X+Y)")
Placeholder = Application.InputBox("What variable in the New _
Formula, entered in the previous Screen, is to be replaced _
with Cell Formula?")
For Each myCellRange In Intersect(Selection, _
Selection.SpecialCells(xlCellTypeFormulas))
CellFormula = Mid(myCellRange.Formula, 2, _
Len(myCellRange.Formula)) _
myCellRange.Formula = Mid(Replace(NewFormula, _
Placeholder, CellFormula), 2)
Next myCellRange
End If
End Sub