You are here:

# Excel/VBA extrapolate and sort

Question
QUESTION: Hi Tom.
Excel 2003 VBA solution

In Sheet1 I have three columns. Column A1:A125 contains names. Column B1:B125 contains numbers between 1 and 100, same for column C1:C125. These three columns must not change.

I need to extrapolate the names based on a sort. If a value in Column B is between 70 and 75 AND if column C values are between 10 and 30 then starting in column D1 list the names where the two conditions are true with no blank spaces.

Sub abc()
Dim v As Variant, rw As Long, i As Long

v = Range("A1:C125").Resize(125, 3)
rw = 1
For i = 1 To 125
If v(i, 2) >= 70 And v(i, 2) <= 75 And _
v(i, 3) >= 10 And v(i, 3) <= 30 Then
Cells(rw, "D").Value = v(i, 1)
rw = rw + 1
End If
Next
End Sub

worked for me if I understand the requirement.

--
Regards,
Tom Ogilvy

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

QUESTION: Hi Tom,

Macro works perfectly. I need one adjustment.

When the results are placed in column D, I need them to be sorted by their column B values and secondarily by their column C values. Currently, the results placed in column D appear in alphabetical order as they are originally.

Dave

Dave,

I assume columns E and F are available to write the values form B and C so I can sort the results.  I then clear data in columns E and F.  [if that is a problem, I can insert two new columns E and F instead and remove them after the sort]

Sub abc()
Dim v As Variant, rw As Long, i As Long
Dim r As Range

v = Range("A1:C125").Resize(125, 3)
rw = 1
For i = 1 To 125
If v(i, 2) >= 70 And v(i, 2) <= 75 And _
v(i, 3) >= 10 And v(i, 3) <= 30 Then
Cells(rw, "D").Value = v(i, 1)
Cells(rw, "E").Value = v(i, 2)
Cells(rw, "F").Value = v(i, 3)
rw = rw + 1
End If
Next
Set r = Range(Cells(1, "D"), Cells(rw - 1, "F"))
r.Sort Key1:=Cells(1, "E"), order1:=xlAscending, _
Key2:=Cells(1, "F"), order1:=xlAscending, _
r.Offset(0, 1).Resize(, 2).ClearContents

End Sub

--
Regards,
Tom Ogilvy

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

#### 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.