Solved

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

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 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