Solved

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

Posted on 2014-12-12
6
499 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
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 31

Accepted Solution

by:
Rob Henson earned 375 total points
Comment Utility
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
Comment Utility
To be memory conservative, instead of using Columns(4), use Columns(4).SpecialCells(xlCellTypeConstants)
0
 

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now