ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Deleting cells using VB (macro) (https://www.excelbanter.com/excel-programming/357173-re-deleting-cells-using-vbulletin-macro.html)

sebastienm

Deleting cells using VB (macro)
 
Hi,
Try the following:

Sub test()

Dim rgToSearch As Range, cell As Range, rgResult As Range
Dim strToSearchFor As String

'---------- CHANGE HERE ------------------------------------------------
Set rgToSearch = ActiveSheet.Range("A1:A10") 'Range to search
strToSearchFor = "C" 'String to search for

'----------------------------------------------------------------------------

'Find non-matching cells
For Each cell In rgToSearch.Cells
If cell.Text < strToSearchFor Then 'If no match
If rgResult Is Nothing Then 'Add the cell to the result range
Set rgResult = cell
Else
Set rgResult = Application.Union(rgResult, cell)
End If
End If
Next cell

'Process the result range
If Not rgResult Is Nothing Then
'rgResult.ClearContents 'clear the contents of these cells
rgResult.Delete xlShiftUp 'delete
End If
End Sub
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"maia" wrote:

Hi.
I have a macro that deletes cells from A1 till A10.
What I'd like the macro to do for me is if the macro finds a specific cell
name (string), then it doesn't delete the cells having that string. If it
doesn't, then delete every cell not cointaining that scheme.

Example: If I have the letter "C" in cell A2 and A7, I'd like the macro to
delete every other cell, ranging from A1 till A10 (excluding A2 and A7 that
has the "C" string).

Best regards



All times are GMT +1. The time now is 01:50 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com