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
 
That's because you have a space before food in:

If InStr(1, .Value, " food", vbTextCompare) = 0 Then

In my code it was:

If InStr(1, .Value, "food", vbTextCompare) = 0 Then
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
That's because you have a space before food in:

If InStr(1, .Value, " food", vbTextCompare) = 0 Then

In my code it was:

If InStr(1, .Value, "food", vbTextCompare) = 0 Then

Thanks, But if I wanted to use the macro I currently have not sure where to slot our above macroSub food() Range("A1").Select ActiveCell.FormulaR1C1 = ActiveCell.Value & " food" Range("A1").Select End Sub
 
Upvote 0
Is this what you want?

Code:
Sub Food()
    With Range("A1")
        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
It cases a debug and highlightsIf InStr(1, .Value, "food", vbTextCompare) = 0 Then
 
Upvote 0
What error do you get?

Sorry seems to be ok now, when I kept editing the previous macros I kept amending the titles slightly and it kept jumping to an incorrect one

Is it best to just open Visual Basic Editor when doing macros and paste what you enter, as I tend to just record macros manually then edit them later.



Also it was data on another sheet which may have got me confused then go back to original sheet

Sub Food()
Sheets("Sheet2").Select
With Range("A1")
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
Sheets("Sheet1").Select
Range("A2").Select

End Sub

Thanks for all yor help
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,581
Messages
6,125,658
Members
449,247
Latest member
wingedshoes

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