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
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

jollywood

New Member
Joined
Oct 4, 2011
Messages
40
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?
 

jollywood

New Member
Joined
Oct 4, 2011
Messages
40

ADVERTISEMENT

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?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,561
Messages
5,596,849
Members
414,107
Latest member
Tigretto

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