views:

292

answers:

2

I am struggling to carry out the following. Any help would be greatly appreciated.

The marco only relates to one excel worksheet.

I have 50 rows of information, 11 columns wide (A - K) in each row of column A there sits an IF condition If the IF condition flags the word 'OK' I would like a macro to trigger, and carry out the following to copy the information in cells B to K, and paste special it back into those cells.

And then move onto the next row

The 50 rows of information will fill up over the course of a day.

Each ROW condition in column A is executed only ONCE, starting with ROW 1 and working it's way down to ROW 50

The trigger event 'OK' will come, it is just a matter of waiting for it to happen.

As I say any help would be greatly appreciated

+1  A: 

Use the Change event of the sheet, see if the target is in the A column. If it is check to see if it equals 'OK', then execute the macro on that row.

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub

On Error Resume Next
Dim TestRange As Range
Set TestRange = Target.Dependents

If TestRange.HasFormula And Err.Number = 0 Then
    If Left$(TestRange.Address, 2) = "$A" Then
        Application.EnableEvents = False

        If TestRange.Value = "OK" Then
            Call GreatMacro(TestRange)
        End If

        Application.EnableEvents = True
    End If
End If

End Sub
Lance Roberts
Hugh
There are a few ways. I'll modify the code I gave you to Call GreatMacro(Target), then your GreatMacro will have a parameter of a range (say RowAddress), then your select statment will be RowAddress.Columns("B:G").Select.
Lance Roberts
Thanks Lance, Will you post the modified code? In terms of the execution of it will be very resource hungry when processing? and secondly if 4 cells come up with 'OK' will just process one row at a time, or will it try to do everything at once and lock up. Thanks again for taking the time out to post.
Hugh
Also forgot to add Lance, if the first row to start from is A6, does a starting point need to be added or will the code just look down column A until it comes across a CELL with 'OK' in
Hugh
ah yes and.. just to re-iterate the 'OK' in column A is a result of an =IF condition within that CELL being met.
Hugh
You'll have to test it on simultaneous IFs, I would think the code could handle it, but haven't had to face that situation myself. The starting point doesn't matter, since the Target is always whatever cell got changed. I've already edited my code (only had to change the parameter). I've found that this kind of simple event/macro stuff works very quickly, shouldn't be any problem.
Lance Roberts
Hi Lance, put the code in and still coming up with the error 1) the macro is called GreatMacro (), do I need to change it to GreatMacro (Target)? and 2) I have the macro positioned just below your code in the Module -is this what is causing the problem
Hugh
The macro (you can name it whatever you want) needs to have a Range parameter like so: Public Sub GreatMacro(RowAddress As Range)
Lance Roberts
ok will put that in -came across the following in my seach for a solution.If C6 is a formula then change it to thisPrivate Sub Worksheet_Change(ByVal Target As Range)Dim rng As RangeIf Target.Cells.Count > 1 Then Exit SubOn Error GoTo EndMacroIf Not Target.HasFormula ThenSet rng = Target.DependentsIf Not Intersect(Range("C6"), rng) Is Nothing ThenIf Target.Value <> "" ThenRange("D6").Value = Target.ValueEnd IfEnd IfEnd IfEndMacro:End SubRegards Ron de Bruinhttp://www.rondebruin.nl/tips.htmhttp://www.pcreview.co.uk/forums/thread-3636981.php
Hugh
Thought it might be of interest -seeing as CELL A is triggered by a condition
Hugh
Hi Lance, got it to work, however only when I type in 'OK', I think the formula does cause a problem -how would I incorporate the above code into your code?
Hugh
We'll have to use some of the concepts he uses, but I need to know what your cell formula is that becomes "OK".
Lance Roberts
In COLUMN EO the cells have the formula =IF(EN6>EM6,"OK","") and the rows that need copying and then paste special are EP:EY. Thanks so much for your effort and input, very much appreciated
Hugh
OK, I've edited to code to work for that specific situation, though you'll need to do some testing.
Lance Roberts
Fantastic, many thanks Lance, will do some testing and report back
Hugh
Hi Lance, notice that the CALL is for GreatMacro(TestRange) whereas the other macro is called GreatMacro(Rowaddress As Range) -do I need to change anything?
Hugh
No, TestRange is the Range your sending to the subroutine, Rowaddress is what you're calling it inside the subroutine.
Lance Roberts
Actually Lance would it be better rather than CALL ing upon a macro because it is so short, could we not add it into your code -the code in GreatMacro was just Rowaddress.Columns("B:G").Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False
Hugh
The reason for the above post, is that I'm having trouble getting it to run
Hugh
Yes, you could just do that. Just change Rowaddress to TestRange.
Lance Roberts
Be aware when testing this code, that if it faults out in the middle of the subroutine, you have to reset Application.EnableEvents = True (in the immediate Window, or you could close and reopen Excel).
Lance Roberts
Magic -thanks Lance will do
Hugh
If it works do me a favor and upvote and accept the answer.
Lance Roberts
Hi Lance it looks good, many thanks for both your help and time -very much appreciated.
Hugh
Hi Lance, seems to keep stopping, is there a way of getting the macro to start at CELL A7 and then wait until it gets 'OK', once it does it copies and pastes special and then moves down 1 row and waits there, and to keep doing this until it reaches a cell with 999 in?
Hugh
oh yes one other additional point it keeps stopping on the line Set TestRange = Target.Dependents, hence the reason for the request above I thought by forcing it to start at a point and then wait for an 'OK' before being directed to the next cell/row would cure this
Hugh
When you say stop, do you mean with an error, or is it just doing nothing?
Lance Roberts
It appears that the problem might be when the Target doesn't have any dependents. There isn't much documentation out there on how to test to see if Target has any dependents, though you could always drop out on an error, but I'll do some research to see if there's a better way.
Lance Roberts
OK, I set it up so that Target.Dependents would be tested using exceptions. Go ahead and see if that solves your problem. Let me know.
Lance Roberts
+2  A: 

This page shows how you can set a macro to run on cell change.
Basically, the method is to over-use the worksheet change function, and if the cell is the correct one, you let the function run.

Try to keep as much as possible inside the if-this-is-correct-cell check to make sure that too much code isn't executing every single time you make a worksheet change.

Andy
Thanks for the post Andy, thought I'd replied earlier apologies for that.
Hugh