Having trouble understanding the logic behind “If..Then Go To” in VBA Excel
up vote
0
down vote
favorite
I am reading through some VBA code written by someone else and I can't understand the logic behind it:
Sheets("IC View").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
If RowCount <= 9 Then GoTo skipNoChange
'Sheets("IC View").Select
Range("A1:BG1").EntireColumn.Hidden = False
Range(Cells(10, "A"), Cells(LastRowIC,
"BG")).SpecialCells(xlCellTypeVisible).Copy
Worksheets("IC Log").Select
nextRowLog = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & nextRowLog).PasteSpecial xlPasteValues
Application.CutCopyMode = False
skipNoChange:
Sheets("IC View").Select
zeroCheck = 2 'start at column 3
Do While Cells(9, zeroCheck + 1).value <> "Checked_By"
If Cells(9, zeroCheck + 1).value = "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = True
If Cells(9, zeroCheck + 1).value <> "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = False
zeroCheck = zeroCheck + 1
Loop
'==================================
Call checkFreesaleChanges
So if the row count is less than or equal to 9 then i understand it needs to go 'SkipNoChange', but where does 'SkipNoChange end? Does it end at 'zeroCheck ... Loop' and then moves on to 'Call checkFreesaleChanges'?
What happens if the row count is greater than 9? Does it continue with the code but doesn't run the bit between 'skipNoChange: ...Loop' but does run CallcheckFreesaleChanges onwards?
excel vba excel-vba loops if-statement
|
show 1 more comment
up vote
0
down vote
favorite
I am reading through some VBA code written by someone else and I can't understand the logic behind it:
Sheets("IC View").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
If RowCount <= 9 Then GoTo skipNoChange
'Sheets("IC View").Select
Range("A1:BG1").EntireColumn.Hidden = False
Range(Cells(10, "A"), Cells(LastRowIC,
"BG")).SpecialCells(xlCellTypeVisible).Copy
Worksheets("IC Log").Select
nextRowLog = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & nextRowLog).PasteSpecial xlPasteValues
Application.CutCopyMode = False
skipNoChange:
Sheets("IC View").Select
zeroCheck = 2 'start at column 3
Do While Cells(9, zeroCheck + 1).value <> "Checked_By"
If Cells(9, zeroCheck + 1).value = "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = True
If Cells(9, zeroCheck + 1).value <> "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = False
zeroCheck = zeroCheck + 1
Loop
'==================================
Call checkFreesaleChanges
So if the row count is less than or equal to 9 then i understand it needs to go 'SkipNoChange', but where does 'SkipNoChange end? Does it end at 'zeroCheck ... Loop' and then moves on to 'Call checkFreesaleChanges'?
What happens if the row count is greater than 9? Does it continue with the code but doesn't run the bit between 'skipNoChange: ...Loop' but does run CallcheckFreesaleChanges onwards?
excel vba excel-vba loops if-statement
but where does 'SkipNoChange end? it doesn't. I recommend you use if then... end if instead it makes the code easier to read in my opinion
– Andreas
Nov 8 at 10:44
@Andreas I am just trying to understand what is happening in the code as I can't change it without knowing the logic. Surely 'skipNoChange' ends somewhere because if the row count is greater than 9, then there needs to be some lines (related to skipNoChange) that aren't run?
– Sorath
Nov 8 at 10:49
No. You say to the compiler if rowcount is more than 9 "skip all this and just resume as normal from there.". There is no end to it. It's like telling someone to walk that direction.
– Andreas
Nov 8 at 10:51
@andreas skip all of what though? and resume from where? .. just reading through your answer now though :)
– Sorath
Nov 8 at 10:53
3
The current logic is just: if rowcount <= 9 then skip all the lines between here and the skipNoChange label, otherwise just run everything.
– Rory
Nov 8 at 10:56
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am reading through some VBA code written by someone else and I can't understand the logic behind it:
Sheets("IC View").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
If RowCount <= 9 Then GoTo skipNoChange
'Sheets("IC View").Select
Range("A1:BG1").EntireColumn.Hidden = False
Range(Cells(10, "A"), Cells(LastRowIC,
"BG")).SpecialCells(xlCellTypeVisible).Copy
Worksheets("IC Log").Select
nextRowLog = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & nextRowLog).PasteSpecial xlPasteValues
Application.CutCopyMode = False
skipNoChange:
Sheets("IC View").Select
zeroCheck = 2 'start at column 3
Do While Cells(9, zeroCheck + 1).value <> "Checked_By"
If Cells(9, zeroCheck + 1).value = "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = True
If Cells(9, zeroCheck + 1).value <> "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = False
zeroCheck = zeroCheck + 1
Loop
'==================================
Call checkFreesaleChanges
So if the row count is less than or equal to 9 then i understand it needs to go 'SkipNoChange', but where does 'SkipNoChange end? Does it end at 'zeroCheck ... Loop' and then moves on to 'Call checkFreesaleChanges'?
What happens if the row count is greater than 9? Does it continue with the code but doesn't run the bit between 'skipNoChange: ...Loop' but does run CallcheckFreesaleChanges onwards?
excel vba excel-vba loops if-statement
I am reading through some VBA code written by someone else and I can't understand the logic behind it:
Sheets("IC View").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
If RowCount <= 9 Then GoTo skipNoChange
'Sheets("IC View").Select
Range("A1:BG1").EntireColumn.Hidden = False
Range(Cells(10, "A"), Cells(LastRowIC,
"BG")).SpecialCells(xlCellTypeVisible).Copy
Worksheets("IC Log").Select
nextRowLog = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & nextRowLog).PasteSpecial xlPasteValues
Application.CutCopyMode = False
skipNoChange:
Sheets("IC View").Select
zeroCheck = 2 'start at column 3
Do While Cells(9, zeroCheck + 1).value <> "Checked_By"
If Cells(9, zeroCheck + 1).value = "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = True
If Cells(9, zeroCheck + 1).value <> "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = False
zeroCheck = zeroCheck + 1
Loop
'==================================
Call checkFreesaleChanges
So if the row count is less than or equal to 9 then i understand it needs to go 'SkipNoChange', but where does 'SkipNoChange end? Does it end at 'zeroCheck ... Loop' and then moves on to 'Call checkFreesaleChanges'?
What happens if the row count is greater than 9? Does it continue with the code but doesn't run the bit between 'skipNoChange: ...Loop' but does run CallcheckFreesaleChanges onwards?
excel vba excel-vba loops if-statement
excel vba excel-vba loops if-statement
asked Nov 8 at 10:42
Sorath
799
799
but where does 'SkipNoChange end? it doesn't. I recommend you use if then... end if instead it makes the code easier to read in my opinion
– Andreas
Nov 8 at 10:44
@Andreas I am just trying to understand what is happening in the code as I can't change it without knowing the logic. Surely 'skipNoChange' ends somewhere because if the row count is greater than 9, then there needs to be some lines (related to skipNoChange) that aren't run?
– Sorath
Nov 8 at 10:49
No. You say to the compiler if rowcount is more than 9 "skip all this and just resume as normal from there.". There is no end to it. It's like telling someone to walk that direction.
– Andreas
Nov 8 at 10:51
@andreas skip all of what though? and resume from where? .. just reading through your answer now though :)
– Sorath
Nov 8 at 10:53
3
The current logic is just: if rowcount <= 9 then skip all the lines between here and the skipNoChange label, otherwise just run everything.
– Rory
Nov 8 at 10:56
|
show 1 more comment
but where does 'SkipNoChange end? it doesn't. I recommend you use if then... end if instead it makes the code easier to read in my opinion
– Andreas
Nov 8 at 10:44
@Andreas I am just trying to understand what is happening in the code as I can't change it without knowing the logic. Surely 'skipNoChange' ends somewhere because if the row count is greater than 9, then there needs to be some lines (related to skipNoChange) that aren't run?
– Sorath
Nov 8 at 10:49
No. You say to the compiler if rowcount is more than 9 "skip all this and just resume as normal from there.". There is no end to it. It's like telling someone to walk that direction.
– Andreas
Nov 8 at 10:51
@andreas skip all of what though? and resume from where? .. just reading through your answer now though :)
– Sorath
Nov 8 at 10:53
3
The current logic is just: if rowcount <= 9 then skip all the lines between here and the skipNoChange label, otherwise just run everything.
– Rory
Nov 8 at 10:56
but where does 'SkipNoChange end? it doesn't. I recommend you use if then... end if instead it makes the code easier to read in my opinion
– Andreas
Nov 8 at 10:44
but where does 'SkipNoChange end? it doesn't. I recommend you use if then... end if instead it makes the code easier to read in my opinion
– Andreas
Nov 8 at 10:44
@Andreas I am just trying to understand what is happening in the code as I can't change it without knowing the logic. Surely 'skipNoChange' ends somewhere because if the row count is greater than 9, then there needs to be some lines (related to skipNoChange) that aren't run?
– Sorath
Nov 8 at 10:49
@Andreas I am just trying to understand what is happening in the code as I can't change it without knowing the logic. Surely 'skipNoChange' ends somewhere because if the row count is greater than 9, then there needs to be some lines (related to skipNoChange) that aren't run?
– Sorath
Nov 8 at 10:49
No. You say to the compiler if rowcount is more than 9 "skip all this and just resume as normal from there.". There is no end to it. It's like telling someone to walk that direction.
– Andreas
Nov 8 at 10:51
No. You say to the compiler if rowcount is more than 9 "skip all this and just resume as normal from there.". There is no end to it. It's like telling someone to walk that direction.
– Andreas
Nov 8 at 10:51
@andreas skip all of what though? and resume from where? .. just reading through your answer now though :)
– Sorath
Nov 8 at 10:53
@andreas skip all of what though? and resume from where? .. just reading through your answer now though :)
– Sorath
Nov 8 at 10:53
3
3
The current logic is just: if rowcount <= 9 then skip all the lines between here and the skipNoChange label, otherwise just run everything.
– Rory
Nov 8 at 10:56
The current logic is just: if rowcount <= 9 then skip all the lines between here and the skipNoChange label, otherwise just run everything.
– Rory
Nov 8 at 10:56
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Here is an example of a if then.... Else.... end if.
It makes the code easier to read and easier to follow in my opinion.
Adding comments to the else and end if rows means you don't need to scroll up and down to see what the else or end if is for.
Sheets("IC View").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
If RowCount > 9 Then
'Sheets("IC View").Select
Range("A1:BG1").EntireColumn.Hidden = False
Range(Cells(10, "A"), Cells(LastRowIC,
"BG")).SpecialCells(xlCellTypeVisible).Copy
Worksheets("IC Log").Select
nextRowLog = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & nextRowLog).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Else ' rowcount is less than 9
msgbox "Rowcount is less than 9"
End If ' end of if rowcount syntax
Sheets("IC View").Select
zeroCheck = 2 'start at column 3
Do While Cells(9, zeroCheck + 1).value <> "Checked_By"
If Cells(9, zeroCheck + 1).value = "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = True
If Cells(9, zeroCheck + 1).value <> "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = False
zeroCheck = zeroCheck + 1
Loop
'==================================
Call checkFreesaleChanges
1
Thank you for your help!
– Sorath
Nov 8 at 11:40
@Sorath no problem. Also another trip is to use the debugger in VBA editor. Press F8 and the code will run one line at the time. If you the use the immediate window, then you can writeRowcount = 5
and step down to your if and see what happens. Then just drag the yellow line up and inputRowcount = 15
in the immediate window and try again with F8
– Andreas
Nov 8 at 11:46
Sorry, i thought I got my head around the logic but I'm confused again! is 'Then' and 'GoTo' meant to be read together? because by your logic GoTo skipNoChange happens when rowcount >9 @andreas
– Sorath
Nov 8 at 12:19
I think you were meant to write "Run code in red if rowcount >9"
– Sorath
Nov 8 at 12:25
Yes you are right I made a mistake there @Sorath
– Andreas
Nov 8 at 13:23
add a comment |
up vote
2
down vote
skipNoChange:
is a label, a place holder. You can use any word ending thit a colon :
It's often used for error handling in VBA.
Sub DoSomething
on error goto hell
'some code here
....
Ciao:
exit Sub
hell:
msgbox "Shit happens"
resume Ciao
End Sub
add a comment |
up vote
0
down vote
GoTo Statement
Branches unconditionally to a specified line within a procedure.
Syntax GoTo line
The required line argument can be any line label or line number.
skipNoChange:
is a line label as indicated by the fact it is completely to the left and the line ends with :
and nothing after. Try and indent the label - it will move back to the left hand side of the code pane.
In your example:
If RowCount <= 9 Then GoTo skipNoChange
where RowCount
is determined by Cells(Rows.Count, 1).End(xlUp).Row
, you are saying if the last populated row in column A of the activesheet, determined by coming up from the bottom of the sheet, is <=9 then branch the code to the line label skipNoChange
. This is a conditional transfer of control. The program control shifts to the line label. The lines between this conditional test and the label are not executed as this point. If code later loops back and the condition is not met then they may in future be executed. The program continues to execute from the label onwards. It pretty much is as on tin "GoTo".
To many GoTo
s make program flow hard to follow and read. Usually you can re-write to use a different control flow structure as shown in at least one of the other answers.
It's something of a relic from before structured programming really took off. Interesting reading here.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Here is an example of a if then.... Else.... end if.
It makes the code easier to read and easier to follow in my opinion.
Adding comments to the else and end if rows means you don't need to scroll up and down to see what the else or end if is for.
Sheets("IC View").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
If RowCount > 9 Then
'Sheets("IC View").Select
Range("A1:BG1").EntireColumn.Hidden = False
Range(Cells(10, "A"), Cells(LastRowIC,
"BG")).SpecialCells(xlCellTypeVisible).Copy
Worksheets("IC Log").Select
nextRowLog = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & nextRowLog).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Else ' rowcount is less than 9
msgbox "Rowcount is less than 9"
End If ' end of if rowcount syntax
Sheets("IC View").Select
zeroCheck = 2 'start at column 3
Do While Cells(9, zeroCheck + 1).value <> "Checked_By"
If Cells(9, zeroCheck + 1).value = "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = True
If Cells(9, zeroCheck + 1).value <> "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = False
zeroCheck = zeroCheck + 1
Loop
'==================================
Call checkFreesaleChanges
1
Thank you for your help!
– Sorath
Nov 8 at 11:40
@Sorath no problem. Also another trip is to use the debugger in VBA editor. Press F8 and the code will run one line at the time. If you the use the immediate window, then you can writeRowcount = 5
and step down to your if and see what happens. Then just drag the yellow line up and inputRowcount = 15
in the immediate window and try again with F8
– Andreas
Nov 8 at 11:46
Sorry, i thought I got my head around the logic but I'm confused again! is 'Then' and 'GoTo' meant to be read together? because by your logic GoTo skipNoChange happens when rowcount >9 @andreas
– Sorath
Nov 8 at 12:19
I think you were meant to write "Run code in red if rowcount >9"
– Sorath
Nov 8 at 12:25
Yes you are right I made a mistake there @Sorath
– Andreas
Nov 8 at 13:23
add a comment |
up vote
2
down vote
accepted
Here is an example of a if then.... Else.... end if.
It makes the code easier to read and easier to follow in my opinion.
Adding comments to the else and end if rows means you don't need to scroll up and down to see what the else or end if is for.
Sheets("IC View").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
If RowCount > 9 Then
'Sheets("IC View").Select
Range("A1:BG1").EntireColumn.Hidden = False
Range(Cells(10, "A"), Cells(LastRowIC,
"BG")).SpecialCells(xlCellTypeVisible).Copy
Worksheets("IC Log").Select
nextRowLog = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & nextRowLog).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Else ' rowcount is less than 9
msgbox "Rowcount is less than 9"
End If ' end of if rowcount syntax
Sheets("IC View").Select
zeroCheck = 2 'start at column 3
Do While Cells(9, zeroCheck + 1).value <> "Checked_By"
If Cells(9, zeroCheck + 1).value = "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = True
If Cells(9, zeroCheck + 1).value <> "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = False
zeroCheck = zeroCheck + 1
Loop
'==================================
Call checkFreesaleChanges
1
Thank you for your help!
– Sorath
Nov 8 at 11:40
@Sorath no problem. Also another trip is to use the debugger in VBA editor. Press F8 and the code will run one line at the time. If you the use the immediate window, then you can writeRowcount = 5
and step down to your if and see what happens. Then just drag the yellow line up and inputRowcount = 15
in the immediate window and try again with F8
– Andreas
Nov 8 at 11:46
Sorry, i thought I got my head around the logic but I'm confused again! is 'Then' and 'GoTo' meant to be read together? because by your logic GoTo skipNoChange happens when rowcount >9 @andreas
– Sorath
Nov 8 at 12:19
I think you were meant to write "Run code in red if rowcount >9"
– Sorath
Nov 8 at 12:25
Yes you are right I made a mistake there @Sorath
– Andreas
Nov 8 at 13:23
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Here is an example of a if then.... Else.... end if.
It makes the code easier to read and easier to follow in my opinion.
Adding comments to the else and end if rows means you don't need to scroll up and down to see what the else or end if is for.
Sheets("IC View").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
If RowCount > 9 Then
'Sheets("IC View").Select
Range("A1:BG1").EntireColumn.Hidden = False
Range(Cells(10, "A"), Cells(LastRowIC,
"BG")).SpecialCells(xlCellTypeVisible).Copy
Worksheets("IC Log").Select
nextRowLog = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & nextRowLog).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Else ' rowcount is less than 9
msgbox "Rowcount is less than 9"
End If ' end of if rowcount syntax
Sheets("IC View").Select
zeroCheck = 2 'start at column 3
Do While Cells(9, zeroCheck + 1).value <> "Checked_By"
If Cells(9, zeroCheck + 1).value = "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = True
If Cells(9, zeroCheck + 1).value <> "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = False
zeroCheck = zeroCheck + 1
Loop
'==================================
Call checkFreesaleChanges
Here is an example of a if then.... Else.... end if.
It makes the code easier to read and easier to follow in my opinion.
Adding comments to the else and end if rows means you don't need to scroll up and down to see what the else or end if is for.
Sheets("IC View").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
If RowCount > 9 Then
'Sheets("IC View").Select
Range("A1:BG1").EntireColumn.Hidden = False
Range(Cells(10, "A"), Cells(LastRowIC,
"BG")).SpecialCells(xlCellTypeVisible).Copy
Worksheets("IC Log").Select
nextRowLog = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & nextRowLog).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Else ' rowcount is less than 9
msgbox "Rowcount is less than 9"
End If ' end of if rowcount syntax
Sheets("IC View").Select
zeroCheck = 2 'start at column 3
Do While Cells(9, zeroCheck + 1).value <> "Checked_By"
If Cells(9, zeroCheck + 1).value = "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = True
If Cells(9, zeroCheck + 1).value <> "" Then Columns(zeroCheck +
1).EntireColumn.Hidden = False
zeroCheck = zeroCheck + 1
Loop
'==================================
Call checkFreesaleChanges
edited Nov 8 at 13:24
answered Nov 8 at 10:49
Andreas
14.3k31441
14.3k31441
1
Thank you for your help!
– Sorath
Nov 8 at 11:40
@Sorath no problem. Also another trip is to use the debugger in VBA editor. Press F8 and the code will run one line at the time. If you the use the immediate window, then you can writeRowcount = 5
and step down to your if and see what happens. Then just drag the yellow line up and inputRowcount = 15
in the immediate window and try again with F8
– Andreas
Nov 8 at 11:46
Sorry, i thought I got my head around the logic but I'm confused again! is 'Then' and 'GoTo' meant to be read together? because by your logic GoTo skipNoChange happens when rowcount >9 @andreas
– Sorath
Nov 8 at 12:19
I think you were meant to write "Run code in red if rowcount >9"
– Sorath
Nov 8 at 12:25
Yes you are right I made a mistake there @Sorath
– Andreas
Nov 8 at 13:23
add a comment |
1
Thank you for your help!
– Sorath
Nov 8 at 11:40
@Sorath no problem. Also another trip is to use the debugger in VBA editor. Press F8 and the code will run one line at the time. If you the use the immediate window, then you can writeRowcount = 5
and step down to your if and see what happens. Then just drag the yellow line up and inputRowcount = 15
in the immediate window and try again with F8
– Andreas
Nov 8 at 11:46
Sorry, i thought I got my head around the logic but I'm confused again! is 'Then' and 'GoTo' meant to be read together? because by your logic GoTo skipNoChange happens when rowcount >9 @andreas
– Sorath
Nov 8 at 12:19
I think you were meant to write "Run code in red if rowcount >9"
– Sorath
Nov 8 at 12:25
Yes you are right I made a mistake there @Sorath
– Andreas
Nov 8 at 13:23
1
1
Thank you for your help!
– Sorath
Nov 8 at 11:40
Thank you for your help!
– Sorath
Nov 8 at 11:40
@Sorath no problem. Also another trip is to use the debugger in VBA editor. Press F8 and the code will run one line at the time. If you the use the immediate window, then you can write
Rowcount = 5
and step down to your if and see what happens. Then just drag the yellow line up and input Rowcount = 15
in the immediate window and try again with F8– Andreas
Nov 8 at 11:46
@Sorath no problem. Also another trip is to use the debugger in VBA editor. Press F8 and the code will run one line at the time. If you the use the immediate window, then you can write
Rowcount = 5
and step down to your if and see what happens. Then just drag the yellow line up and input Rowcount = 15
in the immediate window and try again with F8– Andreas
Nov 8 at 11:46
Sorry, i thought I got my head around the logic but I'm confused again! is 'Then' and 'GoTo' meant to be read together? because by your logic GoTo skipNoChange happens when rowcount >9 @andreas
– Sorath
Nov 8 at 12:19
Sorry, i thought I got my head around the logic but I'm confused again! is 'Then' and 'GoTo' meant to be read together? because by your logic GoTo skipNoChange happens when rowcount >9 @andreas
– Sorath
Nov 8 at 12:19
I think you were meant to write "Run code in red if rowcount >9"
– Sorath
Nov 8 at 12:25
I think you were meant to write "Run code in red if rowcount >9"
– Sorath
Nov 8 at 12:25
Yes you are right I made a mistake there @Sorath
– Andreas
Nov 8 at 13:23
Yes you are right I made a mistake there @Sorath
– Andreas
Nov 8 at 13:23
add a comment |
up vote
2
down vote
skipNoChange:
is a label, a place holder. You can use any word ending thit a colon :
It's often used for error handling in VBA.
Sub DoSomething
on error goto hell
'some code here
....
Ciao:
exit Sub
hell:
msgbox "Shit happens"
resume Ciao
End Sub
add a comment |
up vote
2
down vote
skipNoChange:
is a label, a place holder. You can use any word ending thit a colon :
It's often used for error handling in VBA.
Sub DoSomething
on error goto hell
'some code here
....
Ciao:
exit Sub
hell:
msgbox "Shit happens"
resume Ciao
End Sub
add a comment |
up vote
2
down vote
up vote
2
down vote
skipNoChange:
is a label, a place holder. You can use any word ending thit a colon :
It's often used for error handling in VBA.
Sub DoSomething
on error goto hell
'some code here
....
Ciao:
exit Sub
hell:
msgbox "Shit happens"
resume Ciao
End Sub
skipNoChange:
is a label, a place holder. You can use any word ending thit a colon :
It's often used for error handling in VBA.
Sub DoSomething
on error goto hell
'some code here
....
Ciao:
exit Sub
hell:
msgbox "Shit happens"
resume Ciao
End Sub
answered Nov 8 at 10:55
Patrick Honorez
18.5k563116
18.5k563116
add a comment |
add a comment |
up vote
0
down vote
GoTo Statement
Branches unconditionally to a specified line within a procedure.
Syntax GoTo line
The required line argument can be any line label or line number.
skipNoChange:
is a line label as indicated by the fact it is completely to the left and the line ends with :
and nothing after. Try and indent the label - it will move back to the left hand side of the code pane.
In your example:
If RowCount <= 9 Then GoTo skipNoChange
where RowCount
is determined by Cells(Rows.Count, 1).End(xlUp).Row
, you are saying if the last populated row in column A of the activesheet, determined by coming up from the bottom of the sheet, is <=9 then branch the code to the line label skipNoChange
. This is a conditional transfer of control. The program control shifts to the line label. The lines between this conditional test and the label are not executed as this point. If code later loops back and the condition is not met then they may in future be executed. The program continues to execute from the label onwards. It pretty much is as on tin "GoTo".
To many GoTo
s make program flow hard to follow and read. Usually you can re-write to use a different control flow structure as shown in at least one of the other answers.
It's something of a relic from before structured programming really took off. Interesting reading here.
add a comment |
up vote
0
down vote
GoTo Statement
Branches unconditionally to a specified line within a procedure.
Syntax GoTo line
The required line argument can be any line label or line number.
skipNoChange:
is a line label as indicated by the fact it is completely to the left and the line ends with :
and nothing after. Try and indent the label - it will move back to the left hand side of the code pane.
In your example:
If RowCount <= 9 Then GoTo skipNoChange
where RowCount
is determined by Cells(Rows.Count, 1).End(xlUp).Row
, you are saying if the last populated row in column A of the activesheet, determined by coming up from the bottom of the sheet, is <=9 then branch the code to the line label skipNoChange
. This is a conditional transfer of control. The program control shifts to the line label. The lines between this conditional test and the label are not executed as this point. If code later loops back and the condition is not met then they may in future be executed. The program continues to execute from the label onwards. It pretty much is as on tin "GoTo".
To many GoTo
s make program flow hard to follow and read. Usually you can re-write to use a different control flow structure as shown in at least one of the other answers.
It's something of a relic from before structured programming really took off. Interesting reading here.
add a comment |
up vote
0
down vote
up vote
0
down vote
GoTo Statement
Branches unconditionally to a specified line within a procedure.
Syntax GoTo line
The required line argument can be any line label or line number.
skipNoChange:
is a line label as indicated by the fact it is completely to the left and the line ends with :
and nothing after. Try and indent the label - it will move back to the left hand side of the code pane.
In your example:
If RowCount <= 9 Then GoTo skipNoChange
where RowCount
is determined by Cells(Rows.Count, 1).End(xlUp).Row
, you are saying if the last populated row in column A of the activesheet, determined by coming up from the bottom of the sheet, is <=9 then branch the code to the line label skipNoChange
. This is a conditional transfer of control. The program control shifts to the line label. The lines between this conditional test and the label are not executed as this point. If code later loops back and the condition is not met then they may in future be executed. The program continues to execute from the label onwards. It pretty much is as on tin "GoTo".
To many GoTo
s make program flow hard to follow and read. Usually you can re-write to use a different control flow structure as shown in at least one of the other answers.
It's something of a relic from before structured programming really took off. Interesting reading here.
GoTo Statement
Branches unconditionally to a specified line within a procedure.
Syntax GoTo line
The required line argument can be any line label or line number.
skipNoChange:
is a line label as indicated by the fact it is completely to the left and the line ends with :
and nothing after. Try and indent the label - it will move back to the left hand side of the code pane.
In your example:
If RowCount <= 9 Then GoTo skipNoChange
where RowCount
is determined by Cells(Rows.Count, 1).End(xlUp).Row
, you are saying if the last populated row in column A of the activesheet, determined by coming up from the bottom of the sheet, is <=9 then branch the code to the line label skipNoChange
. This is a conditional transfer of control. The program control shifts to the line label. The lines between this conditional test and the label are not executed as this point. If code later loops back and the condition is not met then they may in future be executed. The program continues to execute from the label onwards. It pretty much is as on tin "GoTo".
To many GoTo
s make program flow hard to follow and read. Usually you can re-write to use a different control flow structure as shown in at least one of the other answers.
It's something of a relic from before structured programming really took off. Interesting reading here.
edited Nov 8 at 13:11
answered Nov 8 at 13:05
QHarr
25.4k81839
25.4k81839
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206050%2fhaving-trouble-understanding-the-logic-behind-if-then-go-to-in-vba-excel%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
but where does 'SkipNoChange end? it doesn't. I recommend you use if then... end if instead it makes the code easier to read in my opinion
– Andreas
Nov 8 at 10:44
@Andreas I am just trying to understand what is happening in the code as I can't change it without knowing the logic. Surely 'skipNoChange' ends somewhere because if the row count is greater than 9, then there needs to be some lines (related to skipNoChange) that aren't run?
– Sorath
Nov 8 at 10:49
No. You say to the compiler if rowcount is more than 9 "skip all this and just resume as normal from there.". There is no end to it. It's like telling someone to walk that direction.
– Andreas
Nov 8 at 10:51
@andreas skip all of what though? and resume from where? .. just reading through your answer now though :)
– Sorath
Nov 8 at 10:53
3
The current logic is just: if rowcount <= 9 then skip all the lines between here and the skipNoChange label, otherwise just run everything.
– Rory
Nov 8 at 10:56