View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Gary L Brown
 
Posts: n/a
Default inserting a conditional "go to" command on a excel "if" functi

You would need a macro such as...

'/============================================/
Public Sub LookAtRows()
'Compare successively two values in a table,
' beginning with C1 with C2, then C3 with C4, then C5 with C6
' and so on till the end of the table.
'If the values are equal nothing to do,
' but if the values are difference a row must be inserted
' below of the first cell of that particular condition with
' the same value of that cell.

Dim rngCell As Range
Dim varAnswer As Variant, varValue As Variant

On Error GoTo err_Sub

'get the cell where the 'compare' will begin
Set varAnswer = Application.InputBox( _
Prompt:="Select the Cell to start comparing successive values." & _
vbCr & vbCr & "Hit CANCEL to stop process.", _
Title:="Insert value if only one found...", _
Default:=ActiveCell.Address, _
Type:=8)

'check for input
If varAnswer = False Or varAnswer = vbCancel Then
GoTo err_Sub
End If

varAnswer.Select

Set varAnswer = Intersect(varAnswer.Parent.UsedRange, _
varAnswer.EntireColumn)

For Each rngCell In varAnswer
If TypeName(Application.Intersect(rngCell, _
(ActiveSheet.UsedRange))) = "Nothing" Then
Exit For
End If
If rngCell.Row < 1 Then
If rngCell.Value < rngCell.Offset(-1, 0).Value And _
rngCell.Value < rngCell.Offset(1, 0).Value Then

varValue = rngCell.Value
rngCell.Offset(1, 0).Insert Shift:=xlDown
rngCell.Offset(1, 0).Value = varValue
varValue = ""

End If
End If
Next rngCell


exit_Sub:
On Error Resume Next
If varAnswer < "" Then
varAnswer = Nothing
End If
Exit Sub

err_Sub:
GoTo exit_Sub

End Sub
'/============================================/

HTH,
--
Gary Brown

If this post was helpful, please click the ''Yes'' button next to ''Was this
Post Helpfull to you?''.


"velasques" wrote:

Thanks.
...and if the condition is a variable, how do I do?
That is: I have to compare successively two values in a table, beginning
with C1 with C2, then C3 with C4, then C5 with C6, and so on till the end of
the table. If the values are equal nothing to do, but if the values are
different a row must be inserted below of the first cell of that particular
condition with the same value of that cell.
That is what I have:
A B C D E F
1 AB
2 AB
3 CD
4 CD
5 EF
6 GH
7 GH
8 IJ
€¦and that is what I need:
A B C D E F
1 AB
2 AB
3 CD
4 CD
5 EF
6 EF
7 GH
8 GH
Thanks again.

"Gary L Brown" escreveu:

From the Visual Basic Editor put something like the following code.
Obviously, change what the condition is that you are looking for from
'Range("C10").Value = 10' to your particular condition and change the name of
the macro from 'MyMacro' to your macro's name.


'/=========================================/
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("C10").Value = 10 Then
MyMacro
End If
End Sub
'/=========================================/


HTH,
--
Gary Brown

If this post was helpful, please click the ''Yes'' button next to ''Was this
Post Helpfull to you?''.


"velasques" wrote:

How do I do if, in a macro, if I want to write a condition that sends the
command pront to a specific routine, if a particular condition is verified?