insert zero's with code?

cheveley

Active Member
Joined
Sep 24, 2002
Messages
273
i would like to insert a zero in each cell in the range L74:P75 with code but i can only do this the long way for each cell by recording the macro.

could anyone suggest some code that would do this neatly?

much obliged
 

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,758
Office Version
  1. 365
Platform
  1. Windows
Hi

You could put a command button on the sheet (I've done it on sheet1)

right click the button and make the code thus:
Code:
Private Sub CommandButton1_Click()

For Each c In Sheets("sheet1").Range("L74:P75")
    c.Value = "0"
Next
End Sub
That should get you started.
 

cheveley

Active Member
Joined
Sep 24, 2002
Messages
273
Thanks. My range is called "pefix" and i have changed the macro so that it forms part of a large macro but I am running into problems. my code is:

Range ("pefix")
c.Value = "0"
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,758
Office Version
  1. 365
Platform
  1. Windows
.......in that case, just use this bit........

Code:
For Each c In Sheets("sheet1").Range("pefix")
    c.Value = "0"
Next
......... obviously change the sheet reference to suit...

PS I presume it's not meant to read "Prefix" is it?
 

cheveley

Active Member
Joined
Sep 24, 2002
Messages
273

ADVERTISEMENT

works a charm. thanks.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
46,822
Office Version
  1. 365
Platform
  1. Windows
Looping is generally fairly slow and most VBA gurus on this board recommend avoiding loops if possible. Try this code:

<font face=Courier New>    Application.Goto Reference:="pefix"
    Selection.FormulaR1C1 = "0"</FONT>
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
46,822
Office Version
  1. 365
Platform
  1. Windows
Or just:

Code:
Range("Prefix").Value = 0
Come on Richard, can't you make it simpler than that!! :devilish:
 

Watch MrExcel Video

Forum statistics

Threads
1,113,849
Messages
5,544,645
Members
410,627
Latest member
georgealice
Top