VBA : Replace cell IF + left/right other cells values

Coder33

New Member
Joined
Jan 24, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm quite new to the VBA game and I've been stuck on a problem.

I scrolled though the forum and did not fully find the way of solving my issue.

To be clear, I would like the Macro to insert values in a xC (cell in the column C), IF the cell xA is blank (on the same row but column A) and IF right (xB;3) is a certain value (let's say 910), based on the last two characters of xB.

This macro should also not change the values in C column (because they might be) if A is not blank.

For example here, B7 being blank and right (C7;3) being "910", I would love to see the C7 cell being "01" as per the two last characters of B7.

You'll find below the mini-sheet who may help you see clearly.
Book1.xlsx
BCD
23423422323
35565561919
44854850000
59619616969
64584588181
791001
891003
999101
101081082626
115705702121
121881883838
136236234343
141121122929
Sheet1



English not being my first language, I hope that I was clear !

Thanks in advance!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Welcome to the Board!

Does this do what you want?
VBA Code:
Sub MyInsertMacro()

    Dim lr As Long
    Dim r As Long

    Application.ScreenUpdating = False
    
'   Find last row in column C with data
    lr = Cells(Rows.Count, "C").End(xlUp).Row
    
'   Loop through all rows starting in row 2
    For r = 2 To lr
'       See if column C is blank and left of column C is "910"
        If Cells(r, "B") = "" And Left(Cells(r, "C"), 3) = "910" Then
'           Add right two characters of column C to column D
            Cells(r, "D").NumberFormat = "@"
            Cells(r, "D") = Right(Cells(r, "C"), 2)
        End If
    Next r
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

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