Solved

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

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

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

734 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