Macro to Edit a Cell

rhombus4

Active Member
Joined
May 26, 2010
Messages
446
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Want to create a macro which will edit cell A1currrenty click a macro button which will select dog, cat, rabbit etc and put in cell A1Once that is selected I want an option where if i need to I can click another button which will add the word food to the cell, for example I select rabbit macro then i select the food button and the cell will say rabbit food
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

rhombus4

Active Member
Joined
May 26, 2010
Messages
446
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
At the moment I have a fex boxes, when I click them it will runs a macro which will add some text to cell A1 dog cat rabbit fox etcwhat i want is another box called food so that when i click it after I have clicked one of the animals it will add the word food.Sub Cat'' Cat Macro' Macro recorded 23/07/20010'' Range("A1").Select ActiveCell.FormulaR1C1 = "Cat"End Subwhat i need is when i click the food macro the cell will then become Cat Food
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Like this?

Code:
Sub Food()
    With Range("A1")
        If Len(.Value) = 0 Then
            .Value = "Food"
        Else
            .Value = .Value & " food"
        End If
    End With
End Sub
 

rhombus4

Active Member
Joined
May 26, 2010
Messages
446
Office Version
  1. 365
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

Like this?

Code:
Sub Food()
    With Range("A1")
        If Len(.Value) = 0 Then
            .Value = "Food"
        Else
            .Value = .Value & " food"
        End If
    End With
End Sub

thanks, I also used ActiveCell.FormulaR1C1 = ActiveCell.Value & " - Food"Only problem I have with both MAcros is is there an option to only select the word once, eg, if i accidently click the food button twice it puts the word food twice
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try:

Code:
Sub Food()
    With ActiveCell
        If InStr(1, .Value, "food", vbTextCompare) = 0 Then
            If Len(.Value) = 0 Then
                .Value = "Food"
            Else
                .Value = .Value & " - Food"
            End If
        End If
    End With
End Sub
 

rhombus4

Active Member
Joined
May 26, 2010
Messages
446
Office Version
  1. 365
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

Try:

Code:
Sub Food()
    With ActiveCell
        If InStr(1, .Value, "food", vbTextCompare) = 0 Then
            If Len(.Value) = 0 Then
                .Value = "Food"
            Else
                .Value = .Value & " - Food"
            End If
        End If
    End With
End Sub

Is it possible to use the formula below to stop it using the word food twice ActiveCell.FormulaR1C1 = ActiveCell.Value & " - Food"ALso the one you posted doesnt mention cell A1 which seems to throw up an error
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Did you try the code I posted? It only adds the word food if it's not already present. It was you that mentioned ActiveCell so I changed it from Range("A1"). Change it back if you want.
 

rhombus4

Active Member
Joined
May 26, 2010
Messages
446
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I currently have:-Sub food() Range("A1").Select WithActiveCell If InStr(1, .Value, " food", vbTextCompare) = 0 Then If Len(.Value) = 0 Then .Value = " food" Else .Value = .Value & " - food" End If End If End With End SubBut it still repeats the word food if I click the button more than once
 

rhombus4

Active Member
Joined
May 26, 2010
Messages
446
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Ideally I want the macro below but with the bit about not duplicating the word foodSub food() Range("A1").Select ActiveCell.FormulaR1C1 = ActiveCell.Value & " food" Range("A1").Select End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,133,278
Messages
5,657,822
Members
418,414
Latest member
ECMdusty

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
Top