macro to delete certain cells in a sheet

Joke

Board Regular
Joined
Jan 21, 2004
Messages
171
Hi,

Im having a sheet with a number of cells to enter data which are spread all over the page. I included an erase'button with macro to clear content of all cells where data is entered so users can use the same entry form again.
The sheet is protected and users can only enter de cells where data have to be entered.

The clumsy thing is that everytime we want to change the format of the sheet including the position of the datacells, I have to redo the entire macro because references in the macro are to each dataentry cell.

Is there a way to make a macro to select only the data entry cells on basis of the format (color) and then clear the content of these cells. I have tried to name the cells but then I have to include about 40 different names which is also rather clumsy.

Any tips or suggestions?

Thanks,

Joke
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Joke said:
Hi,

Im having a sheet with a number of cells to enter data which are spread all over the page. I included an erase'button with macro to clear content of all cells where data is entered so users can use the same entry form again.
The sheet is protected and users can only enter de cells where data have to be entered.

The clumsy thing is that everytime we want to change the format of the sheet including the position of the datacells, I have to redo the entire macro because references in the macro are to each dataentry cell.

Is there a way to make a macro to select only the data entry cells on basis of the format (color) and then clear the content of these cells. I have tried to name the cells but then I have to include about 40 different names which is also rather clumsy.

Any tips or suggestions?

Thanks,

Joke

You can use a single range name such as EraseCells. Use Ctrl F3 to start the name and Use the Ctrl key to click on the individual cell or cells to be erased. When you GOTO that range name each of the cells are selected and a single delete key erases all. You should be able to incorporate that into your macro. This is the same as setting several cells or ranges when using =SUM(A1,A4.A5)

The named cells will look like this

=Sheet5!$A$3,Sheet5!$A$5,Sheet5!$C$10
 
Upvote 0
or:

Is there a way to make a macro to select only the data entry cells on basis of the format (color) and then clear the content of these cells.

Code:
Sub erasecolorvalues()
    Dim rwIndex, colIndex As Integer

    For rwIndex = 1 To 27 ' rows 1 to 27
        For colIndex = 1 To 10 ' columns A to J
            With Worksheets("Sheet7").Cells(rwIndex, colIndex)
' colorindex for green is either 4 or 35 depending on which green
                If .Interior.ColorIndex = 4 Then .ClearContents
            End With
        Next colIndex
    Next rwIndex
End Sub
 
Upvote 0
Excellent, the namng solution works very well!! only that the number of ranges you can add under one name has a limit. When it becomes to much the selected ranges just diseappear. So I had to use three names to cover all the cells.

I still have to try the macro but would you know by chance where can i find information on the numbers of all the colors.

In addition to the erasing I also have to fill a couple of cells with 0%. I tried naming them but then the macro only put 0% in the first cell. So apparently I should name them all individual to include them in the macro.

Any more tip on that one and thanks a lot for the help,

Joke
 
Upvote 0
Joke said:
Excellent, the namng solution works very well!! only that the number of ranges you can add under one name has a limit. When it becomes to much the selected ranges just diseappear. So I had to use three names to cover all the cells.

I still have to try the macro but would you know by chance where can i find information on the numbers of all the colors.

In addition to the erasing I also have to fill a couple of cells with 0%. I tried naming them but then the macro only put 0% in the first cell. So apparently I should name them all individual to include them in the macro.

Joke

You ran into a limit that Excel has of 32 entries i.e. If you do an =SUM(Cell1,Cell2,etc) Excel will only accept 32 entries. You have to do this(similar to your range names)

=SUM(Cell1,Cell2,etc)+SUM(Cell1,Cell2,etc) Most Excel version will not even give you an error message except to tell you that your SUM formula is incorrect.

For the Color Palette go into VBA, click on help and type in PatternColorIndex Property. It will show you the default color palette with 56 colors and associated color number.

If your code looks something like this when it rune the Selection.Value = "0%" all cells in the range name get 0% entered.

Sub EraseCells()
Application.Goto Reference:="EraseCells"
Range("EraseCells").Select
Selection.Value = "0%"
End Sub

Hope this helps. Post again if needed.
 
Upvote 0
excellent works perfect! what I record all ranges in a cell on the side

=$C$1,$C$3,$H$2:$h$6,..........etc using the CTRL button

with about 40 references and afterwards I copy and paste this text inside the NAME window and apparently then EXCEL doesnt have a problem anymore with the large naming ranging.

THANKS again,
Joke
 
Upvote 0
Joke,
...the number of ranges you can add under one name has a limit. When it becomes to much the selected ranges just diseappear. So I had to use three names to cover all the cells.
Combine your three named ranges into one master range, say MyMaster. Assuming that your three named ranges are rng1, rng2 and rng3:

Insert menu | Name | Define
Names in workbook: MyMaster
Refers to: “=rng1,rng2,rng3” (no quotes – separate the individual ranges with a comma).
Add
OK

Then you can use MyMaster as follows:
Code:
Sub macro4()
     Range("MyMaster").ClearContents
End Sub
……where can I find information on the numbers of all the colors.
See:
http://www.geocities.com/davemcritchie/excel/colors.htm

or run this macro in a new workbook:
Code:
Sub colorCode()
 c = 1
 For i = 1 To 8
  For j = 1 To 7
  Cells(i, j).Select
    With Selection.Interior
     .ColorIndex = c
    ActiveCell.Value = c
    End With
    c = c + 1
  Next j
 Next i
End Sub
Regards,

Mike
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,922
Members
449,094
Latest member
teemeren

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