Solved

Use Excel Macro to append text to cell

Posted on 2004-09-30
5
2,615 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
  • 2
  • 2
5 Comments
 
LVL 80

Accepted Solution

by:
byundt earned 50 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 80

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
GFI and SCCM 2012 5 62
Best Online Bookmark Organiser 6 64
Win 7 OS unable to install Win updates 3 87
Denali Software 2 18
Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Viewers will learn how to use the Hootsuite Dashboard.
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now