Excel - uma macro para excluir um valor de célula específico

Questão

Eu tenho um requisito para excluir o valor no conjunto específico de células no Excel usando o script VBA.

Por exemplo: eu preciso excluir d3: d4, e3: e4, g3: g4, h3: h4

e outro conjunto d9: d11, e9: e11, g9: g11, h9: h11.

 sub delete Eu estou tentando assim folhas ("sheet1"). range ("d3: e4) .selecione selection.clearcontents sheets (" sheet1 "). range (" g3: h4) .selecione selection.clearcontents. . . . end sub 

Mas desta forma a redundância é vista. Alguém por favor pode me ajudar a escrever um código melhor?

Solução

Crie uma rotina comum e chame essa rotina

 'rowToClearStarting: primeira linha no intervalo que precisa ser limpo' rowToClearEnding: última linha no intervalo que precisa ser limpo 'Sample Call: clearContents (4, 6) Public Sub clearContents (rowToClearStarting Como Long, rowToClearEnding Como Longo) Sheets ( "sheet1"). Range ("d" e rowToClearStarting & ": d" & rowToClearEnding) .clearContents Sheets ("sheet1"). Range ("e" & rowToClearStarting & ": e" & rowToClearEnding) .clearContents Sheets ("planilha1" ") .Range (" g "& rowToClearStarting &": g "& rowToClearEnding) .clearContents Sheets (" sheet1 "). Range (" h "& rowToClearStarting &": h "& rowToClearEnding) .clearContents End Sub 

Obrigado a Rrizvisa1 por essa dica.

Artigo Anterior Próximo Artigo

Principais Dicas