“The item with the specified name wasn't found” error when trying to group shapes on a worksheet
I am writing a VBA program to selectively group some rounded rectangle shapes. There are going to be sets of these groups, so I want to store them in an array. (For e.g. I would like to have dataSeriesGroup(1) to have a group of say three rounded rectangles, dataSeriesGroup(2) to have a group of three other rounded rectangles, and so on). I am trying to assign them to the group using the .Name attribute, as follows:
Dim ctr, ctr2, seriesCount, dataCount as Integer
Dim dataSeriesGroup() as Shape
Dim dataPoint() as Shape
Dim dTop, dLeft, dWidth, dHeight as long
Dim dataPointName as Variant
<Bunch of code to calculate values of dTop, dLeft, dWidth, dHeight, seriesCount, dataCount>
Redim dataSeriesGroup(seriesCount)
Redim dataPoint(dataCount, dataSeriesCount)
Redim dataPointName(dataCount)
For ctr = 1 to seriesCount
For ctr2 = 1 to dataCount
Set dataPoint(ctr2, ctr) = ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangle, dLeft, dTop, dWidth, dHeight)
dataPointName(ctr2) = dataPoint(ctr2, ctr).Name
Next ctr2
Set dataSeriesGroup(ctr) = Activesheet.Shapes(Array(dataPointName)).Group
Next ctr
Everything is working fine, but when I am trying to set the dataSeriesGroup(ctr) I am getting an error "Run-time error '-2147352571 (80020005)': The item with the specified name wasn't found."
Can someone please provide some guidance as to what I am doing wrong?
excel vba shapes
add a comment |
I am writing a VBA program to selectively group some rounded rectangle shapes. There are going to be sets of these groups, so I want to store them in an array. (For e.g. I would like to have dataSeriesGroup(1) to have a group of say three rounded rectangles, dataSeriesGroup(2) to have a group of three other rounded rectangles, and so on). I am trying to assign them to the group using the .Name attribute, as follows:
Dim ctr, ctr2, seriesCount, dataCount as Integer
Dim dataSeriesGroup() as Shape
Dim dataPoint() as Shape
Dim dTop, dLeft, dWidth, dHeight as long
Dim dataPointName as Variant
<Bunch of code to calculate values of dTop, dLeft, dWidth, dHeight, seriesCount, dataCount>
Redim dataSeriesGroup(seriesCount)
Redim dataPoint(dataCount, dataSeriesCount)
Redim dataPointName(dataCount)
For ctr = 1 to seriesCount
For ctr2 = 1 to dataCount
Set dataPoint(ctr2, ctr) = ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangle, dLeft, dTop, dWidth, dHeight)
dataPointName(ctr2) = dataPoint(ctr2, ctr).Name
Next ctr2
Set dataSeriesGroup(ctr) = Activesheet.Shapes(Array(dataPointName)).Group
Next ctr
Everything is working fine, but when I am trying to set the dataSeriesGroup(ctr) I am getting an error "Run-time error '-2147352571 (80020005)': The item with the specified name wasn't found."
Can someone please provide some guidance as to what I am doing wrong?
excel vba shapes
add a comment |
I am writing a VBA program to selectively group some rounded rectangle shapes. There are going to be sets of these groups, so I want to store them in an array. (For e.g. I would like to have dataSeriesGroup(1) to have a group of say three rounded rectangles, dataSeriesGroup(2) to have a group of three other rounded rectangles, and so on). I am trying to assign them to the group using the .Name attribute, as follows:
Dim ctr, ctr2, seriesCount, dataCount as Integer
Dim dataSeriesGroup() as Shape
Dim dataPoint() as Shape
Dim dTop, dLeft, dWidth, dHeight as long
Dim dataPointName as Variant
<Bunch of code to calculate values of dTop, dLeft, dWidth, dHeight, seriesCount, dataCount>
Redim dataSeriesGroup(seriesCount)
Redim dataPoint(dataCount, dataSeriesCount)
Redim dataPointName(dataCount)
For ctr = 1 to seriesCount
For ctr2 = 1 to dataCount
Set dataPoint(ctr2, ctr) = ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangle, dLeft, dTop, dWidth, dHeight)
dataPointName(ctr2) = dataPoint(ctr2, ctr).Name
Next ctr2
Set dataSeriesGroup(ctr) = Activesheet.Shapes(Array(dataPointName)).Group
Next ctr
Everything is working fine, but when I am trying to set the dataSeriesGroup(ctr) I am getting an error "Run-time error '-2147352571 (80020005)': The item with the specified name wasn't found."
Can someone please provide some guidance as to what I am doing wrong?
excel vba shapes
I am writing a VBA program to selectively group some rounded rectangle shapes. There are going to be sets of these groups, so I want to store them in an array. (For e.g. I would like to have dataSeriesGroup(1) to have a group of say three rounded rectangles, dataSeriesGroup(2) to have a group of three other rounded rectangles, and so on). I am trying to assign them to the group using the .Name attribute, as follows:
Dim ctr, ctr2, seriesCount, dataCount as Integer
Dim dataSeriesGroup() as Shape
Dim dataPoint() as Shape
Dim dTop, dLeft, dWidth, dHeight as long
Dim dataPointName as Variant
<Bunch of code to calculate values of dTop, dLeft, dWidth, dHeight, seriesCount, dataCount>
Redim dataSeriesGroup(seriesCount)
Redim dataPoint(dataCount, dataSeriesCount)
Redim dataPointName(dataCount)
For ctr = 1 to seriesCount
For ctr2 = 1 to dataCount
Set dataPoint(ctr2, ctr) = ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangle, dLeft, dTop, dWidth, dHeight)
dataPointName(ctr2) = dataPoint(ctr2, ctr).Name
Next ctr2
Set dataSeriesGroup(ctr) = Activesheet.Shapes(Array(dataPointName)).Group
Next ctr
Everything is working fine, but when I am trying to set the dataSeriesGroup(ctr) I am getting an error "Run-time error '-2147352571 (80020005)': The item with the specified name wasn't found."
Can someone please provide some guidance as to what I am doing wrong?
excel vba shapes
excel vba shapes
edited Nov 23 '18 at 8:03
keepAlive
3,14041224
3,14041224
asked Nov 20 '18 at 16:47
Samik BanerjeeSamik Banerjee
203
203
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The issue lies in the way variable datapointName
is declared. You want to build an array that will behave the same as those returned by the Array
function, which returns a zero-based array of Variants:
Dim datapointName() As Variant '<== Notice the parentheses.
Careful with the ReDims, as you typically don't want dangling Empty values at the far end of your arrays, so:
ReDim dataPointName(0 To dataCount - 1) '<== That's dataCount elements!
Also see the comments in the sample code, below, regarding variable declaration.
Finally, use the Range
property of the Shapes
collection to get a subset, and remove the call to Array()
, since dataPointName is already that:
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Putting it all together, here's some functional demo code based upon yours:
Sub DoTheShapesThing()
'Note: in VBA, to each variable its type; otherwise: Variant.
'I've renamed some variables for clarity.
Dim seriesIndex As Integer
Dim dataIndex As Integer
Dim seriesCount As Integer
Dim dataCount As Integer
Dim dataSeriesGroup() As Shape
Dim dataPoint() As Shape
'Haven't altered your position and size variables, but the type should typically be Double.
Dim dTop As Long
Dim dLeft As Long
Dim dWidth As Long
Dim dHeight As Long
Dim dataPointName() As Variant '<== Here, the parentheses make all the difference! You want an array of Variants, just like the Array function returns.
'I've added this declaration for the code to compile. REMOVE IT! You've probably declared this variable elsewhere.
Dim dataseriesCount As Long
'Test values...
seriesCount = 2
dataCount = 2
dataseriesCount = seriesCount '<== Note that dataseriesCount must be >= seriesCount so the code below doesn't go "Subscript out of range".
dLeft = 100: dTop = 100: dWidth = 100: dHeight = 100
ReDim dataSeriesGroup(0 To seriesCount - 1)
ReDim dataPoint(0 To dataCount - 1, 0 To dataseriesCount - 1)
ReDim dataPointName(0 To dataCount - 1)
For seriesIndex = 0 To seriesCount - 1
For dataIndex = 0 To dataCount - 1
'Took some liberties with shape disposition here...
Set dataPoint(dataIndex, seriesIndex) = ActiveSheet.Shapes.AddShape( _
msoShapeRoundedRectangle, _
dLeft + 10 * (seriesIndex + dataIndex), _
dTop + 10 * (seriesIndex + dataIndex), _
dWidth, _
dHeight)
dataPointName(dataIndex) = dataPoint(dataIndex, seriesIndex).Name
Next dataIndex
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Next seriesIndex
End Sub
Thank you so much!! My code is working now - the issue was with me not defining the dataPointName as an array. Really appreciate the help!
– Samik Banerjee
Nov 23 '18 at 8:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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%2fstackoverflow.com%2fquestions%2f53397720%2fthe-item-with-the-specified-name-wasnt-found-error-when-trying-to-group-shape%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue lies in the way variable datapointName
is declared. You want to build an array that will behave the same as those returned by the Array
function, which returns a zero-based array of Variants:
Dim datapointName() As Variant '<== Notice the parentheses.
Careful with the ReDims, as you typically don't want dangling Empty values at the far end of your arrays, so:
ReDim dataPointName(0 To dataCount - 1) '<== That's dataCount elements!
Also see the comments in the sample code, below, regarding variable declaration.
Finally, use the Range
property of the Shapes
collection to get a subset, and remove the call to Array()
, since dataPointName is already that:
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Putting it all together, here's some functional demo code based upon yours:
Sub DoTheShapesThing()
'Note: in VBA, to each variable its type; otherwise: Variant.
'I've renamed some variables for clarity.
Dim seriesIndex As Integer
Dim dataIndex As Integer
Dim seriesCount As Integer
Dim dataCount As Integer
Dim dataSeriesGroup() As Shape
Dim dataPoint() As Shape
'Haven't altered your position and size variables, but the type should typically be Double.
Dim dTop As Long
Dim dLeft As Long
Dim dWidth As Long
Dim dHeight As Long
Dim dataPointName() As Variant '<== Here, the parentheses make all the difference! You want an array of Variants, just like the Array function returns.
'I've added this declaration for the code to compile. REMOVE IT! You've probably declared this variable elsewhere.
Dim dataseriesCount As Long
'Test values...
seriesCount = 2
dataCount = 2
dataseriesCount = seriesCount '<== Note that dataseriesCount must be >= seriesCount so the code below doesn't go "Subscript out of range".
dLeft = 100: dTop = 100: dWidth = 100: dHeight = 100
ReDim dataSeriesGroup(0 To seriesCount - 1)
ReDim dataPoint(0 To dataCount - 1, 0 To dataseriesCount - 1)
ReDim dataPointName(0 To dataCount - 1)
For seriesIndex = 0 To seriesCount - 1
For dataIndex = 0 To dataCount - 1
'Took some liberties with shape disposition here...
Set dataPoint(dataIndex, seriesIndex) = ActiveSheet.Shapes.AddShape( _
msoShapeRoundedRectangle, _
dLeft + 10 * (seriesIndex + dataIndex), _
dTop + 10 * (seriesIndex + dataIndex), _
dWidth, _
dHeight)
dataPointName(dataIndex) = dataPoint(dataIndex, seriesIndex).Name
Next dataIndex
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Next seriesIndex
End Sub
Thank you so much!! My code is working now - the issue was with me not defining the dataPointName as an array. Really appreciate the help!
– Samik Banerjee
Nov 23 '18 at 8:38
add a comment |
The issue lies in the way variable datapointName
is declared. You want to build an array that will behave the same as those returned by the Array
function, which returns a zero-based array of Variants:
Dim datapointName() As Variant '<== Notice the parentheses.
Careful with the ReDims, as you typically don't want dangling Empty values at the far end of your arrays, so:
ReDim dataPointName(0 To dataCount - 1) '<== That's dataCount elements!
Also see the comments in the sample code, below, regarding variable declaration.
Finally, use the Range
property of the Shapes
collection to get a subset, and remove the call to Array()
, since dataPointName is already that:
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Putting it all together, here's some functional demo code based upon yours:
Sub DoTheShapesThing()
'Note: in VBA, to each variable its type; otherwise: Variant.
'I've renamed some variables for clarity.
Dim seriesIndex As Integer
Dim dataIndex As Integer
Dim seriesCount As Integer
Dim dataCount As Integer
Dim dataSeriesGroup() As Shape
Dim dataPoint() As Shape
'Haven't altered your position and size variables, but the type should typically be Double.
Dim dTop As Long
Dim dLeft As Long
Dim dWidth As Long
Dim dHeight As Long
Dim dataPointName() As Variant '<== Here, the parentheses make all the difference! You want an array of Variants, just like the Array function returns.
'I've added this declaration for the code to compile. REMOVE IT! You've probably declared this variable elsewhere.
Dim dataseriesCount As Long
'Test values...
seriesCount = 2
dataCount = 2
dataseriesCount = seriesCount '<== Note that dataseriesCount must be >= seriesCount so the code below doesn't go "Subscript out of range".
dLeft = 100: dTop = 100: dWidth = 100: dHeight = 100
ReDim dataSeriesGroup(0 To seriesCount - 1)
ReDim dataPoint(0 To dataCount - 1, 0 To dataseriesCount - 1)
ReDim dataPointName(0 To dataCount - 1)
For seriesIndex = 0 To seriesCount - 1
For dataIndex = 0 To dataCount - 1
'Took some liberties with shape disposition here...
Set dataPoint(dataIndex, seriesIndex) = ActiveSheet.Shapes.AddShape( _
msoShapeRoundedRectangle, _
dLeft + 10 * (seriesIndex + dataIndex), _
dTop + 10 * (seriesIndex + dataIndex), _
dWidth, _
dHeight)
dataPointName(dataIndex) = dataPoint(dataIndex, seriesIndex).Name
Next dataIndex
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Next seriesIndex
End Sub
Thank you so much!! My code is working now - the issue was with me not defining the dataPointName as an array. Really appreciate the help!
– Samik Banerjee
Nov 23 '18 at 8:38
add a comment |
The issue lies in the way variable datapointName
is declared. You want to build an array that will behave the same as those returned by the Array
function, which returns a zero-based array of Variants:
Dim datapointName() As Variant '<== Notice the parentheses.
Careful with the ReDims, as you typically don't want dangling Empty values at the far end of your arrays, so:
ReDim dataPointName(0 To dataCount - 1) '<== That's dataCount elements!
Also see the comments in the sample code, below, regarding variable declaration.
Finally, use the Range
property of the Shapes
collection to get a subset, and remove the call to Array()
, since dataPointName is already that:
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Putting it all together, here's some functional demo code based upon yours:
Sub DoTheShapesThing()
'Note: in VBA, to each variable its type; otherwise: Variant.
'I've renamed some variables for clarity.
Dim seriesIndex As Integer
Dim dataIndex As Integer
Dim seriesCount As Integer
Dim dataCount As Integer
Dim dataSeriesGroup() As Shape
Dim dataPoint() As Shape
'Haven't altered your position and size variables, but the type should typically be Double.
Dim dTop As Long
Dim dLeft As Long
Dim dWidth As Long
Dim dHeight As Long
Dim dataPointName() As Variant '<== Here, the parentheses make all the difference! You want an array of Variants, just like the Array function returns.
'I've added this declaration for the code to compile. REMOVE IT! You've probably declared this variable elsewhere.
Dim dataseriesCount As Long
'Test values...
seriesCount = 2
dataCount = 2
dataseriesCount = seriesCount '<== Note that dataseriesCount must be >= seriesCount so the code below doesn't go "Subscript out of range".
dLeft = 100: dTop = 100: dWidth = 100: dHeight = 100
ReDim dataSeriesGroup(0 To seriesCount - 1)
ReDim dataPoint(0 To dataCount - 1, 0 To dataseriesCount - 1)
ReDim dataPointName(0 To dataCount - 1)
For seriesIndex = 0 To seriesCount - 1
For dataIndex = 0 To dataCount - 1
'Took some liberties with shape disposition here...
Set dataPoint(dataIndex, seriesIndex) = ActiveSheet.Shapes.AddShape( _
msoShapeRoundedRectangle, _
dLeft + 10 * (seriesIndex + dataIndex), _
dTop + 10 * (seriesIndex + dataIndex), _
dWidth, _
dHeight)
dataPointName(dataIndex) = dataPoint(dataIndex, seriesIndex).Name
Next dataIndex
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Next seriesIndex
End Sub
The issue lies in the way variable datapointName
is declared. You want to build an array that will behave the same as those returned by the Array
function, which returns a zero-based array of Variants:
Dim datapointName() As Variant '<== Notice the parentheses.
Careful with the ReDims, as you typically don't want dangling Empty values at the far end of your arrays, so:
ReDim dataPointName(0 To dataCount - 1) '<== That's dataCount elements!
Also see the comments in the sample code, below, regarding variable declaration.
Finally, use the Range
property of the Shapes
collection to get a subset, and remove the call to Array()
, since dataPointName is already that:
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Putting it all together, here's some functional demo code based upon yours:
Sub DoTheShapesThing()
'Note: in VBA, to each variable its type; otherwise: Variant.
'I've renamed some variables for clarity.
Dim seriesIndex As Integer
Dim dataIndex As Integer
Dim seriesCount As Integer
Dim dataCount As Integer
Dim dataSeriesGroup() As Shape
Dim dataPoint() As Shape
'Haven't altered your position and size variables, but the type should typically be Double.
Dim dTop As Long
Dim dLeft As Long
Dim dWidth As Long
Dim dHeight As Long
Dim dataPointName() As Variant '<== Here, the parentheses make all the difference! You want an array of Variants, just like the Array function returns.
'I've added this declaration for the code to compile. REMOVE IT! You've probably declared this variable elsewhere.
Dim dataseriesCount As Long
'Test values...
seriesCount = 2
dataCount = 2
dataseriesCount = seriesCount '<== Note that dataseriesCount must be >= seriesCount so the code below doesn't go "Subscript out of range".
dLeft = 100: dTop = 100: dWidth = 100: dHeight = 100
ReDim dataSeriesGroup(0 To seriesCount - 1)
ReDim dataPoint(0 To dataCount - 1, 0 To dataseriesCount - 1)
ReDim dataPointName(0 To dataCount - 1)
For seriesIndex = 0 To seriesCount - 1
For dataIndex = 0 To dataCount - 1
'Took some liberties with shape disposition here...
Set dataPoint(dataIndex, seriesIndex) = ActiveSheet.Shapes.AddShape( _
msoShapeRoundedRectangle, _
dLeft + 10 * (seriesIndex + dataIndex), _
dTop + 10 * (seriesIndex + dataIndex), _
dWidth, _
dHeight)
dataPointName(dataIndex) = dataPoint(dataIndex, seriesIndex).Name
Next dataIndex
Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
Next seriesIndex
End Sub
edited Nov 23 '18 at 6:16
answered Nov 22 '18 at 20:44
ExcelosaurusExcelosaurus
2,1771715
2,1771715
Thank you so much!! My code is working now - the issue was with me not defining the dataPointName as an array. Really appreciate the help!
– Samik Banerjee
Nov 23 '18 at 8:38
add a comment |
Thank you so much!! My code is working now - the issue was with me not defining the dataPointName as an array. Really appreciate the help!
– Samik Banerjee
Nov 23 '18 at 8:38
Thank you so much!! My code is working now - the issue was with me not defining the dataPointName as an array. Really appreciate the help!
– Samik Banerjee
Nov 23 '18 at 8:38
Thank you so much!! My code is working now - the issue was with me not defining the dataPointName as an array. Really appreciate the help!
– Samik Banerjee
Nov 23 '18 at 8:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53397720%2fthe-item-with-the-specified-name-wasnt-found-error-when-trying-to-group-shape%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