Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel Replace Items

Posted on 2011-03-14
7
Medium Priority
?
213 Views
Last Modified: 2012-06-27
Hi Experts,

Can someone please tell me if it possible to replace more than one item in excel?

For example, if I wanted to replace the numbers 5,4,3,2,1 with 1,2,3,4,5 can I do that in one shot?

At the moment, I would have to replace 5, with 1, then go back in and replace 4 with 2 etc...


Cheers

Carlton
0
Comment
Question by:cpatte7372
[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
7 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35127903
No you can't. And doing it one at a time will be tricky since you'll end up replacing items multiple times - e.g. you convert 5 to 1, then later you convert all 1s to 5s!
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 35128374
Your problem will come when you get past 3.....

Replace all 5 with 1
Replace all 4 with 2
3 = 3 anyway
Replace all 2 with 4 !!

Hold on!! All the ones that were 4 became 2! Now you dont know what 2 is a 2 and what used to be 4!

Needs a bit more thinking behind this one.
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35128735
>>>>Can someone please tell me if it possible to replace more than one item in excel?

Yes it is possible via VBA Code. Let me create a sample for you.

Sid
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:nettek0300
ID: 35128790
If the numbers are in a single cell, they you can do a find and replace.  Type 5,4,3,2,1 in the find and 1,2,3,4,5 in the replace field.  You can do each one individually by clicking replace, or all at one time with replace all.
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 2000 total points
ID: 35129056
Ok Here is the sample. Please note that for realistic situation, you will have to change the code. The logic that I am using is to replace the values of the numbers with their ASCII values and then doing a final replace. I also took the liberty to create multiple sample data i.e I pasted 1-5 multiple times.

Hope this is what you wanted?

Sid

Code Used

Option Explicit

Private Sub CommandButton1_Click()
    Dim rng As Range, aCell As Range, bCell As Range
    Dim ws As Worksheet
    Dim ReplaceChar As Long, i As Long, j As Long
    Dim ExitLoop As Boolean
    
    Set ws = Sheets("Sheet1")
    
    Set rng = ws.Range("A1:A15")
    
    For Each aCell In rng
        aCell.Value = Asc(aCell.Value)
    Next
    
    j = 53
    For i = 49 To 53
        ReplaceChar = Chr(j)
        j = j - 1
        Set aCell = rng.Find(What:=i, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
        ExitLoop = False
        If Not aCell Is Nothing Then
            Set bCell = aCell
            aCell.Value = ReplaceChar
            Do While ExitLoop = False
                Set aCell = rng.FindNext(After:=aCell)
                If Not aCell Is Nothing Then
                    If aCell.Address = bCell.Address Then Exit Do
                    aCell.Value = ReplaceChar
                Else
                    ExitLoop = True
                End If
            Loop
        End If
    Next i
End Sub

Open in new window



Sample.xls
0
 

Author Comment

by:cpatte7372
ID: 35129901
Sid,

That is exactly what I wanted.

You've come through again.

Cheers man.
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35130000
Glad to be of help :)

Sid
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

609 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