Macro to Edit a Cell

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,273
Members
448,559
Latest member
MrPJ_Harper

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