[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

excel macro to replace cells values and then get total unique values

Posted on 2011-03-23
12
Medium Priority
?
537 Views
Last Modified: 2012-05-11
Hi,

I need to write a macro that basically does the following -
for each row in the sheet, if the cell value in Column A exists within the cell value of Col D, then replace the Col D value with "".

I then need to get the unqiue number of values in Col D. Ive written the start of the macro but now im completely lost

any help would be great

cheers

test-macro
0
Comment
Question by:DiCanio13
[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
  • 8
  • 4
12 Comments
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35203802
Maybe something like this?

try in attached file

Curt
Sub ReplaceCells_Click()
For i = 1 To 10
If Cells(i, 1) = Cells(i, 4) Then Cells(i, 4) = ""
Next i
Cells(i, 4) = Range("d1:d10").SpecialCells(xlCellTypeConstants).Count
End Sub

Open in new window

ReplaceCells.xls
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35203844
Or maybe like this if you want the total of the remaining values in column D

try in attached file

Curt
Sub ReplaceCells_Click()
    For i = 1 To 10
        If Cells(i, 1) = Cells(i, 4) Then Cells(i, 4) = ""
    Next i
    Cells(i, 4).Activate
    ActiveCell.FormulaR1C1 = "=SUM(R[-10]C:R[-1]C)"
End Sub

Open in new window

ReplaceCells-mod1.xls
0
 

Author Comment

by:DiCanio13
ID: 35204045
Hi,

Instead of -
If Cells(i, 1) = Cells(i, 4) Then Cells(i, 4) = ""

i need If Cells(i,1) "exists in" Cells(i,4) then.....

If you understand?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35204087
Do you have text in the cells? Like "Frog" in A1 and if  "GreenFrogJumps" in D1 you want "" in D1 but not if there is just "GreenJumps" in D1.

Curt
0
 

Author Comment

by:DiCanio13
ID: 35204144
Hey Curt,

Thats correct, if i have Col A as "test" and Col D has "testing" then i want col D to end up with "ing". I then wish to sum up all the unique values left in Col D. An example would be -

Col A Col B Col C Col D
test                       testing
frog                      green frog
test                       testing

would then look like this....
Col A Col B Col C Col D
test                        ing
frog                        green
test                         ing

....and the sum of unique values in Col D would be 2

Cheers
0
 
LVL 18

Accepted Solution

by:
Curt Lindstrom earned 2000 total points
ID: 35204417
I've done the string manipulation. Is this how you want it to work?
I'm still looking at the unique counting solution

Try in attached file

Curt
Sub ReplaceCells_Click()

    Dim LeftPart As String
    Dim RightPart As String
    Dim StartPos As Long
Dim FoundLen As Long, DLen As Long
    For i = 1 To 10

        If InStr(1, Cells(i, 4), Cells(i, 1)) > 0 Then
            'Number of characters before found string
            StartPos = InStr(1, Cells(i, 4), Cells(i, 1)) - 1
            'Length of found string
            FoundLen = Len(Cells(i, 1))
            'Length of original string in column D
            DLen = Len(Cells(i, 4))
            'Characters to the left of found string
            LeftPart = Left(Cells(i, 4), StartPos)
            
            'Characters to the right of found string
            RightPart = Right(Cells(i, 4), DLen - StartPos - FoundLen)
            'Assembling the result
            Cells(i, 4) = LeftPart & RightPart
        End If
    Next i
End Sub

Open in new window

ReplaceCells-mod2.xls
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35204429
Are you using Excel 2007 or higher o does it have to work in Excel 2003?

Cheers,
Curt
0
 

Author Comment

by:DiCanio13
ID: 35204455
no excel 2007 is perfect, the above code works brilliant! now we just need the sum of the unique values....
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35204487
Just to be sure of the definition of unique:
===========
would then look like this....
Col A Col B Col C Col D
test                        ing
frog                        green
test                         ing

....and the sum of unique values in Col D would be 2
=============
So in this case you mean to count "Green" and "ing" as the 2 unique values?

Cheers,
Curt
0
 

Author Comment

by:DiCanio13
ID: 35204502
its cool, i can do the remainder using the filter -> Advanced option in excel

thanks for all your help with the initial problem
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35204509
Ok, that's what I was going to look into to build in an advance filter in the code. But if your happy with it as it is I'll stop looking at and we can close the question.

Cheers,
Curt
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35204511
Ok, closed already :-) Thanks for grading and score!

Cheers,
Curt
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

656 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