Excel VBA - code to skip lines / goto command
up vote
0
down vote
favorite
I have built a code into excel to take the data from a pivot table and insert it into a chart, a pivot chart directly linked to the table wont give me the manueverability I am looking for. The reason I have gone through the trouble of making such an 'intricate' code is that for each Plant and Test Info combination, I need it to be a separate entry into the chart.
So the main point of this code is to go through each plant and Test info combination (the nested for commands) then insert the data into the chart. My user will not be changing the column location for x and y, so the offsets work fine.
My problem is that if the plant / test info combination does not exist it enters it onto the chart anyways. When I try to use the goto command and send it to the Next PI2 using an errorhandle, it doesnt work (maybe because of the nested if commands). I was looking around trying to find a command that could send my code to a particular line in the code (i.e just after the graph commands) but didnt have any luck...
Does anyone know of a way to skip to a particular line in the event of an error?
I have added the set of commands to resume at the next PI2, where I say on error goto errhandler, then from errhandler go to the next iteration, but when I run the code and get an error it isnt going through this route, it is instead stopping at the 'intersect' line.
Sub CreatePivotChart()
Dim PF1 As PivotField
Dim PI1 As PivotItem
Dim PI2 As PivotItem
Dim PF2 As PivotField
Dim chartcount As Integer
Dim pt As PivotTable
Set pt = Worksheets("Pivot Table").PivotTables("PivotTable")
'set up pivot field locations 1 - plant and unit , 2 - test conditions
Set PF1 = Worksheets("PivotTable").PivotTables("PivotTable").PivotFields("Plant")
Set PF2 = Worksheets("Pivot Table").PivotTables("PivotTable").PivotFields("Test Info")
'clear the chart from previous run
chartcount = 0
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Chart.ChartArea.ClearContents
On Error GoTo ErrHandler
'find each visible unit
For Each PI1 In PF1.PivotItems
If PI1.Visible = True Then
Unit = PI1.Name
For Each PI2 In PF2.PivotItems
'for each unit and test condition find the information at their intersection
If PI2.Visible = True Then
TC = PI2.Name
'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If
NextIteration:
Next PI2
End If
Next PI1
Exit Sub
ErrHandler:
Resume NextIteration:
End Sub
microsoft-excel microsoft-excel-2010 vba pivot-table
add a comment |
up vote
0
down vote
favorite
I have built a code into excel to take the data from a pivot table and insert it into a chart, a pivot chart directly linked to the table wont give me the manueverability I am looking for. The reason I have gone through the trouble of making such an 'intricate' code is that for each Plant and Test Info combination, I need it to be a separate entry into the chart.
So the main point of this code is to go through each plant and Test info combination (the nested for commands) then insert the data into the chart. My user will not be changing the column location for x and y, so the offsets work fine.
My problem is that if the plant / test info combination does not exist it enters it onto the chart anyways. When I try to use the goto command and send it to the Next PI2 using an errorhandle, it doesnt work (maybe because of the nested if commands). I was looking around trying to find a command that could send my code to a particular line in the code (i.e just after the graph commands) but didnt have any luck...
Does anyone know of a way to skip to a particular line in the event of an error?
I have added the set of commands to resume at the next PI2, where I say on error goto errhandler, then from errhandler go to the next iteration, but when I run the code and get an error it isnt going through this route, it is instead stopping at the 'intersect' line.
Sub CreatePivotChart()
Dim PF1 As PivotField
Dim PI1 As PivotItem
Dim PI2 As PivotItem
Dim PF2 As PivotField
Dim chartcount As Integer
Dim pt As PivotTable
Set pt = Worksheets("Pivot Table").PivotTables("PivotTable")
'set up pivot field locations 1 - plant and unit , 2 - test conditions
Set PF1 = Worksheets("PivotTable").PivotTables("PivotTable").PivotFields("Plant")
Set PF2 = Worksheets("Pivot Table").PivotTables("PivotTable").PivotFields("Test Info")
'clear the chart from previous run
chartcount = 0
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Chart.ChartArea.ClearContents
On Error GoTo ErrHandler
'find each visible unit
For Each PI1 In PF1.PivotItems
If PI1.Visible = True Then
Unit = PI1.Name
For Each PI2 In PF2.PivotItems
'for each unit and test condition find the information at their intersection
If PI2.Visible = True Then
TC = PI2.Name
'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If
NextIteration:
Next PI2
End If
Next PI1
Exit Sub
ErrHandler:
Resume NextIteration:
End Sub
microsoft-excel microsoft-excel-2010 vba pivot-table
You mentioned error handling but I don't see any in the code. You should be able to useon error goto
see here for explanation
– gtwebb
Jun 13 '16 at 20:37
stackoverflow.com/questions/6028288/…
– Vojtěch Dohnal
Jun 14 '16 at 8:13
@gtwebb I have added in my interpretation of the goto command, but I my code is still stopping at the 'intersection', not sure why the error isnt sending it to my handler.
– Sarah Hartman
Jun 15 '16 at 11:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have built a code into excel to take the data from a pivot table and insert it into a chart, a pivot chart directly linked to the table wont give me the manueverability I am looking for. The reason I have gone through the trouble of making such an 'intricate' code is that for each Plant and Test Info combination, I need it to be a separate entry into the chart.
So the main point of this code is to go through each plant and Test info combination (the nested for commands) then insert the data into the chart. My user will not be changing the column location for x and y, so the offsets work fine.
My problem is that if the plant / test info combination does not exist it enters it onto the chart anyways. When I try to use the goto command and send it to the Next PI2 using an errorhandle, it doesnt work (maybe because of the nested if commands). I was looking around trying to find a command that could send my code to a particular line in the code (i.e just after the graph commands) but didnt have any luck...
Does anyone know of a way to skip to a particular line in the event of an error?
I have added the set of commands to resume at the next PI2, where I say on error goto errhandler, then from errhandler go to the next iteration, but when I run the code and get an error it isnt going through this route, it is instead stopping at the 'intersect' line.
Sub CreatePivotChart()
Dim PF1 As PivotField
Dim PI1 As PivotItem
Dim PI2 As PivotItem
Dim PF2 As PivotField
Dim chartcount As Integer
Dim pt As PivotTable
Set pt = Worksheets("Pivot Table").PivotTables("PivotTable")
'set up pivot field locations 1 - plant and unit , 2 - test conditions
Set PF1 = Worksheets("PivotTable").PivotTables("PivotTable").PivotFields("Plant")
Set PF2 = Worksheets("Pivot Table").PivotTables("PivotTable").PivotFields("Test Info")
'clear the chart from previous run
chartcount = 0
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Chart.ChartArea.ClearContents
On Error GoTo ErrHandler
'find each visible unit
For Each PI1 In PF1.PivotItems
If PI1.Visible = True Then
Unit = PI1.Name
For Each PI2 In PF2.PivotItems
'for each unit and test condition find the information at their intersection
If PI2.Visible = True Then
TC = PI2.Name
'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If
NextIteration:
Next PI2
End If
Next PI1
Exit Sub
ErrHandler:
Resume NextIteration:
End Sub
microsoft-excel microsoft-excel-2010 vba pivot-table
I have built a code into excel to take the data from a pivot table and insert it into a chart, a pivot chart directly linked to the table wont give me the manueverability I am looking for. The reason I have gone through the trouble of making such an 'intricate' code is that for each Plant and Test Info combination, I need it to be a separate entry into the chart.
So the main point of this code is to go through each plant and Test info combination (the nested for commands) then insert the data into the chart. My user will not be changing the column location for x and y, so the offsets work fine.
My problem is that if the plant / test info combination does not exist it enters it onto the chart anyways. When I try to use the goto command and send it to the Next PI2 using an errorhandle, it doesnt work (maybe because of the nested if commands). I was looking around trying to find a command that could send my code to a particular line in the code (i.e just after the graph commands) but didnt have any luck...
Does anyone know of a way to skip to a particular line in the event of an error?
I have added the set of commands to resume at the next PI2, where I say on error goto errhandler, then from errhandler go to the next iteration, but when I run the code and get an error it isnt going through this route, it is instead stopping at the 'intersect' line.
Sub CreatePivotChart()
Dim PF1 As PivotField
Dim PI1 As PivotItem
Dim PI2 As PivotItem
Dim PF2 As PivotField
Dim chartcount As Integer
Dim pt As PivotTable
Set pt = Worksheets("Pivot Table").PivotTables("PivotTable")
'set up pivot field locations 1 - plant and unit , 2 - test conditions
Set PF1 = Worksheets("PivotTable").PivotTables("PivotTable").PivotFields("Plant")
Set PF2 = Worksheets("Pivot Table").PivotTables("PivotTable").PivotFields("Test Info")
'clear the chart from previous run
chartcount = 0
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Chart.ChartArea.ClearContents
On Error GoTo ErrHandler
'find each visible unit
For Each PI1 In PF1.PivotItems
If PI1.Visible = True Then
Unit = PI1.Name
For Each PI2 In PF2.PivotItems
'for each unit and test condition find the information at their intersection
If PI2.Visible = True Then
TC = PI2.Name
'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If
NextIteration:
Next PI2
End If
Next PI1
Exit Sub
ErrHandler:
Resume NextIteration:
End Sub
microsoft-excel microsoft-excel-2010 vba pivot-table
microsoft-excel microsoft-excel-2010 vba pivot-table
edited Jun 15 '16 at 11:35
asked Jun 13 '16 at 18:23
Sarah Hartman
114
114
You mentioned error handling but I don't see any in the code. You should be able to useon error goto
see here for explanation
– gtwebb
Jun 13 '16 at 20:37
stackoverflow.com/questions/6028288/…
– Vojtěch Dohnal
Jun 14 '16 at 8:13
@gtwebb I have added in my interpretation of the goto command, but I my code is still stopping at the 'intersection', not sure why the error isnt sending it to my handler.
– Sarah Hartman
Jun 15 '16 at 11:24
add a comment |
You mentioned error handling but I don't see any in the code. You should be able to useon error goto
see here for explanation
– gtwebb
Jun 13 '16 at 20:37
stackoverflow.com/questions/6028288/…
– Vojtěch Dohnal
Jun 14 '16 at 8:13
@gtwebb I have added in my interpretation of the goto command, but I my code is still stopping at the 'intersection', not sure why the error isnt sending it to my handler.
– Sarah Hartman
Jun 15 '16 at 11:24
You mentioned error handling but I don't see any in the code. You should be able to use
on error goto
see here for explanation– gtwebb
Jun 13 '16 at 20:37
You mentioned error handling but I don't see any in the code. You should be able to use
on error goto
see here for explanation– gtwebb
Jun 13 '16 at 20:37
stackoverflow.com/questions/6028288/…
– Vojtěch Dohnal
Jun 14 '16 at 8:13
stackoverflow.com/questions/6028288/…
– Vojtěch Dohnal
Jun 14 '16 at 8:13
@gtwebb I have added in my interpretation of the goto command, but I my code is still stopping at the 'intersection', not sure why the error isnt sending it to my handler.
– Sarah Hartman
Jun 15 '16 at 11:24
@gtwebb I have added in my interpretation of the goto command, but I my code is still stopping at the 'intersection', not sure why the error isnt sending it to my handler.
– Sarah Hartman
Jun 15 '16 at 11:24
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
You can handle errors without goto
in VBA like this:
Sub ErrorHandling()
Dim A, d
On Error Resume Next
REM Line that throws an error
A = A / 0
REM Store details about your error before it gets cleared
d = Err.Description
On Error GoTo 0
REM You see and can handle your error message here
MsgBox d
End Sub
On Error Resume Next
Disables throwing errors
On Error GoTo 0
Enables throwing errors and clears the Err
object
add a comment |
up vote
0
down vote
I ended up answering my own questions, by continuing to look through old posts etc., I found http://www.cpearson.com/excel/errorhandling.htm to be extremely helpful.
It turned out I was trying to use two goto commands, first to goto the error handler then goto next iteration. What I needed to do was change the second goto, to resume.
Thanks for all the help guys, the above code runs perfectly!
You should test the intersection instead of capturing the error. Otherwise, you should at least test the error number for the one(s) you expect to happen. Intersect returns a range orNothing
( msdn.microsoft.com/en-us/library/office/ff835030.aspx )
– Yorik
Jun 15 '16 at 14:34
add a comment |
up vote
0
down vote
A better approach would be to test your data with an if statement to ensure your data is valid. If not, do not proceed with the code block that may generate an error.
In your example, this may work... change this:
'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
To this:
'find the information that corresponds to each unit/test condition combination
Set isect = Application.Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange)
If isect Is Nothing Then
'Msgbox "Ranges do not intersect"
Else
isect.Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If
I'm not able to test this since I don't have your workbook, but if it doesn't work it should demonstrate the approach.
thanks for the suggestion. The only thing I was worried about was nesting another if statement into the code. But it would probably be better than 'ignoring' errors.
– Sarah Hartman
Jun 15 '16 at 17:40
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1088793%2fexcel-vba-code-to-skip-lines-goto-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can handle errors without goto
in VBA like this:
Sub ErrorHandling()
Dim A, d
On Error Resume Next
REM Line that throws an error
A = A / 0
REM Store details about your error before it gets cleared
d = Err.Description
On Error GoTo 0
REM You see and can handle your error message here
MsgBox d
End Sub
On Error Resume Next
Disables throwing errors
On Error GoTo 0
Enables throwing errors and clears the Err
object
add a comment |
up vote
0
down vote
You can handle errors without goto
in VBA like this:
Sub ErrorHandling()
Dim A, d
On Error Resume Next
REM Line that throws an error
A = A / 0
REM Store details about your error before it gets cleared
d = Err.Description
On Error GoTo 0
REM You see and can handle your error message here
MsgBox d
End Sub
On Error Resume Next
Disables throwing errors
On Error GoTo 0
Enables throwing errors and clears the Err
object
add a comment |
up vote
0
down vote
up vote
0
down vote
You can handle errors without goto
in VBA like this:
Sub ErrorHandling()
Dim A, d
On Error Resume Next
REM Line that throws an error
A = A / 0
REM Store details about your error before it gets cleared
d = Err.Description
On Error GoTo 0
REM You see and can handle your error message here
MsgBox d
End Sub
On Error Resume Next
Disables throwing errors
On Error GoTo 0
Enables throwing errors and clears the Err
object
You can handle errors without goto
in VBA like this:
Sub ErrorHandling()
Dim A, d
On Error Resume Next
REM Line that throws an error
A = A / 0
REM Store details about your error before it gets cleared
d = Err.Description
On Error GoTo 0
REM You see and can handle your error message here
MsgBox d
End Sub
On Error Resume Next
Disables throwing errors
On Error GoTo 0
Enables throwing errors and clears the Err
object
edited Jun 14 '16 at 8:51
answered Jun 14 '16 at 8:43
Vojtěch Dohnal
1,81141237
1,81141237
add a comment |
add a comment |
up vote
0
down vote
I ended up answering my own questions, by continuing to look through old posts etc., I found http://www.cpearson.com/excel/errorhandling.htm to be extremely helpful.
It turned out I was trying to use two goto commands, first to goto the error handler then goto next iteration. What I needed to do was change the second goto, to resume.
Thanks for all the help guys, the above code runs perfectly!
You should test the intersection instead of capturing the error. Otherwise, you should at least test the error number for the one(s) you expect to happen. Intersect returns a range orNothing
( msdn.microsoft.com/en-us/library/office/ff835030.aspx )
– Yorik
Jun 15 '16 at 14:34
add a comment |
up vote
0
down vote
I ended up answering my own questions, by continuing to look through old posts etc., I found http://www.cpearson.com/excel/errorhandling.htm to be extremely helpful.
It turned out I was trying to use two goto commands, first to goto the error handler then goto next iteration. What I needed to do was change the second goto, to resume.
Thanks for all the help guys, the above code runs perfectly!
You should test the intersection instead of capturing the error. Otherwise, you should at least test the error number for the one(s) you expect to happen. Intersect returns a range orNothing
( msdn.microsoft.com/en-us/library/office/ff835030.aspx )
– Yorik
Jun 15 '16 at 14:34
add a comment |
up vote
0
down vote
up vote
0
down vote
I ended up answering my own questions, by continuing to look through old posts etc., I found http://www.cpearson.com/excel/errorhandling.htm to be extremely helpful.
It turned out I was trying to use two goto commands, first to goto the error handler then goto next iteration. What I needed to do was change the second goto, to resume.
Thanks for all the help guys, the above code runs perfectly!
I ended up answering my own questions, by continuing to look through old posts etc., I found http://www.cpearson.com/excel/errorhandling.htm to be extremely helpful.
It turned out I was trying to use two goto commands, first to goto the error handler then goto next iteration. What I needed to do was change the second goto, to resume.
Thanks for all the help guys, the above code runs perfectly!
answered Jun 15 '16 at 11:37
Sarah Hartman
114
114
You should test the intersection instead of capturing the error. Otherwise, you should at least test the error number for the one(s) you expect to happen. Intersect returns a range orNothing
( msdn.microsoft.com/en-us/library/office/ff835030.aspx )
– Yorik
Jun 15 '16 at 14:34
add a comment |
You should test the intersection instead of capturing the error. Otherwise, you should at least test the error number for the one(s) you expect to happen. Intersect returns a range orNothing
( msdn.microsoft.com/en-us/library/office/ff835030.aspx )
– Yorik
Jun 15 '16 at 14:34
You should test the intersection instead of capturing the error. Otherwise, you should at least test the error number for the one(s) you expect to happen. Intersect returns a range or
Nothing
( msdn.microsoft.com/en-us/library/office/ff835030.aspx )– Yorik
Jun 15 '16 at 14:34
You should test the intersection instead of capturing the error. Otherwise, you should at least test the error number for the one(s) you expect to happen. Intersect returns a range or
Nothing
( msdn.microsoft.com/en-us/library/office/ff835030.aspx )– Yorik
Jun 15 '16 at 14:34
add a comment |
up vote
0
down vote
A better approach would be to test your data with an if statement to ensure your data is valid. If not, do not proceed with the code block that may generate an error.
In your example, this may work... change this:
'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
To this:
'find the information that corresponds to each unit/test condition combination
Set isect = Application.Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange)
If isect Is Nothing Then
'Msgbox "Ranges do not intersect"
Else
isect.Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If
I'm not able to test this since I don't have your workbook, but if it doesn't work it should demonstrate the approach.
thanks for the suggestion. The only thing I was worried about was nesting another if statement into the code. But it would probably be better than 'ignoring' errors.
– Sarah Hartman
Jun 15 '16 at 17:40
add a comment |
up vote
0
down vote
A better approach would be to test your data with an if statement to ensure your data is valid. If not, do not proceed with the code block that may generate an error.
In your example, this may work... change this:
'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
To this:
'find the information that corresponds to each unit/test condition combination
Set isect = Application.Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange)
If isect Is Nothing Then
'Msgbox "Ranges do not intersect"
Else
isect.Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If
I'm not able to test this since I don't have your workbook, but if it doesn't work it should demonstrate the approach.
thanks for the suggestion. The only thing I was worried about was nesting another if statement into the code. But it would probably be better than 'ignoring' errors.
– Sarah Hartman
Jun 15 '16 at 17:40
add a comment |
up vote
0
down vote
up vote
0
down vote
A better approach would be to test your data with an if statement to ensure your data is valid. If not, do not proceed with the code block that may generate an error.
In your example, this may work... change this:
'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
To this:
'find the information that corresponds to each unit/test condition combination
Set isect = Application.Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange)
If isect Is Nothing Then
'Msgbox "Ranges do not intersect"
Else
isect.Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If
I'm not able to test this since I don't have your workbook, but if it doesn't work it should demonstrate the approach.
A better approach would be to test your data with an if statement to ensure your data is valid. If not, do not proceed with the code block that may generate an error.
In your example, this may work... change this:
'find the information that corresponds to each unit/test condition combination
Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange).Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
To this:
'find the information that corresponds to each unit/test condition combination
Set isect = Application.Intersect(pt.PivotFields("Plant").PivotItems(Unit).DataRange.EntireRow, pt.PivotFields("Test Info").PivotItems(TC).DataRange)
If isect Is Nothing Then
'Msgbox "Ranges do not intersect"
Else
isect.Select
Selection.Offset(-1, 0).Select
ForXRanges = "='Pivot Table'!" & Selection.Address
Selection.Offset(0, 1).Select
ForYRanges = "='Pivot Table'!" & Selection.Address
ForRangesName = Unit & "_" & TC
'for each combination create a new series on the chart
chartcount = chartcount + 1
Sheets("Pivot Table Graph").ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(chartcount).Name = ForRangesName
ActiveChart.SeriesCollection(chartcount).XValues = ForXRanges
ActiveChart.SeriesCollection(chartcount).Values = ForYRanges
End If
I'm not able to test this since I don't have your workbook, but if it doesn't work it should demonstrate the approach.
edited Jun 15 '16 at 13:58
answered Jun 15 '16 at 13:30
picklemonkey
464
464
thanks for the suggestion. The only thing I was worried about was nesting another if statement into the code. But it would probably be better than 'ignoring' errors.
– Sarah Hartman
Jun 15 '16 at 17:40
add a comment |
thanks for the suggestion. The only thing I was worried about was nesting another if statement into the code. But it would probably be better than 'ignoring' errors.
– Sarah Hartman
Jun 15 '16 at 17:40
thanks for the suggestion. The only thing I was worried about was nesting another if statement into the code. But it would probably be better than 'ignoring' errors.
– Sarah Hartman
Jun 15 '16 at 17:40
thanks for the suggestion. The only thing I was worried about was nesting another if statement into the code. But it would probably be better than 'ignoring' errors.
– Sarah Hartman
Jun 15 '16 at 17:40
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1088793%2fexcel-vba-code-to-skip-lines-goto-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You mentioned error handling but I don't see any in the code. You should be able to use
on error goto
see here for explanation– gtwebb
Jun 13 '16 at 20:37
stackoverflow.com/questions/6028288/…
– Vojtěch Dohnal
Jun 14 '16 at 8:13
@gtwebb I have added in my interpretation of the goto command, but I my code is still stopping at the 'intersection', not sure why the error isnt sending it to my handler.
– Sarah Hartman
Jun 15 '16 at 11:24