Need macro code please to add a word to beginning of a cell

Henner

Board Regular
Joined
May 16, 2002
Messages
174
Hi,

It would be appreciated if someone could provide me with macro code to add the word DEC at the beginning of every cell in Column C, but only when the 3rd character in a cell in Column A is a letter of the alphabet.

For example:

Column A Column B Column C
06N0104 This is a test

BECOMES

Column A Column B Column C
06N0104 DEC This is a test


Thanks
Paul
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

gaynard_nelson

Active Member
Joined
Dec 4, 2002
Messages
323
Re: Need macro code please to add a word to beginning of a c

Henner said:
Hi,

It would be appreciated if someone could provide me with macro code to add the word DEC at the beginning of every cell in Column C, but only when the 3rd character in a cell in Column A is a letter of the alphabet.

For example:

Column A Column B Column C
06N0104 This is a test

BECOMES

Column A Column B Column C
06N0104 DEC This is a test

Thanks
Paul

You could enter a formula in Col C like this

DEC This is a Test =IF(ISTEXT(MID(A6,3,1)),"DEC This is a Test","")
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473
Re: Need macro code please to add a word to beginning of a c

Well, this works but I'm not sure the For-Next/Select Case is the best structure --

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> AddDEC()
<SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, LastRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">With</SPAN> ActiveSheet
    LastRow = .[a65536].End(3).Row
    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> LastRow
        <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> Mid(.Cells(i, 1), 3, 1)
            <SPAN style="color:#00007F">Case</SPAN> "a" <SPAN style="color:#00007F">To</SPAN> "z", "A" <SPAN style="color:#00007F">To</SPAN> "Z"
                .Cells(i, 3) = "DEC " & .Cells(i, 3)
            <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> i
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,101
Messages
5,768,100
Members
425,453
Latest member
bince

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