Macro to delete cell and shift cells up

entity789

Board Regular
Joined
Jul 15, 2002
Messages
95
I'm trying to write a Macro that will delete a cell with the value of "---" and then shift all cells up. What I tried (and does not work) is...

If Cells(x, col) = "---" Then
Cells(x, col) = Delete.cell
End If


Thank you

p.s. It's painfully obvious I have no experience writing macro's :(
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This should do the trick:

Sub DeleteThem()
'This will do this for Column A down to row 10000
Dim i as Integer
For i = 1 to 10000
If Range("A" & i).Value = "--" Then
Range("A" & i).Delete Shift:=xlUp
Next i
End Sub
 
Upvote 0
Thanks for your help, but it didn't quite work as i had needed. This is a portion of the data set:
Book1
BCDE
3------------
4------------
5------------
6------------
7------------
8454433------
9479NaN442477
10NaN439439478
11NaN449436486
12442470439489
13461453439479
Sheet1


This is my macro:
Sub deletedata()

col = ActiveCell.Column

For x = 3 To 500
If Cells(x, col) = "---" Then
Cells(x, col).Select
Selection.Delete Shift:=xlUp
End If
If Cells(x, col) = "NaN" Then
Cells(x, col) = 0
End If
If Cells(x + 1, col) = "NaN" Then
Cells(x + 1, col) = 0
End If
If Abs(Cells(x, col) - Cells(x + 1, col)) > 30 Then
Cells(x, col).Interior.ColorIndex = 36
Cells(x, col).Interior.Pattern = xlSolid
Cells(x + 1, col).Interior.ColorIndex = 36
Cells(x + 1, col).Interior.Pattern = xlSolid
End If
Next x

End Sub

The first IF statement is the one not working. When removed the other three IF's work fine (by first changing any NaN to zero, then highlighting any two sequential cells with a difference of greater than 30). The problem is I need ALL cells in a column with "---" deleted and shifted up BEFORE any of the other IF statements run. Is there a way to have it select ALL the cells with "---" before any of them are deleted, THEN run the other If statements?

thanks again
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,829
Members
449,471
Latest member
lachbee

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