I tried to work with macros that should
- sort p-values in column H
- delete all the rows with p-values
>=0.05 - fill column I with "B - E", I mean
I2=B2-E2;I3=B3-E3et cetera - sort by column I
This macros was created by Jeeped:
Sub sort1()
Dim m As Variant
With ActiveSheet
'delete rows 1:6
.Range("1:6").EntireRow.Delete Shift:=xlUp
'new column header for column I
Range("I1") = "diff"
'sort A:I on column H (ascending)
With .Range("A:I")
.Sort Key1:=.Columns(8), Order1:=xlAscending, Header:=xlYes
End With
'find >=0.05
m = Application.Match(0.05, .Range("H:H"), 0)
If IsError(m) Then m = Application.Match(0.05, .Range("H:H"))
'delete rows (>=0.05):<bottom of worksheet>
.Range(.Cells(m, "A"), .Cells(.rows.Count, "A")).EntireRow.Delete Shift:=xlUp
'new formula for column I data range
.Range("I2:I" & m - 1).FormulaR1C1 = "=RC[-7]-RC[-4]"
'calculate (actually unnecessary, putting in new formulas forces true calculation)
.Calculate
'sort A:I on column I (ascending)
With .Range("A:I")
.Sort Key1:=.Columns(9), Order1:=xlAscending, Header:=xlYes
End With
End With
End Sub
It worked before but now it stops on the string
.Range(.Cells(m, "A"), .Cells(.rows.Count, "A")).EntireRow.Delete Shift:=xlUp
It says
Run-time error '13': Type mismatch
Please help!
Without knowing the data that this sub is processing it is difficult to guess why are you getting that error. However, my bet is that the
Application.Matchfunction can't find any values in columnHthat are less than or equal to0.05, som = #N/Aand you can't pass#N/Ato the.Cells()property.If that is the case (you can check if it is in the immediate window after the error appears), try deleting the rows and sorting only if
MATCHwas successful:P.S, your code currently doesn't do exactly what you said all the times:
You wanted to delete all rows that are bigger than or equal to
0.05, but if you didn't have a value that is exactly equal to0.05, and had values like:(0, ..., 0.048, 0.049, 0.051, 0.052, ...)in yourHcolumn, then this line of code:will find the largest value that is less than or equal to
0.05, which is0.049in our hypothetical data.Then this line of code:
will delete the rows which had the values from
0.049to the end (0.049included).You code is deleting one extra value more than what you wanted, but since you said it worked before as you wanted, I didn't change it.
If you want to correct this behavior, you need to add one to
mthe second time you useMATCH: