Excel/Altering VBA

Advertisement


Question
Hi Tom

I am trying, without any success to add the first piece of code (Sub masterlistreplacement) to the end of the second piece of code (removeduplicates).  Could you help me with this please?  

Thanks in advance

Chris Mitchell

Sub masterlistreplacement()
'
' masterlistreplacement Macro
'

Dim icol As Long, lastrow As Long, i As Long
Dim scol As String

scol = "B"
icol = Range(scol & ":" & scol).Column

lastrow = Cells(Rows.Count, icol).End(xlUp).Row
Application.Calculation = xlManual
For i = 7 To 11
If Application.CountIf(Cells(1, icol).Resize(lastrow, 1), i) = 0 Then
  lastrow = lastrow + 1
  Cells(lastrow, icol).Value = i
End If
Next

   Sheets("SPtemplate2").Select
   Range("X4:AC4").Select
   Selection.ClearContents
   Sheets("Spare sheet2rng").Select
   Range("A1:AG2020").Select
   Selection.Copy
   Sheets("Sheet2rng").Select
   Range("A1").Select
   ActiveSheet.Paste
   Sheets("Choose class").Select
   Range("B4").Select
   Selection.Delete
      
End Sub

Sub removeduplicates()

Dim r As Range
Dim r1 As Range
' turn calculation to manual hopefully speed things up
Application.Calculation = xlCalculationManual

Set r = Worksheets("Sheet2").Range("A1:A2500")
Set r1 = Worksheets("Sheet2rng").Range("S2:S2500")
r.Value = r1.Value
r.removeduplicates Columns:=1, Header:=xlNo
Application.GoTo Sheets("Sheet2rng").Range("a1"), Scroll:=True

End Sub

Answer
Christopher Mitchell,

You are working on a number of sheets in your code and it is not clear to me where you want the masterlistreplacement code to do its work (what sheet).  As written below it will happen on Sheet2rng

Sub removeduplicates()

Dim r As Range
Dim r1 As Range
Dim icol As Long, lastrow As Long, i As Long
Dim scol As String

' turn calculation to manual hopefully speed things up
Application.Calculation = xlCalculationManual

Set r = Worksheets("Sheet2").Range("A1:A2500")
Set r1 = Worksheets("Sheet2rng").Range("S2:S2500")
r.Value = r1.Value
r.removeduplicates Columns:=1, Header:=xlNo
Application.GoTo Sheets("Sheet2rng").Range("a1"), Scroll:=True


' Sheet2rng is the activesheet, so the code below will work
' on column B of Sheet2rng.  If you want a different sheet,
' then add a command like   Worksheets("MySheet").Activate


scol = "B"
icol = Range(scol & ":" & scol).Column

lastrow = Cells(Rows.Count, icol).End(xlUp).Row
For i = 7 To 11
If Application.CountIf(Cells(1, icol).Resize(lastrow, 1), i) = 0 Then
 lastrow = lastrow + 1
 Cells(lastrow, icol).Value = i
End If
Next

Sheets("SPtemplate2").Range("X4:AC4").ClearContents
Sheets("Spare sheet2rng").Range("A1:AG2020").Copy _
Sheets("Sheet2rng").Range("A1")
Sheets("Choose class").Range("B4").ClearContents
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.