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?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
The following assumes 5 columns (A:E) and pastes the copied columns to the next available column after G.
Code:
Sub Compare_Entries()
Dim Tcell As Range
Dim Mycell As Range

Set Mycell = Range("G1").End(xlToRight).Offset(0, 1)

For Each Tcell In Range("A1:E1")
        If Tcell.Offset(5).Value > Tcell.Offset(2).Value Then
                Tcell.EntireColumn.Copy Mycell
        End If
Next Tcell
        
End Sub
Perhaps you can adapt it.

Mac
 
Upvote 0
thanks... the line:
Code:
Set Mycell = Range("G1").End(xlToRight).Offset(0, 1)
produces an error
 
Upvote 0
I neglected to add the Sort Method:
Code:
Sub Compare_Entries()
Dim Tcell As Range
Dim Mycell As Range

Set Mycell = Range("G1").End(xlToRight).Offset(0, 1)

For Each Tcell In Range("A1:E1")
        If Tcell.Offset(5).Value > Tcell.Offset(2).Value Then
          Tcell.EntireColumn.Copy Mycell
          Mycell.Offset(1).Resize(5).Sort Key1:=Mycell.Offset(1), Order1:=xlAscending, Header:=xlGuess, _
          OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        End If
Next Tcell

End Sub
 
Upvote 0
thanks... the line:
Code:
Set Mycell = Range("G1").End(xlToRight).Offset(0, 1)
produces an error
This will produce an error if there is no value in G1. Change the range to whatever range you're using.
 
Upvote 0
This will produce an error if there is no value in G1. Change the range to whatever range you're using.
dont quite understand what you mean here...we're assuming columns a to e have data, so f is the first available column... you say it produces an error if there is "nothing" in g - what would be in g?
 
Upvote 0
Mac...
is there a way i can adjust the copy to location? i figured out the error part - i'd like to copy the data to say a10
 
Upvote 0
Code:
Sub Compare_Entries() 
Dim Tcell As Range 
Dim Mycell As Range 

Set Mycell = Range("A10") 

For Each Tcell In Range("A1:E1") 
        If Tcell.Offset(5).Value > Tcell.Offset(2).Value Then 
          Tcell.EntireColumn.Copy Mycell 
          Mycell.Offset(1).Resize(5).Sort Key1:=Mycell.Offset(1), Order1:=xlAscending, Header:=xlGuess, _ 
          OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom 
        End If 
Next Tcell 

End Sub
 
Upvote 0
ahh, the copy entire column gets in the way of pasting below the data...the number of rows is fixed - how would i make the adjustment to copy a fixed number of rows instead of the entire column?
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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