Solved

Excel macro to select the first value in cell in a specific column

Posted on 2014-12-12
6
563 Views
Last Modified: 2014-12-12
Hello,

I'm attempting to create a VBA macro in Excel that looks at a specific column (column D) which contains values of "male" and "female".  It then needs to convert those to M and F.  I've created the following macro that does this.  However in it's current construction the user must select the values in the column.  I would like to set the macro to run for a specific column D and change the values there.  Please help.

Thanks

Sub ConvertGender()
Dim c As Range
For Each c In Selection.Cells
    c.Value = UCase(Left(c.Value, 1))
    Next
End Sub
0
Comment
Question by:wsom-itg
[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
  • 2
6 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40496464
start with:

Columns(4).select

Open in new window


after Dim c As Range

That will select column D, which I think is all that you are missing.
0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 125 total points
ID: 40496466
Alternatively, you can replace your code with:

    Columns(4).Replace What:="female", Replacement:="F", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Columns(4).Replace What:="male", Replacement:="M", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

Open in new window


This will avoid "wrong" cells like "Mole" being replaced with "M".
0
 

Author Comment

by:wsom-itg
ID: 40496472
Thanks Phillip,

I guess I should of added that it needs to have functionality to make it memory conservative, so that doesn't check the entire million+ rows of excel but only the ones with values in them
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 33

Accepted Solution

by:
Rob Henson earned 375 total points
ID: 40496474
Rather than looping each cell at a time why not do a Find & Replace?

If you do the Female to F first, you won't end up converting incorrectly or you could enable the look at whole cell option.

However selecting whole column may take longer to process. It might be worth specifying the range to speed things up:
Range("D1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Replace What:="male", Replacement:="M", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="female", Replacement:="F", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Range("A1").Select

Open in new window

Thanks
Rob H
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40496482
To be memory conservative, instead of using Columns(4), use Columns(4).SpecialCells(xlCellTypeConstants)
0
 

Author Closing Comment

by:wsom-itg
ID: 40496661
Thanks alot for the help.  Both solutions work.  I really like the Rob's find and replace
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

752 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