Using For Each Loop to Programmatically Generate Shapes and Add Macros
Okay, I'm in a bit over my head with this one, but I'm hoping that the answer is much simpler than it seems from my limited perspective.
I have an Excel workbook that I'm trying to set up as a Macro-enabled Template, and so I need all of my code and formulas to be extremely flexible, since they'll need to adapt dynamically to the project they're being used for in each instance.
Sheet 1 will be the Splice_Diagram sheet, and I have a named range on this sheet with the following reference:
=Splice_Diagram!$R$3:INDEX(Splice_Diagram!$R:$R,MATCH("",Splice_Diagram!$R:$R,-1))
The number of sheets named in this range will be variable based on the number of splices the project requires.
I need to create a macro to iterate through this list, create a shape for each, and assign the following to each shape:
Sheets("[Name of Sheet indicated in List]").Select
Range("A3").Select
The individual phases of this, I'm more or less able to muddle through, but I'm having trouble passing the names of the sheets iterated through in the Loop to the Macro Sheets.Select command.
For instance, if there are three Splices, then the list on the Splice_Diagram page will be Splice_01, Splice_02, and Splice_03. This should result in three shapes with Text that says Splice_01, Splice_02, and Splice_03 respectively. These shapes should run:
Splice_01:
Sheets("Splice_01").Select
Range("A3").Select
Splice_02:
Sheets("Splice_02").Select
Range("A3").Select
Splice_03:
Sheets("Splice_03").Select
Range("A3").Select
The sheets will always be labeled with the Prefix "Splice_" and an incrementing number, if that helps. However, the number of Splices will always be different.
The locations of the shapes don't matter at all, even if they're all stacked on top of each other, because I'll need to move them around to simulate the network anyway.
For the creation of the shapes themselves, I'm just using this little piece currently:
SpliceGuide.Shapes.AddShape msoShapeRectangle, 50, 50, 100, 200
I hope this was clear, please let me know if there's something incredibly obvious that I'm missing.
Thanks!
excel vba
add a comment |
Okay, I'm in a bit over my head with this one, but I'm hoping that the answer is much simpler than it seems from my limited perspective.
I have an Excel workbook that I'm trying to set up as a Macro-enabled Template, and so I need all of my code and formulas to be extremely flexible, since they'll need to adapt dynamically to the project they're being used for in each instance.
Sheet 1 will be the Splice_Diagram sheet, and I have a named range on this sheet with the following reference:
=Splice_Diagram!$R$3:INDEX(Splice_Diagram!$R:$R,MATCH("",Splice_Diagram!$R:$R,-1))
The number of sheets named in this range will be variable based on the number of splices the project requires.
I need to create a macro to iterate through this list, create a shape for each, and assign the following to each shape:
Sheets("[Name of Sheet indicated in List]").Select
Range("A3").Select
The individual phases of this, I'm more or less able to muddle through, but I'm having trouble passing the names of the sheets iterated through in the Loop to the Macro Sheets.Select command.
For instance, if there are three Splices, then the list on the Splice_Diagram page will be Splice_01, Splice_02, and Splice_03. This should result in three shapes with Text that says Splice_01, Splice_02, and Splice_03 respectively. These shapes should run:
Splice_01:
Sheets("Splice_01").Select
Range("A3").Select
Splice_02:
Sheets("Splice_02").Select
Range("A3").Select
Splice_03:
Sheets("Splice_03").Select
Range("A3").Select
The sheets will always be labeled with the Prefix "Splice_" and an incrementing number, if that helps. However, the number of Splices will always be different.
The locations of the shapes don't matter at all, even if they're all stacked on top of each other, because I'll need to move them around to simulate the network anyway.
For the creation of the shapes themselves, I'm just using this little piece currently:
SpliceGuide.Shapes.AddShape msoShapeRectangle, 50, 50, 100, 200
I hope this was clear, please let me know if there's something incredibly obvious that I'm missing.
Thanks!
excel vba
1
Can you edit the question to include more of the code? The 2 lines included aren't enough to give more than the most general suggestions.
– Comintern
Nov 21 '18 at 20:51
Do you want to assign the name of a sheet to each shape? Like changing the name of the shape to the name of the sheet? And if so, what you you mean by "passing the values from the Loop to the Sheet Names". Are you changing the sheet names?
– Christofer Weber
Nov 21 '18 at 21:07
1
Thanks for the questions. I edited my post to add a little clarity, but definitely let me know if I'm still not being clear enough.
– Tom Coker
Nov 21 '18 at 22:00
You need some way of dynamically determining how many splices are on yourSplice_Diagram
worksheet (might be as simple as just reading the values in the relevant range). Once you know that, you can loop over each solice -- and stick the code that you use to create each shape (plus text) inside that loop. You might need to change parts of the loop to refer to the current iteration/splice being looped over.
– chillin
Nov 21 '18 at 23:23
Do you want the shape text to be the value from "A3" from the corresponding sheet? If you wouldn't mind, an image with an example would be helpful.
– Christofer Weber
Nov 22 '18 at 16:51
add a comment |
Okay, I'm in a bit over my head with this one, but I'm hoping that the answer is much simpler than it seems from my limited perspective.
I have an Excel workbook that I'm trying to set up as a Macro-enabled Template, and so I need all of my code and formulas to be extremely flexible, since they'll need to adapt dynamically to the project they're being used for in each instance.
Sheet 1 will be the Splice_Diagram sheet, and I have a named range on this sheet with the following reference:
=Splice_Diagram!$R$3:INDEX(Splice_Diagram!$R:$R,MATCH("",Splice_Diagram!$R:$R,-1))
The number of sheets named in this range will be variable based on the number of splices the project requires.
I need to create a macro to iterate through this list, create a shape for each, and assign the following to each shape:
Sheets("[Name of Sheet indicated in List]").Select
Range("A3").Select
The individual phases of this, I'm more or less able to muddle through, but I'm having trouble passing the names of the sheets iterated through in the Loop to the Macro Sheets.Select command.
For instance, if there are three Splices, then the list on the Splice_Diagram page will be Splice_01, Splice_02, and Splice_03. This should result in three shapes with Text that says Splice_01, Splice_02, and Splice_03 respectively. These shapes should run:
Splice_01:
Sheets("Splice_01").Select
Range("A3").Select
Splice_02:
Sheets("Splice_02").Select
Range("A3").Select
Splice_03:
Sheets("Splice_03").Select
Range("A3").Select
The sheets will always be labeled with the Prefix "Splice_" and an incrementing number, if that helps. However, the number of Splices will always be different.
The locations of the shapes don't matter at all, even if they're all stacked on top of each other, because I'll need to move them around to simulate the network anyway.
For the creation of the shapes themselves, I'm just using this little piece currently:
SpliceGuide.Shapes.AddShape msoShapeRectangle, 50, 50, 100, 200
I hope this was clear, please let me know if there's something incredibly obvious that I'm missing.
Thanks!
excel vba
Okay, I'm in a bit over my head with this one, but I'm hoping that the answer is much simpler than it seems from my limited perspective.
I have an Excel workbook that I'm trying to set up as a Macro-enabled Template, and so I need all of my code and formulas to be extremely flexible, since they'll need to adapt dynamically to the project they're being used for in each instance.
Sheet 1 will be the Splice_Diagram sheet, and I have a named range on this sheet with the following reference:
=Splice_Diagram!$R$3:INDEX(Splice_Diagram!$R:$R,MATCH("",Splice_Diagram!$R:$R,-1))
The number of sheets named in this range will be variable based on the number of splices the project requires.
I need to create a macro to iterate through this list, create a shape for each, and assign the following to each shape:
Sheets("[Name of Sheet indicated in List]").Select
Range("A3").Select
The individual phases of this, I'm more or less able to muddle through, but I'm having trouble passing the names of the sheets iterated through in the Loop to the Macro Sheets.Select command.
For instance, if there are three Splices, then the list on the Splice_Diagram page will be Splice_01, Splice_02, and Splice_03. This should result in three shapes with Text that says Splice_01, Splice_02, and Splice_03 respectively. These shapes should run:
Splice_01:
Sheets("Splice_01").Select
Range("A3").Select
Splice_02:
Sheets("Splice_02").Select
Range("A3").Select
Splice_03:
Sheets("Splice_03").Select
Range("A3").Select
The sheets will always be labeled with the Prefix "Splice_" and an incrementing number, if that helps. However, the number of Splices will always be different.
The locations of the shapes don't matter at all, even if they're all stacked on top of each other, because I'll need to move them around to simulate the network anyway.
For the creation of the shapes themselves, I'm just using this little piece currently:
SpliceGuide.Shapes.AddShape msoShapeRectangle, 50, 50, 100, 200
I hope this was clear, please let me know if there's something incredibly obvious that I'm missing.
Thanks!
excel vba
excel vba
edited Nov 21 '18 at 21:54
Tom Coker
asked Nov 21 '18 at 20:47
Tom CokerTom Coker
113
113
1
Can you edit the question to include more of the code? The 2 lines included aren't enough to give more than the most general suggestions.
– Comintern
Nov 21 '18 at 20:51
Do you want to assign the name of a sheet to each shape? Like changing the name of the shape to the name of the sheet? And if so, what you you mean by "passing the values from the Loop to the Sheet Names". Are you changing the sheet names?
– Christofer Weber
Nov 21 '18 at 21:07
1
Thanks for the questions. I edited my post to add a little clarity, but definitely let me know if I'm still not being clear enough.
– Tom Coker
Nov 21 '18 at 22:00
You need some way of dynamically determining how many splices are on yourSplice_Diagram
worksheet (might be as simple as just reading the values in the relevant range). Once you know that, you can loop over each solice -- and stick the code that you use to create each shape (plus text) inside that loop. You might need to change parts of the loop to refer to the current iteration/splice being looped over.
– chillin
Nov 21 '18 at 23:23
Do you want the shape text to be the value from "A3" from the corresponding sheet? If you wouldn't mind, an image with an example would be helpful.
– Christofer Weber
Nov 22 '18 at 16:51
add a comment |
1
Can you edit the question to include more of the code? The 2 lines included aren't enough to give more than the most general suggestions.
– Comintern
Nov 21 '18 at 20:51
Do you want to assign the name of a sheet to each shape? Like changing the name of the shape to the name of the sheet? And if so, what you you mean by "passing the values from the Loop to the Sheet Names". Are you changing the sheet names?
– Christofer Weber
Nov 21 '18 at 21:07
1
Thanks for the questions. I edited my post to add a little clarity, but definitely let me know if I'm still not being clear enough.
– Tom Coker
Nov 21 '18 at 22:00
You need some way of dynamically determining how many splices are on yourSplice_Diagram
worksheet (might be as simple as just reading the values in the relevant range). Once you know that, you can loop over each solice -- and stick the code that you use to create each shape (plus text) inside that loop. You might need to change parts of the loop to refer to the current iteration/splice being looped over.
– chillin
Nov 21 '18 at 23:23
Do you want the shape text to be the value from "A3" from the corresponding sheet? If you wouldn't mind, an image with an example would be helpful.
– Christofer Weber
Nov 22 '18 at 16:51
1
1
Can you edit the question to include more of the code? The 2 lines included aren't enough to give more than the most general suggestions.
– Comintern
Nov 21 '18 at 20:51
Can you edit the question to include more of the code? The 2 lines included aren't enough to give more than the most general suggestions.
– Comintern
Nov 21 '18 at 20:51
Do you want to assign the name of a sheet to each shape? Like changing the name of the shape to the name of the sheet? And if so, what you you mean by "passing the values from the Loop to the Sheet Names". Are you changing the sheet names?
– Christofer Weber
Nov 21 '18 at 21:07
Do you want to assign the name of a sheet to each shape? Like changing the name of the shape to the name of the sheet? And if so, what you you mean by "passing the values from the Loop to the Sheet Names". Are you changing the sheet names?
– Christofer Weber
Nov 21 '18 at 21:07
1
1
Thanks for the questions. I edited my post to add a little clarity, but definitely let me know if I'm still not being clear enough.
– Tom Coker
Nov 21 '18 at 22:00
Thanks for the questions. I edited my post to add a little clarity, but definitely let me know if I'm still not being clear enough.
– Tom Coker
Nov 21 '18 at 22:00
You need some way of dynamically determining how many splices are on your
Splice_Diagram
worksheet (might be as simple as just reading the values in the relevant range). Once you know that, you can loop over each solice -- and stick the code that you use to create each shape (plus text) inside that loop. You might need to change parts of the loop to refer to the current iteration/splice being looped over.– chillin
Nov 21 '18 at 23:23
You need some way of dynamically determining how many splices are on your
Splice_Diagram
worksheet (might be as simple as just reading the values in the relevant range). Once you know that, you can loop over each solice -- and stick the code that you use to create each shape (plus text) inside that loop. You might need to change parts of the loop to refer to the current iteration/splice being looped over.– chillin
Nov 21 '18 at 23:23
Do you want the shape text to be the value from "A3" from the corresponding sheet? If you wouldn't mind, an image with an example would be helpful.
– Christofer Weber
Nov 22 '18 at 16:51
Do you want the shape text to be the value from "A3" from the corresponding sheet? If you wouldn't mind, an image with an example would be helpful.
– Christofer Weber
Nov 22 '18 at 16:51
add a comment |
0
active
oldest
votes
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%2f53420236%2fusing-for-each-loop-to-programmatically-generate-shapes-and-add-macros%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2fstackoverflow.com%2fquestions%2f53420236%2fusing-for-each-loop-to-programmatically-generate-shapes-and-add-macros%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
1
Can you edit the question to include more of the code? The 2 lines included aren't enough to give more than the most general suggestions.
– Comintern
Nov 21 '18 at 20:51
Do you want to assign the name of a sheet to each shape? Like changing the name of the shape to the name of the sheet? And if so, what you you mean by "passing the values from the Loop to the Sheet Names". Are you changing the sheet names?
– Christofer Weber
Nov 21 '18 at 21:07
1
Thanks for the questions. I edited my post to add a little clarity, but definitely let me know if I'm still not being clear enough.
– Tom Coker
Nov 21 '18 at 22:00
You need some way of dynamically determining how many splices are on your
Splice_Diagram
worksheet (might be as simple as just reading the values in the relevant range). Once you know that, you can loop over each solice -- and stick the code that you use to create each shape (plus text) inside that loop. You might need to change parts of the loop to refer to the current iteration/splice being looped over.– chillin
Nov 21 '18 at 23:23
Do you want the shape text to be the value from "A3" from the corresponding sheet? If you wouldn't mind, an image with an example would be helpful.
– Christofer Weber
Nov 22 '18 at 16:51