?
Solved

Use Excel Macro to append text to cell

Posted on 2004-09-30
5
Medium Priority
?
2,627 Views
Last Modified: 2008-02-26
I would like to use a Visual Basic Macro in Excel to take the selected cell and append "//" to the beginning of any text within.

Conversly, I would like to create a macro which removes "//" from any text within the selected cell.

I also would like to move the selection to the next cell down in the row.

Thanks!

-Matthew
0
Comment
Question by:nordee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 200 total points
ID: 12190743
Hi Matthew,
Here is one macro that does both jobs--it removes the // if the cell contains it at the beginning, and adds it at the beginning otherwise. It then moves to the next cell in the same column.

Sub DoubleSlash()
Dim str As String
Dim cel As Range
Set cel = ActiveCell
If cel = "" Then Exit Sub
If Left(cel, 2) = "//" Then
    cel = Mid(cel, 3, Len(cel) - 2)
Else
    cel = "//" & cel
End If
cel.Offset(1, 0).Activate
End Sub

To install a sub or function, ALT + F11 to open the VBA Editor. Insert...Module to create a blank module sheet. Paste the code there, then ALT + F11 to return to the spreadsheet.

To run a sub, ALT + F8 to open the macro window. Select the macro, then click the Run button.

If the above procedure doesn't work, then you need to change your macro security setting. To do so, open the Tools...Macro...Security menu item. Choose Medium, then click OK.

Cheers!

Brad
0
 
LVL 6

Expert Comment

by:KingDumbNo
ID: 12191601
One _very_minor_ enhancement would be to change the line in Brad's code that reads
cel = Mid(cel, 3, Len(cel) - 2)
with this
cel = Mid(cel, 3)

It'll do the exact same thing but just less typing (and maybe less CPU, but hardly worth the effort to benchmark it).

Regards,
Emory
0
 

Author Comment

by:nordee
ID: 12193645
Excellent! That works great.

One quick question...is

Dim str as String

declaring a variable? If so, is it unused? It's the only thing in the code above I didn't understand.

Thanks.

-Matthew
0
 
LVL 81

Expert Comment

by:byundt
ID: 12194173
Matthew,
The str declaration is there because I forgot to take it out after the code was debugged.

I declare all my variables as a general principle, and start a project by declaring the ones I think I might need. Turns out that the str variable wasn't needed in the final version of the sub. You can safely delete that line of code.

The following code eliminates the extra declaration and also includes the simplified Mid function suggested by Emory:

Sub DoubleSlash()
Dim cel As Range
Set cel = ActiveCell
If cel = "" Then Exit Sub
If Left(cel, 2) = "//" Then
    cel = Mid(cel, 3)
Else
    cel = "//" & cel
End If
cel.Offset(1, 0).Activate
End Sub

Brad
0
 

Author Comment

by:nordee
ID: 12194251
Thank you for the clarification.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The main intent of this article is to make you aware of ‘Exchange fail to mount’ error, its effects, causes, and solution.
Here in this article, you will get a step by step guidance on how to restore an Exchange database to a recovery database. Get a brief on Recovery Database and how it can be used to restore Exchange database in this section!
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question