help with column looping

new2vba

Board Regular
Joined
Aug 15, 2006
Messages
146
i have multiple columns with a name followed by five #'s as such:

Name
#1
#2
#3
#4
#5

i'm want to loop through each column, and when criteria are met (ex:#4>#1), copy that column to another location and sort the numbers. i have a problem thinking in vba code, and i can't figure out how to code this logic:

for each column b to e, if row 5>row2 then....

the copy and sort which follows i think i got. Any ideas?
 
ok, that's what i want...so what happens is the first column that meets the criteria is copied to a10 - that's it. in my test data, the very next column also satisfies, plus another - these two aren't copied
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Well, I don't know what's going on.
in stepping through your code:
Code:
Range("A1").Offset(10).Value
          
Mycell = Range("A1").Offset(10)
          
Mycell = Range("IV11").End(xlToLeft).Offset(0, 1)
all of these values remain the first name to meet the criteria, even if a susequent name meets the criteria.
 
Upvote 0
sample
columntry.xls
ABCDEFGH
1name1name2name3name4name5name6name7name8
20.610.430.750.770.650.850.670.67
30.610.430.740.790.640.840.670.67
40.620.440.750.820.650.850.680.68
50.60.420.730.810.640.850.670.66
60.620.430.740.820.650.850.680.67
7
Sheet1
[/code]
 
Upvote 0
sample sub

Code:
Sub Compare_Entries()
Dim Tcell As Range
Dim Mycell As Range


For Each Tcell In Range("A1", Range("A1").End(xlToRight))
    If Tcell.Offset(5).Value < Tcell.Offset(3).Value Then
        Select Case Range("A1").Offset(10).Value
                Case ""
                    Set Mycell = Range("A1").Offset(10)
                Case Is <> ""
                    Set Mycell = Range("IV11").End(xlToLeft).Offset(0, 1)
        End Select

        Tcell.Resize(6).Copy Mycell
        Mycell.Offset(1).Resize(6).Sort Key1:=Mycell.Offset(1), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        
    End If
Next Tcell

End Sub
 
Upvote 0
after sub run
columntry.xls
ABCDEFGH
1name1name2name3name4name5name6name7name8
20.610.430.750.770.650.850.670.67
30.610.430.740.790.640.840.670.67
40.620.440.750.820.650.850.680.68
50.60.420.730.810.640.850.670.66
60.620.430.740.820.650.850.680.67
7
8
9
10
11name2
120.42
130.43
140.43
150.43
160.44
17
Sheet1
 
Upvote 0
Should look like this:
columntry.xls
ABCDEFGH
1name1name2name3name4name5name6name7name8
20.610.430.750.770.650.850.670.67
30.610.430.740.790.640.840.670.67
40.620.440.750.820.650.850.680.68
50.60.420.730.810.640.850.670.66
60.620.430.740.820.650.850.680.67
7
8
9
10
11name2name3name8
120.420.730.66
130.430.740.67
140.430.740.67
150.430.750.67
160.440.750.68
Sheet1
 
Upvote 0
That's funny. I used the same code on the same data and the result was:
Book1
ABCDEFGH
1name1name2name3name4name5name6name7name8
20.610.430.750.770.650.850.670.67
30.610.430.740.790.640.840.670.67
40.620.440.750.820.650.850.680.68
50.60.420.730.810.640.850.670.66
60.620.430.740.820.650.850.680.67
7
8
9
10
11name2name3name8
120.420.730.66
130.430.740.67
140.430.740.67
150.430.750.67
160.440.750.68
Sheet1


How, exactly, are you running the code?
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,779
Members
449,123
Latest member
StorageQueen24

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