Excel - A macro to delete a particular cell value

Issue

I have a requirement to delete value in specific set of cells in excel using VBA script.

Eg: I need to delete d3:d4,e3:e4,g3:g4,h3:h4

and another set d9:d11,e9:e11,g9:g11,h9:h11.

sub delete I am trying this way sheets("sheet1ange("d3:e4).select selectionlearcontents sheets("sheet1ange("g3:h4).select selectionlearcontents . . . . end sub

But this way redundancy is seen. Can some one please help me in writing a better code.

Solution

Create a common routine and call that routine

'rowToClearStarting : first row in the range that needs to be cleared 'rowToClearEnding : last row in the range that needs to be cleared 'Sample Call : clearContents(4,6) Public Sub clearContents(rowToClearStarting As Long, rowToClearEnding As Long) Sheets("sheet1").Range("d" & rowToClearStarting & ":d" & rowToClearEndinglearContents Sheets("sheet1").Range("e" & rowToClearStarting & ":e" & rowToClearEndinglearContents Sheets("sheet1").Range("g" & rowToClearStarting & ":g" & rowToClearEndinglearContents Sheets("sheet1").Range("h" & rowToClearStarting & ":h" & rowToClearEndinglearContents End Sub

Thanks to Rrizvisa1 for this tip.

Hunter Jones

Hunter Jones

Next Post

Leave a Reply

Your email address will not be published. Required fields are marked *