Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
velasques
 
Posts: n/a
Default inserting a conditional "go to" command on a excel "if" function

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?
  #2   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" function

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?

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
velasques
 
Posts: n/a
Default inserting a conditional "go to" command on a excel "if" functi

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?

  #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?

  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
velasques
 
Posts: n/a
Default inserting a conditional "go to" command on a excel "if" functi

Hi Gary
Well, sorry to bore you again but I have a question yet.
In the macro you wrote, when the values are different, one cell is added in
the next row; nevertheless I need to add a complete row, not just one cell.
If not the corresponding previous values in the same row will change:
BEFO
A B C D E F
1 AB aa ab ac
2 AB ba bb bc
3 CD ca cb cc
4 CD da db dc
5 EF ea eb ec
6 GH ga gb gc
7 GH ha hb hc
8

AFTER YOUR MACRO:

A B C D E F
1 AB aa ab ac
2 AB ba bb bc
3 CD ca cb cc
4 CD da db dc
5 EF ea eb ec
6 EF ga gb gc
7 GH ha hb hc
8 GH

WHAT I NEED:

A B C D E F
1 AB aa ab ac
2 AB ba bb bc
3 CD ca cb cc
4 CD da db dc
5 EF ea eb ec
6 EF
7 GH ga gb gc
8 GH ha hb hc

Again, thank you very much.


"Gary L Brown" escreveu:

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?



  #6   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

Easy fix.
Change the line...

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

to

rngCell.Offset(1, 0).EntireRow.Insert

HTH,
--
Gary Brown

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


"velasques" wrote:

Hi Gary
Well, sorry to bore you again but I have a question yet.
In the macro you wrote, when the values are different, one cell is added in
the next row; nevertheless I need to add a complete row, not just one cell.
If not the corresponding previous values in the same row will change:
BEFO
A B C D E F
1 AB aa ab ac
2 AB ba bb bc
3 CD ca cb cc
4 CD da db dc
5 EF ea eb ec
6 GH ga gb gc
7 GH ha hb hc
8

AFTER YOUR MACRO:

A B C D E F
1 AB aa ab ac
2 AB ba bb bc
3 CD ca cb cc
4 CD da db dc
5 EF ea eb ec
6 EF ga gb gc
7 GH ha hb hc
8 GH

WHAT I NEED:

A B C D E F
1 AB aa ab ac
2 AB ba bb bc
3 CD ca cb cc
4 CD da db dc
5 EF ea eb ec
6 EF
7 GH ga gb gc
8 GH ha hb hc

Again, thank you very much.


"Gary L Brown" escreveu:

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?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Should Have a History Function. Ray M. Excel Worksheet Functions 2 September 23rd 05 08:03 AM
Conditional formatting not available in Excel BAB Excel Discussion (Misc queries) 2 January 1st 05 04:33 PM
Access Module coded converted to Excel Function Adam Excel Discussion (Misc queries) 1 December 23rd 04 03:48 PM
Excel function help facilities RPS Excel Discussion (Misc queries) 1 December 8th 04 03:36 AM
I cant use englisch function names in a swedich version of excel PE Excel Discussion (Misc queries) 2 December 7th 04 02:00 AM


All times are GMT +1. The time now is 05:51 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"