Excel/merging macros

Advertisement


Question
QUESTION: Hi Tom

I seem to be accumulating lots of macros.  I would like to incorporate two of them into one.  The first macro “Sub replacezero()” has an error.  I have tried to rewrite it so that it incorporates several worksheets: “PON7” to “PON8”.  There is something wrong with my syntax.

Ideally I would like the macro “SUB replacezero()” incorporated into another macro that you wrote for me: Public Sub ABC()

Is this possible?

Sub replacezero()
Dim cell As Range
For Each cell In Worksheets("PON7", "PON8", "PON9", "PON10", "PON11").Range("A1:AE250")
If Trim(cell.Text) = "0" Or cell.Text = "" Or cell.Text = " " Then
 cell.Value = "_"
End If
Next
End Sub



Public Sub ABC()
Dim v As Variant, s As String
Dim sh As Worksheet, srng As String
Dim i As Long, r As Range
Set sh = Worksheets("termlyassessment")
v = Array("B", "E", "H", "K", "N", "Q")
s = "XX3:XX1000"
For i = LBound(v) To UBound(v)
srng = Replace(s, "XX", v(i))
Set r = sh.Range(srng)
If Application.CountIfs(r, "<>0", r, "<>_") > 3 Then
r.Formula = r.Value
Range("W1").Select
   Selection.ClearContents

End If
Next
End Sub

Thanks in advance

Chris Mitchell

ANSWER: Sub replacezero()
Dim cell As Range
v = Array("PON7", "PON8", "PON9", "PON10", "PON11")
for i = lbound(v) to Ubound(v)
set sh = worksheets(v(i))
For Each cell In Sh.Range("A1:AE250")
If Trim(cell.Text) = "0" Or cell.Text = "" Or cell.Text = " " Then
cell.Value = "_"
End If
Next cell
Next i
End Sub


If you want to loop through the same set of sheets and perform both actions then this would be my guess as to how to combine them:

Public Sub ABC()
Dim v As Variant, s As String, vsh as Variant
Dim sh As Worksheet, srng As String
Dim i As Long, r As Range, j as long

vSh = Array("PON7", "PON8", "PON9", "PON10", "PON11")
v = Array("B", "E", "H", "K", "N", "Q")
s = "XX3:XX1000"

for j = lbound(vsh) to Ubound(vsh)
Set sh = Worksheets(vsh(j))
For i = LBound(v) To UBound(v)
srng = Replace(s, "XX", v(i))
Set r = sh.Range(srng)
If Application.CountIfs(r, "<>0", r, "<>_") > 3 Then
r.Formula = r.Value
End If
Next
' I am not sure where W1 is located, but if it is on each
' sheet in the list then
  sh.Range("W1").ClearContents

For Each cell In Sh.Range("A1:AE250")
If Trim(cell.Text) = "0" Or cell.Text = "" Or cell.Text = " " Then
cell.Value = "_"
End If
Next cell


End Sub

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Hi Tom

All of your solutions do exactly what I want them to do.  However, this issue is arising.  Let's say I have identical worksheets (worksheet A) in two separate workbooks (workbook1 and workbook2)

The information in both worksheets covers cells A1:AE1000.  When I apply the code the worksheet A in workbook 1 will refresh and update almost instantly.  Whereas in worksheet A in workbook 2 will constantly recalculate one cell at a time making the VBA inefficient.  

Workbook 1 contains only three worksheets, one of which is worksheet A.  Whereas workbook 2 has many worksheets one of which is worksheet A.

Could you shed some light on this speed anomaly.

The code I am applying is:

Sub replaceblanks()
Dim cell as Range
for each cell in Worksheets("Sheet2").Range("A1:AE1000")
 if Trim(cell.Text) = "0" then
   cell.Value = "_"
 end if
Next
End Sub


Thanks in advance

Chris Mitchell

Answer
Christopher Mitchell,

I would assume the bigger workbook requires much more recalculation (has more formulas and therefor perhaps more dependencies).  What I would do would be to make calculation manual before running the macro and then set it back to automatic after.

You could have the code do it

Sub replaceblanks()
Dim cell as Range
' add code at the top to make calculation manual
Application.Calculation = xlManual
for each cell in Worksheets("Sheet2").Range("A1:AE1000")
if Trim(cell.Text) = "0" then
  cell.Value = "_"
end if
Next
' add code at the bottom to make calculation automatic
Application.Calculation = xlAutomatic
End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.