Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I preserve the text string variable in a macro?

Posted on 2011-03-08
3
Medium Priority
?
235 Views
Last Modified: 2012-05-11
I use the attached code as part of a much larger macro.
Cell M8 is formatted as text but contains a 6 digit number sequence starting "0000", eg 000021
When the code is run, the word "DEPOSIT" is replaced, but the result in that cell is "21"
Range A14:A25 is formatted as text.
How can I preserve the string please?
Sub Macro1()
Dim DocType As String
DocType = Range("M8").Value
With Range("A14:A25")
.Replace What:="DEPOSIT", Replacement:=DocType, LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        End With
End Sub

Open in new window

0
Comment
Question by:spar-kle
3 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35067186
Try:

DocType = cstr(Range("M8").Value)

Chris
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 35067905
Or use an apostrophe:
DocType = "'" & Range("M8").Value
0
 

Author Closing Comment

by:spar-kle
ID: 35067986
Excellent, thanks for your time.
Chris, your code returned "21"
Thanks anyway
Des
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

824 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