Looking for simpler way to match/sort.

r.baldwin7960

New Member
Joined
Jun 25, 2008
Messages
29
I have data on a sheet (sheet3) that pulls from a web query table. This tables first column is a list of names, the rest of the columns contain the data. This information changes daily, and I create reports based off of this information.

On Sheet2 in column B, I have a master list of all the names that would appear in the table. (they only appear if they have data for that day) I then copy the table from Sheet3 to Sheet2, starting in column C.

I use the code below so that I can match/sort the names from the table to the names in the master list. This way I can have the names in the same cells each time so that I can use VLOOKUP() on Sheet1 (which I use for the reports).

I hope I haven't lost you so far...

What I'd like to know is if there is a different way of achieving the same results, or if there is a simpler way of writing the code I have so far.

Thank you again in advance!

Code:
Sub Sort()
 
 iRow = 2
 
Do Until IsEmpty(Cells(iRow, 2)) Or IsEmpty(Cells(iRow, 3))
If Cells(iRow, 2) < Cells(iRow, 3) Then
Cells(iRow, 3).Insert xlShiftDown
Cells(iRow, 4).Insert xlShiftDown
Cells(iRow, 5).Insert xlShiftDown
Cells(iRow, 6).Insert xlShiftDown
Cells(iRow, 7).Insert xlShiftDown
Cells(iRow, 8).Insert xlShiftDown
Cells(iRow, 9).Insert xlShiftDown
Cells(iRow, 10).Insert xlShiftDown
ElseIf Cells(iRow, 2) > Cells(iRow, 3) Then
Cells(iRow, 1).Insert xlShiftDown
Cells(iRow, 2).Insert xlShiftDown
End If
iRow = iRow + 1
Loop
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Another problem that I am having with this is that the section that looks like:

Code:
Cells(iRow, 3).Insert xlShiftDown
Cells(iRow, 4).Insert xlShiftDown
Cells(iRow, 5).Insert xlShiftDown
Cells(iRow, 6).Insert xlShiftDown
Cells(iRow, 7).Insert xlShiftDown
Cells(iRow, 8).Insert xlShiftDown
Cells(iRow, 9).Insert xlShiftDown
Cells(iRow, 10).Insert xlShiftDown

....actually continues on to Cells(iRow, 87).Insert........

This makes for a very long block of code. Is there any way to make this happen in one line?
 
Upvote 0
Another problem that I am having with this is that the section that looks like:

Code:
Cells(iRow, 3).Insert xlShiftDown
Cells(iRow, 4).Insert xlShiftDown
Cells(iRow, 5).Insert xlShiftDown
Cells(iRow, 6).Insert xlShiftDown
Cells(iRow, 7).Insert xlShiftDown
Cells(iRow, 8).Insert xlShiftDown
Cells(iRow, 9).Insert xlShiftDown
Cells(iRow, 10).Insert xlShiftDown

....actually continues on to Cells(iRow, 87).Insert........

This makes for a very long block of code. Is there any way to make this happen in one line?
Try
Code:
Cells(iRow, 3).Resize(, 85).Insert xlShiftDown
 
Upvote 0

Forum statistics

Threads
1,215,813
Messages
6,127,026
Members
449,352
Latest member
Tileni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top