How can I expand this code

Dougie1

Board Regular
Joined
Jul 27, 2007
Messages
212
Hi all,

Using Excel 2003

I currently am using this module of code :

Code:
Sub ID
    
    Dim Rng1, c As Range, LastRow As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Set Rng1 = Range("A1:A" & LastRow)
    For Each c In Rng1
    If UCase(c) = "1" Then
    c = c.Offset(0, 1)
    End If
    Next

End Sub

which looks at column A and finds every occurence of a 1 then offsets to pull the value from column B into column A where it has found a 1.

How would I expand this code so that it will also look at column A for every occurence of a 2, 3, 4, 5, 6, 7, 8, 9 or 10 but will offset to pull the value from column C into column A where it has found a 2, 3, 4, 5, 6, 7, 8, 9 or 10 ?

TIA

D
 

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.
How about this?

Code:
Sub ID_2()
    
    Dim Rng1, c As Range, LastRow As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Set Rng1 = Range("A1:A" & LastRow)
    For Each c In Rng1
    Select Case UCase(c)
    Case "1"
    c = c.Offset(0, 1)
    Case "2" To "10"
    c = c.Offset(0, 2)
    Case Else
    End Select
    Next

End Sub

Lee
 
Upvote 0
Something like this? Dave
Code:
Sub ID()
    
    Dim Rng1, c As Range, LastRow As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Set Rng1 = Range("A1:A" & LastRow)
    For Each c In Rng1
    If UCase(c) = "1" Then
    c = c.Offset(0, 1)
    Else
    If UCase(c) > 1 And UCase(c) < 11 Then
    c = c.Offset(0, 2)
    End If
    End If
    Next

End Sub
 
Upvote 0
Ended up using Dave's solution and it worked a treat.

As ever - this site delivers.

Thanks again guys.

All the best

D
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,676
Members
448,977
Latest member
moonlight6

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