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
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

the_original_invisible

Board Regular
Joined
May 27, 2004
Messages
199
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
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,723
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
 

Dougie1

Board Regular
Joined
Jul 27, 2007
Messages
212
Ended up using Dave's solution and it worked a treat.

As ever - this site delivers.

Thanks again guys.

All the best

D
 

Watch MrExcel Video

Forum statistics

Threads
1,123,281
Messages
5,600,711
Members
414,401
Latest member
grenona2020

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
Top