View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
joeu2004[_2_] joeu2004[_2_] is offline
external usenet poster
 
Posts: 829
Default Macro adds "=" to each cell in defined range across multiple worksheets

"abbruno" wrote:
I have the following macro that works great on a single
worksheet. However, I need to get this to loop through
a workbook. What is the most efficient way to do this?



Option Explicit
Sub Set_Q2_Targets()

Dim myRng As Range
Dim myCell As Range
Dim sh As Worksheet '<<<<<

For Each sh In Sheets '<<<<<
Set myRng = Nothing

On Error Resume Next
With sh '<<<<<
Set myRng = .Range("N14", .Cells(.Rows.Count, "N").End(xlUp)) _
SpecialCells(xlCellTypeConstants)
End With
On Error GoTo 0

If myRng Is Nothing Then
MsgBox "Macro Error"
Exit Sub
End If

myRng.NumberFormat = "General" '<<<< improvement
For Each myCell In myRng.Cells
With myCell
..Formula = "=" & .Value '<<<< fixed defect
End With
Next myCell
Next sh '<<<<

End Sub


If you want to do this only on selective worksheets:

Dim shName as String
For Each shName in Array("This Sheet","That Sheet","etc")
Set myRng = Nothing
On Error Resume Next
With Sheets(shName)
Set myRng = .Range(...)....
' .... rest of code ....
Next myCell
Next shName