![]() |
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? |
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? |
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? |
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? |
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? |
All times are GMT +1. The time now is 03:26 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com