How do i write a Macro that copies and pastes on the first 5 characters from one column into the next column?

medwardnfriends

New Member
Joined
Jun 20, 2011
Messages
22
I have a column filled with 17 character identification numbers. I need the next column over to automatically replicate the first 8 characters from the same row in the column next to it, and also to lock this column so that the data can not be modified??
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Here is a simple one that takes the first 8 characters from all rows in column A and puts it in column B, and then locks the data (without a password).
Code:
Sub MyMacro()
 
    Dim cell As Range
    
    Application.ScreenUpdating = False
    
'   Loop through all rows in column A putting first 8 characters in column B
    For Each cell In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        cell.Offset(0, 1) = Left(cell, 8)
    Next cell
    
'   Unlock all cells, then just lock column B
    Cells.Locked = False
    Columns("B:B").Locked = True
'   Protect sheet so lock on column B takes affect (no password here, but you can lock with a password, if you like)
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    
    Application.ScreenUpdating = False
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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