Macros: insert value and remove rows

hello9000

New Member
Joined
May 18, 2011
Messages
4
Hi,
I have two different codes that I am working on, and need som help figuring out.

#1: I have this Macro-code, it is intended to add the value 1, to column S if the corresponding column Q have value that is <= 1000 and >= 9999 (equal to or between 1000-9999).

So far this code only add the value 1 to the S column if the corresponding Q column is exactly 1000 or 9999, how can this code be modified to also inched the values between 1000-9999?

Code:
With Range("s4:s" & LastRow)
        t = .Offset(, 1).Address
        .Offset(, -2).Value = Evaluate("=if(len(" & t &  "),LEFT(" & t & ",SEARCH("" ""," & t & "&""  "")-1)+0," & """"")")
        s = .Address
        q = .Offset(, -2).Address
        .FormulaR1C1 = "=if(or(rc[-2]<=1000,rc[-2]>=9999),1,0)"
        .Value2 = .Value2
    End With
#2: I have this Macro-code that is intended to remove all rows that does not contain the word "ARG1" and "ARG2" in column A.

Code:
Sub Clear_test()

Set r = Range(Range("A4"), Range("A65535").End(xlDown))
For Each c In r

If Trim(c) <> "ARG1" And Trim(c) <> "ARG2" Then c.EntireRow.Delete
Next c

End Sub
So far this code seams to run in an internal loop, that never ends.. but it does remove the unwanted rows, just does not stop running!
And the numbers of rows in my sheet is not a fixed value. For now it contains almost 5000 rows but I think it will grow until 16000 at the end of this year!

Please help!

progress.gif
 
Last edited by a moderator:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi hello9000,

In the first code you could try changing:

Code:
.FormulaR1C1 = "=if(or(rc[-2]<=1000,rc[-2]>=9999),1,0)"

To:

Code:
.FormulaR1C1 = "=if(or(rc[-2]>=1000,rc[-2]<=9999),1,0)"

And for the second code you may want to try:

Code:
Sub Clear_test()
 
Set r = Range(Range("A4"), Range("A65535").End(xlDown))
For  a= r to 4 step-1
 
If Cells(a, 1) <> "ARG1" And Cells(a, 1) <> "ARG2" Then Rows(a & ":" & a).EntireRow.Delete
Next c
 
End Sub
 
Upvote 0
Yes, the first sugestion did the job! Thank you :) About the second code you gave me, I keep getting "Invalide Next control variable reference"-Error, have tried changing Next c to Next a, then I get an error in the: "For a = r To 4 Step -1" line.
 
Upvote 0
Hi

Sorry I should have tested it, try this code.

Code:
R = Range("A" & Rows.Count).End(xlUp).Row
For a = R To 4 Step -1
If Cells(a, 1) <> "ARG1" And Cells(a, 1) <> "ARG2" Then Rows(a & ":" & a).EntireRow.Delete
Next a
 
Upvote 0
It is very strange, but after this code has ran for abount 3min, all the cells from row 4 and down is deleted, in every column!!
 
Upvote 0
H1 hello9000,

If I send you a PM with my email add, are you able to send me an example of the data, so I can see what is happening.
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,421
Members
452,913
Latest member
JWD210

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