Solved

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

Posted on 2011-03-08
3
220 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel 2016 - Edit Items in Custom Sort Level 5 19
Converting ounces to grams - simple formula 8 40
Turn date into age 17 32
Index Match Formula VBA 6 13
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

839 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