Change gender and modify some words accordingly.

harzer

Board Regular
Joined
Dec 15, 2021
Messages
159
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,
In column "A", all my cells end with "M" for Male and "F" for Female.
In column "J", All cells start with "Male" when the last character in column "A" is an "M", and start with "Female" when the last character in column "A" is "F".
Still in column "J", each cell begins with "Male or "Female" followed by several information on the subject which is in column "A".
Information: It is not always easy to determine the exact sex especially when my birds are young, hence the need to change the sex in column "A" and column "J" thereafter.
When the sex is definitely determined, then, I start by selecting the cell(s) in column "A" of the subjects whose sex I want to change via a button, By clicking on this button, if the subject is mentioned as a Male ( So, the column cell "A" ends with "M") and I want to change the sex, so we replace the "M" with "F", likewise, if the subject is mentioned as a Female (so , the cell in column "A" ends with an "F"), then, we replace the "F" by "M".
The code below makes it possible to implement what I have just described.

VBA Code:
Sub Changer_Sexe()
    '''select one or more cells and then replace "M" with "F" and "F" with "M"
    Application.ScreenUpdating = False
    For Each c In Selection
        If Right(c.Value, 1) = "M" Then
            c.Value = Left(c.Value, Len(c.Value) - 1) & "F"
        ElseIf Right(c.Value, 1) = "F" Then
            c.Value = Left(c.Value, Len(c.Value) - 1) & "M"
        End If        'fin de test pour changement de sexe
    Next c
    Application.ScreenUpdating = True
End Sub

This is where I would like your help, namely:
I also want to change the sex in column "J", replacing the word "Male" or "Female" at the beginning of the cells in column "J" depending of course on the sex changed in column "A".

My table is quite large, that's why I allow myself to ask you for a code that can make the changes quickly.

I hope that I am clear in my explanations, if not, I remain at your disposal for other additional information.
Thanks in advance for your contributions.

Here is the working table in reduced format.

Classeur1.xlsm
ABCDEFGHIJK
1JeunePèreMèreEleveurAgeVolièreCageNé(e)ToursInformationElevage
2AETYTT-093/2012 MAED27-093/2012 M27-093/2012 MGérard Claude10a 11m 28j2B16410684TMâle Tt Jaune (Belle Posture)
3AE27-094/2012 MAE27-094/2012 MAET27-094/2012 MGérard Claude10a 11m 28j2B16410684TMale avec tâche au dosX
4AEY27-100/2012 FAER27-100/2012 FE27-100/2012 FGérard Claude10a 11m 26j2B13410704TFemelle Toute Jaune (très belle)
5GAE27-059/2013 FAEGG27-059/2013 FAE27-059/2013 FGérard Claude10a 1m 11j3H13413894TFemelle toute jaune (Magnifique)X
6AE27-060/2013 MAE27-060/2013 MAE27-060/2013 MGérard Claude10a 0m 31j5H19413995TMâle Panaché
7AUE27-087/2013 FAE27-087/2013 FAE-087/2013 FGérard Claude10a 0m 18j5H22414124TFemelle tâche à la têteX
8AE27-011/2019 FAEJ27-011/2019 FRG27-011/2019 FGérard Claude4a 1m 14j4H11435775TFemelle Tâche au dosX
9AE27-012/2019 FAE27-012/2019 FHT-012/2019 FGérard Claude4a 1m 14j5H11435775TFemelle Tte JauneX
10AE27-013/2019 MAE27-013/2019 MAE27-013/2019 MGérard Claude4a 1m 14j4H1435774TMâle (très beau)X
11AE27-014/2019 FAEG27-014/2019 FA-014/2019 FGérard Claude4a 1m 16j4H2435754TFemelle Belle Taille
12AEGYT-015/2019 MG-015/2019 MTT7-015/2019 MGérard Claude4a 1m 17j4H6435744TMâle beau ChantX
Feuil1


EDIT:
I forgot to add a small detail concerning the cells in column "J", if the cells in column "J" do not begin with the word "Male" or "Female" (The words "Male" or "Female" are absent ), then we add the word adapted to the sex of the subjects who are in the cells in column "A".
Good programming.
 
Last edited by a moderator:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Here is a modified version of your changer_sexe Sub (Macro). Will this work?
 
Upvote 0
VBA Code:
Sub Changer_Sexe()
    Dim sheet As Worksheet
    Dim rnginfo As Range
    Dim r
    r = 0
    '''select one or more cells and then replace "M" with "F" and "F" with "M"
    Application.ScreenUpdating = False
    Set sheet = Selection.Parent
    For Each c In Selection
        If c.Row <> r Then  'Change Gender Mâle or Femelle once in each row of selection
          r = c.Row
          Set rnginfo = sheet.Range("J" & r)
          tmp = Split(rnginfo, " ", 2)
          If UBound(tmp) >= 1 Then
            gender = tmp(0)
            Select Case gender
              Case "Mâle": gender = "Femelle"
              Case "Femelle": gender = "Mâle"
            End Select
            tmp(0) = gender
            rnginfo = Join(tmp, " ")
          End If
        End If
        If Right(c.Value, 1) = "M" Then
            c.Value = Left(c.Value, Len(c.Value) - 1) & "F"
        ElseIf Right(c.Value, 1) = "F" Then
            c.Value = Left(c.Value, Len(c.Value) - 1) & "M"
        End If        'fin de test pour changement de sexe
    Next c
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hello Bosquedeguate,
Thank you for your feedback and your code.
The code works well and gives me the desired result, however, there is still a case to implement in the code, I mentioned this case in a second message that I sent after my request and that probably you do not haven't seen, let me explain:
It sometimes happens (See Often!) that the sex "Male" or "Female" is not indicated at the beginning of the cell of the column "J", in this case how to add the sex at the beginning of the cell "J" according to gender are in column "A.
I hope I am clear in my explanations.
Happy programming and looking forward to reading from you.
 
Upvote 0
Hello Bosquedeguate,
Thank you for your feedback and your code.
The code works well and gives me the desired result, however, there is still a case to implement in the code, I mentioned this case in a second message that I sent after my request and that probably you do not haven't seen, let me explain:
It sometimes happens (See Often!) that the sex "Male" or "Female" is not indicated at the beginning of the cell of the column "J", in this case how to add the sex at the beginning of the cell "J" according to gender are in column "A.
I hope I am clear in my explanations.
Happy programming and looking forward to reading from you.
OK
Code to add (prepend) gender to values in Column J it is not present.
VBA Code:
Sub Changer_Sexe()
    Dim sheet As Worksheet
    Dim rnginfo As Range
    Dim r
    Dim mf
    Dim female
    female = "Femelle"
    Dim male
    male = "Mâle"
    
    r = 0
    '''select one or more cells and then replace "M" with "F" and "F" with "M"
    Application.ScreenUpdating = False
    Set sheet = Selection.Parent
    For Each c In Selection
        mf = Right(c.Value, 1) 'get trailing "M" or "F" from cell
        If c.Row <> r Then  'Change Gender Mâle or Femelle once in each row of selection
          r = c.Row
          Set rnginfo = sheet.Range("J" & r)
          tmp = Split(rnginfo, " ", 2)
          If UBound(tmp) >= 1 Then
            gender = tmp(0)
            Select Case gender
              Case "Mâle": gender = female
              Case "Femelle": gender = male
              'Note MF are flipped in Choose array because we're changing gender
              'Add gender when "Mâle" or "Femelle" if not present
              Case Else: gender = Choose(InStr(1, "MF", mf), female, male) & " " & tmp(0)
            End Select
            tmp(0) = gender
            rnginfo = Join(tmp, " ")
          End If
        End If
        If mf = "M" Then
            c.Value = Left(c.Value, Len(c.Value) - 1) & "F"
        ElseIf mf = "F" Then
            c.Value = Left(c.Value, Len(c.Value) - 1) & "M"
        End If        'fin de test pour changement de sexe
    Next c
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hello Bosquedeguate,
Thank you for the good proposal that works very well and gives me total satisfaction.
Thank you also for your availability and for sharing your knowledge.
Friendly greetings.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top