Edit Length Based on Other Column

jollywood

New Member
Joined
Oct 4, 2011
Messages
40
I have two columns, one with an account number (call it A) and another with a vendor id (call it B). I need the length of the number in column A to be truncated to 10 characters by just deleting any additional characters after 10. That should be based on whether or not the corresponding cell in column B equals the following number "147212336". Right now, my code edits the ENTIRE column A, not just the cell in the same row.

I know it's because I'm using a "FOR EACH CELL" argument but I don't what else would work.

Code:
Set acctlengthrange = Range("N2", Cells(LastRowB, "N"))
Set dplrange = Range("P2", Cells(LastRowB, "P"))

With dplrange
    If cell.Value = "147212336" Then
        For Each cell In acctlengthrange
            cell.Value = Left(cell.Value, 10)
        Next
    End If
Next
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Maybe like this

Code:
Set acctlengthrange = Range("N2", Cells(LastRowB, "N"))
Set dplrange = Range("P2", Cells(LastRowB, "P"))

For Each c In dplrange
    If c.Value = 147212336 Then
            c.Offset(, -2).Value = Left(c.Offset(, -2).Value, 10)
    End If
Next c
 
Upvote 0
That worked! Thanks for your help! Just so I understand, though. The "-2" in the offset line...is that telling the code to jump back two columns?
 
Upvote 0
Well, the previous code worked, and I thank you for that. However, some of the account numbers have leading zeros which are removed during this process...Is there a way to keep those zeros in place? Should I call on the code to format BEFORE running the code?
 
Upvote 0
Try this

Code:
For Each c In dplrange
    If c.Value = 147212336 Then
        With c.Offset(, -2)
            .Value = Left(c.Offset(, -2).Value, 10)
            .NumberFormat = "0000000000"
        End With
    End If
Next c
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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