You are here:

Excel/Extracting data vba


Hello Damon,

Excel 2003

In sheet1, I have three columns. Column "A" contains a name. Columns "B&C" contain numbers. I cannot sort the columns because they are fed to excel in a certain format.

I need a VBA script that will identify the numbers in Column "B" between 70 and 75 and place them in descending order along with the corresponding names in Sheet2. If the numbers in Column "B" are the same the ranking should look for values in column "C".

The end result should be a list of names with values between 70 and 75 with a secondary descending ranking for Column "C".

Thank you


Hi Dave,

I believe the following macro does what you describe.  I assumed that neither of the two sheets had a heading row, but you can add space for a header row on Sheet1 by initializing the iRow loop at 2 instead of 1, and on Sheet2 by initializing dRow = 2 instead of 1.

Here's the code:

Sub FilterSort()

  Dim iRow       As Long        'Row index on source worksheet
  Dim dRow       As Long        'Row index on destination worksheet
  Dim FromSh     As Worksheet   'Source worksheet
  Dim DestSh     As Worksheet   'Destination worksheet
  Set FromSh = Worksheets("Sheet1")
  Set DestSh = Worksheets("Sheet2")
  dRow = 1
  'copy data to destination worksheet
  With FromSh
     For iRow = 1 To .Range("B65536").End(xlUp).Row
        If .Cells(iRow, "B") > 70 And .Cells(iRow, "B") < 75 Then
         .Range(.Cells(iRow, "A"), .Cells(iRow, "C")).Copy _
         Destination:=DestSh.Cells(dRow, "A")
         dRow = dRow + 1
        End If
     Next iRow
  End With
  'sort the destination worksheet data using column B as the primary
  'sort key and column C as the secondary
  With DestSh
     .Range("A1", .Cells(dRow - 1, "C")).Sort Key1:=.Range("B1"), key2:=.Range("C1"), _
        order1:=xlDescending, order2:=xlDescending
  End With
End Sub

I hope you find this helpful.

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


All Answers

Answers by Expert:

Ask Experts


Damon Ostrander


I have extensive experience with VBA programming in Excel 5 through Excel 2013. As a former aerospace engineer with a large aerospace corporation and consultant in a small defense technology services company, I have developed a wide range of applications in VBA, including simulations involving mixed-language programming, satellite orbit mechanics, graphics and animation, and real-time applications. I am interested in moderate to hard VBA-related questions only.


I have developed and taught several courses in Excel VBA programming and also VBA programming in Office 97, 2000, and 2007. I have developed a number of large technical applications in Excel VBA for use within the aerospace industry.

B.S. in Electrical Engineering and Computer Science, University of California, Berkeley.

©2016 All rights reserved.