Excel/Altering VBA

Advertisement


Question
Hi Tom

I am trying to merge these two pieces of code so that the second code runs (Sheet2rng7891011) after the first (deleterows).

Could you help me with this please?

Thanks in advance.

Chris Mitchell


Option Explicit
Option Base 1

Sub deleterows()
   
   Dim sh          As Worksheet
   Dim sh2          As Worksheet
   Dim lastrow         As Long
   Dim LastCol         As Long
   Dim i          As Long
   Dim r2          As Range  ' column A of choose class
   Dim rngToDelete     As Range
   Dim blnMatchFound   As Boolean
   Dim lngStart        As Long
   Dim varData         As Variant
   Dim varNewData      As Variant
   Dim lngPos          As Long
   Dim ilngCol         As Long

   Set sh2 = Worksheets("Choose class")
   Set r2 = sh2.Columns(1)
   Set sh = Worksheets("Sheet2rng")
   
   lngStart = Timer
'   Save initial data
   With sh
       lastrow = .Cells(sh.Rows.Count, "s").End(xlUp).Row
       LastCol = .Cells(1, sh.Columns.Count).End(xlToLeft).Column
       varData = .[A1].Resize(lastrow, LastCol)
   End With
   
'   Create empty (final) array similar to initial data array
   varNewData = varData
'   Copy heading row to final array
   lngPos = lngPos + 1
   For ilngCol = 1 To LastCol
       varNewData(lngPos, ilngCol) = varData(lngPos, ilngCol)
   Next ilngCol
'   copy all the rows that contain specified class
   For i = 2 To lastrow
       If Application.CountIf(r2, sh.Cells(i, "s").Value) <> 0 Then
         blnMatchFound = True
         lngPos = lngPos + 1
         For ilngCol = 1 To LastCol
         varNewData(lngPos, ilngCol) = varData(i, ilngCol)
         Next ilngCol
       End If
   Next
   
'   Create new list
   If blnMatchFound Then
       With sh
         .Cells.ClearContents
         .[A1].Resize(lngPos, LastCol) = varNewData
       End With
   Else
       MsgBox "No match is found", vbCritical
   End If
   Debug.Print Format(Timer - lngStart, "0.0")
   
End Sub





Sub Sheet2rng7891011()

Dim icol As Long, lastrow As Long, i As Long
Dim scol As String
Dim sh as worksheet

set sh = worksheets("Sheet2rng")
scol = "B"
icol = sh.Range(scol & ":" & scol).Column

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

Answer
Christopher Mitchell,

In this case, you would just put the Sheet2rng7891011 code after the existing code in deleterows before the End Sub

Variable declaration doesn't have to be at the top.  

Option Explicit
Option Base 1

Sub deleterows()
  
  Dim sh          As Worksheet
  Dim sh2          As Worksheet
  Dim lastrow         As Long
  Dim LastCol         As Long
  Dim i          As Long
  Dim r2          As Range  ' column A of choose class
  Dim rngToDelete     As Range
  Dim blnMatchFound   As Boolean
  Dim lngStart        As Long
  Dim varData         As Variant
  Dim varNewData      As Variant
  Dim lngPos          As Long
  Dim ilngCol         As Long

  Set sh2 = Worksheets("Choose class")
  Set r2 = sh2.Columns(1)
  Set sh = Worksheets("Sheet2rng")
  
  lngStart = Timer
'   Save initial data
  With sh
      lastrow = .Cells(sh.Rows.Count, "s").End(xlUp).Row
      LastCol = .Cells(1, sh.Columns.Count).End(xlToLeft).Column
      varData = .[A1].Resize(lastrow, LastCol)
  End With
  
'   Create empty (final) array similar to initial data array
  varNewData = varData
'   Copy heading row to final array
  lngPos = lngPos + 1
  For ilngCol = 1 To LastCol
      varNewData(lngPos, ilngCol) = varData(lngPos, ilngCol)
  Next ilngCol
'   copy all the rows that contain specified class
  For i = 2 To lastrow
      If Application.CountIf(r2, sh.Cells(i, "s").Value) <> 0 Then
        blnMatchFound = True
        lngPos = lngPos + 1
        For ilngCol = 1 To LastCol
        varNewData(lngPos, ilngCol) = varData(i, ilngCol)
        Next ilngCol
      End If
  Next
  
'   Create new list
  If blnMatchFound Then
      With sh
        .Cells.ClearContents
        .[A1].Resize(lngPos, LastCol) = varNewData
      End With
  Else
      MsgBox "No match is found", vbCritical
  End If
  Debug.Print Format(Timer - lngStart, "0.0")
 

Dim icol As Long, lastrow As Long, i As Long
Dim scol As String
Dim sh as worksheet

set sh = worksheets("Sheet2rng")
scol = "B"
icol = sh.Range(scol & ":" & scol).Column

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

another way would be just to call you second routine from your first

Option Explicit
Option Base 1

Sub deleterows()
  
  Dim sh          As Worksheet
  Dim sh2          As Worksheet
  Dim lastrow         As Long
  Dim LastCol         As Long
  Dim i          As Long
  Dim r2          As Range  ' column A of choose class
  Dim rngToDelete     As Range
  Dim blnMatchFound   As Boolean
  Dim lngStart        As Long
  Dim varData         As Variant
  Dim varNewData      As Variant
  Dim lngPos          As Long
  Dim ilngCol         As Long

  Set sh2 = Worksheets("Choose class")
  Set r2 = sh2.Columns(1)
  Set sh = Worksheets("Sheet2rng")
  
  lngStart = Timer
'   Save initial data
  With sh
      lastrow = .Cells(sh.Rows.Count, "s").End(xlUp).Row
      LastCol = .Cells(1, sh.Columns.Count).End(xlToLeft).Column
      varData = .[A1].Resize(lastrow, LastCol)
  End With
  
'   Create empty (final) array similar to initial data array
  varNewData = varData
'   Copy heading row to final array
  lngPos = lngPos + 1
  For ilngCol = 1 To LastCol
      varNewData(lngPos, ilngCol) = varData(lngPos, ilngCol)
  Next ilngCol
'   copy all the rows that contain specified class
  For i = 2 To lastrow
      If Application.CountIf(r2, sh.Cells(i, "s").Value) <> 0 Then
        blnMatchFound = True
        lngPos = lngPos + 1
        For ilngCol = 1 To LastCol
        varNewData(lngPos, ilngCol) = varData(i, ilngCol)
        Next ilngCol
      End If
  Next
  
'   Create new list
  If blnMatchFound Then
      With sh
        .Cells.ClearContents
        .[A1].Resize(lngPos, LastCol) = varNewData
      End With
  Else
      MsgBox "No match is found", vbCritical
  End If
  Debug.Print Format(Timer - lngStart, "0.0")

Sheet2rng7891011   '<== executes the sheet2rng7891011 routine
  
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.