How to create a Macro that runs only on selected cells










-2















OK, So I have a very specific problem that I hope to make not-a-problem, but I need some help. I am a complete neophyte when it comes to making Macros.



I’ve got a customer that has put long lists of references in a single column in Excel (normally, the references are just in one cell). I’ve figured out how to consolidate a selected portion of a column of references into a single cell (see my procedure below), but what I’d really like to do is to turn this into a Macro that does it instantly.



The problem is that I’d like to be able to run a Macro on a selection, that is, I’d like to select the cells and have the macro run on ONLY those cells. Then I could select the cells, run the macro, make another selection, run the macro, etc. This would speed up my work tremendously.



My problem: I can’t figure out how to make the Macro run on just a selection. The Macros I’ve recorded only run on the selections I’ve made while recording the Macro. Is there any way to make the Macro generic, so I can select cells, and then run the Macro on them?



Combining References in Excel



  1. Let’s say the references you want to combine are in E18:E27

  2. Make a new column next to the reference designator column, for consolidated references

  3. In a blank cell in the new column, where you want to combine all the values, type:

  4. =CONCATENATE(TRANSPOSE(

  5. Then select the cells that need to be combined

  6. The formula will change to =CONCATENATE(TRANSPOSE(E18:E27

  7. Don’t press enter yet.

  8. Click after the last cell reference and hit spacebar

  9. Type the & operator

  10. Type ”, ” (double quote, comma, space, double quote)

  11. This will add a comma and a space after every reference

  12. Select TRANSPOSE(E18:E27 &", " and press the F9 key (F9 replaces formulas with values)

  13. This replaces TRANSPOSE(E18:E27 &" " with its result, "E18, ","E19, ","E20, ","E21, ","E22, ","E23, ","E24, ","E25, ","E26, ","E27, "

  14. Now remove the curly brackets and

  15. The formula will now look like =CONCATENATE("E18, ","E19, ","E20, ","E21, ","E22, ","E23, ","E24, ","E25, ","E26, ","E27, "

  16. Select the entire formula, including the = sign, and hit F9

  17. Hit Enter

  18. Done!

Note: this only works for up to 256 references. Any more and the formula will thrown an error. If there's any way to change it so it will work on more than that, please let me know.



Here's the code I get just by Record Macro-ing the procedure above, with "relative references" turned on. I don't know if that will help me or not.



Sub ConsolidateReferencesMacro2()
'
' ConsolidateReferencesMacro2 Macro
'

'
ActiveCell.Offset(-1, 3).Range("A1").Select
ActiveCell.FormulaR1C1 = _
"U102, U103, U104, U105, U199, U200, U201, U202, U204, U205, U206,
U207, U232, U233, U234, U235, U245, U246, U44, U45, U65, U66, "
With ActiveCell.Characters(Start:=1, Length:=128).Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub


I don't see "Concactencate" and "Transpose" so I don't know what is actually happening in this bit of code.










share|improve this question
























  • Would probably be easier to break the code down a bit. Might be an idea to assign your range to a variable like MyRange = “E18:E27”. Then make a loop, For each c in Range(MyRange).Cells.

    – Patrick S
    Nov 13 '18 at 0:13











  • If you’re selecting the area yourself, then you could use MyRange = Selection.

    – Patrick S
    Nov 13 '18 at 0:24











  • I think you should post the code you've recorded. It's possible that it can be tweaked by replacing the hardcoded range address in the recording with some range variable (e.g. rangeToConsolidate), then Set rangeToConsolidate = Selection (assuming you've highlighted/selected some cells). You could assign the macro to a keyboard shortcut, which should then reduce the procedure to 1. highlight/select cells, 2. press keyboard shortcut to execute macro. But start by posting the recorded code.

    – chillin
    Nov 13 '18 at 0:25











  • OK, I have added the code from recording my procedure.

    – WhiteLancer64
    Nov 13 '18 at 17:41















-2















OK, So I have a very specific problem that I hope to make not-a-problem, but I need some help. I am a complete neophyte when it comes to making Macros.



I’ve got a customer that has put long lists of references in a single column in Excel (normally, the references are just in one cell). I’ve figured out how to consolidate a selected portion of a column of references into a single cell (see my procedure below), but what I’d really like to do is to turn this into a Macro that does it instantly.



The problem is that I’d like to be able to run a Macro on a selection, that is, I’d like to select the cells and have the macro run on ONLY those cells. Then I could select the cells, run the macro, make another selection, run the macro, etc. This would speed up my work tremendously.



My problem: I can’t figure out how to make the Macro run on just a selection. The Macros I’ve recorded only run on the selections I’ve made while recording the Macro. Is there any way to make the Macro generic, so I can select cells, and then run the Macro on them?



Combining References in Excel



  1. Let’s say the references you want to combine are in E18:E27

  2. Make a new column next to the reference designator column, for consolidated references

  3. In a blank cell in the new column, where you want to combine all the values, type:

  4. =CONCATENATE(TRANSPOSE(

  5. Then select the cells that need to be combined

  6. The formula will change to =CONCATENATE(TRANSPOSE(E18:E27

  7. Don’t press enter yet.

  8. Click after the last cell reference and hit spacebar

  9. Type the & operator

  10. Type ”, ” (double quote, comma, space, double quote)

  11. This will add a comma and a space after every reference

  12. Select TRANSPOSE(E18:E27 &", " and press the F9 key (F9 replaces formulas with values)

  13. This replaces TRANSPOSE(E18:E27 &" " with its result, "E18, ","E19, ","E20, ","E21, ","E22, ","E23, ","E24, ","E25, ","E26, ","E27, "

  14. Now remove the curly brackets and

  15. The formula will now look like =CONCATENATE("E18, ","E19, ","E20, ","E21, ","E22, ","E23, ","E24, ","E25, ","E26, ","E27, "

  16. Select the entire formula, including the = sign, and hit F9

  17. Hit Enter

  18. Done!

Note: this only works for up to 256 references. Any more and the formula will thrown an error. If there's any way to change it so it will work on more than that, please let me know.



Here's the code I get just by Record Macro-ing the procedure above, with "relative references" turned on. I don't know if that will help me or not.



Sub ConsolidateReferencesMacro2()
'
' ConsolidateReferencesMacro2 Macro
'

'
ActiveCell.Offset(-1, 3).Range("A1").Select
ActiveCell.FormulaR1C1 = _
"U102, U103, U104, U105, U199, U200, U201, U202, U204, U205, U206,
U207, U232, U233, U234, U235, U245, U246, U44, U45, U65, U66, "
With ActiveCell.Characters(Start:=1, Length:=128).Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub


I don't see "Concactencate" and "Transpose" so I don't know what is actually happening in this bit of code.










share|improve this question
























  • Would probably be easier to break the code down a bit. Might be an idea to assign your range to a variable like MyRange = “E18:E27”. Then make a loop, For each c in Range(MyRange).Cells.

    – Patrick S
    Nov 13 '18 at 0:13











  • If you’re selecting the area yourself, then you could use MyRange = Selection.

    – Patrick S
    Nov 13 '18 at 0:24











  • I think you should post the code you've recorded. It's possible that it can be tweaked by replacing the hardcoded range address in the recording with some range variable (e.g. rangeToConsolidate), then Set rangeToConsolidate = Selection (assuming you've highlighted/selected some cells). You could assign the macro to a keyboard shortcut, which should then reduce the procedure to 1. highlight/select cells, 2. press keyboard shortcut to execute macro. But start by posting the recorded code.

    – chillin
    Nov 13 '18 at 0:25











  • OK, I have added the code from recording my procedure.

    – WhiteLancer64
    Nov 13 '18 at 17:41













-2












-2








-2


1






OK, So I have a very specific problem that I hope to make not-a-problem, but I need some help. I am a complete neophyte when it comes to making Macros.



I’ve got a customer that has put long lists of references in a single column in Excel (normally, the references are just in one cell). I’ve figured out how to consolidate a selected portion of a column of references into a single cell (see my procedure below), but what I’d really like to do is to turn this into a Macro that does it instantly.



The problem is that I’d like to be able to run a Macro on a selection, that is, I’d like to select the cells and have the macro run on ONLY those cells. Then I could select the cells, run the macro, make another selection, run the macro, etc. This would speed up my work tremendously.



My problem: I can’t figure out how to make the Macro run on just a selection. The Macros I’ve recorded only run on the selections I’ve made while recording the Macro. Is there any way to make the Macro generic, so I can select cells, and then run the Macro on them?



Combining References in Excel



  1. Let’s say the references you want to combine are in E18:E27

  2. Make a new column next to the reference designator column, for consolidated references

  3. In a blank cell in the new column, where you want to combine all the values, type:

  4. =CONCATENATE(TRANSPOSE(

  5. Then select the cells that need to be combined

  6. The formula will change to =CONCATENATE(TRANSPOSE(E18:E27

  7. Don’t press enter yet.

  8. Click after the last cell reference and hit spacebar

  9. Type the & operator

  10. Type ”, ” (double quote, comma, space, double quote)

  11. This will add a comma and a space after every reference

  12. Select TRANSPOSE(E18:E27 &", " and press the F9 key (F9 replaces formulas with values)

  13. This replaces TRANSPOSE(E18:E27 &" " with its result, "E18, ","E19, ","E20, ","E21, ","E22, ","E23, ","E24, ","E25, ","E26, ","E27, "

  14. Now remove the curly brackets and

  15. The formula will now look like =CONCATENATE("E18, ","E19, ","E20, ","E21, ","E22, ","E23, ","E24, ","E25, ","E26, ","E27, "

  16. Select the entire formula, including the = sign, and hit F9

  17. Hit Enter

  18. Done!

Note: this only works for up to 256 references. Any more and the formula will thrown an error. If there's any way to change it so it will work on more than that, please let me know.



Here's the code I get just by Record Macro-ing the procedure above, with "relative references" turned on. I don't know if that will help me or not.



Sub ConsolidateReferencesMacro2()
'
' ConsolidateReferencesMacro2 Macro
'

'
ActiveCell.Offset(-1, 3).Range("A1").Select
ActiveCell.FormulaR1C1 = _
"U102, U103, U104, U105, U199, U200, U201, U202, U204, U205, U206,
U207, U232, U233, U234, U235, U245, U246, U44, U45, U65, U66, "
With ActiveCell.Characters(Start:=1, Length:=128).Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub


I don't see "Concactencate" and "Transpose" so I don't know what is actually happening in this bit of code.










share|improve this question
















OK, So I have a very specific problem that I hope to make not-a-problem, but I need some help. I am a complete neophyte when it comes to making Macros.



I’ve got a customer that has put long lists of references in a single column in Excel (normally, the references are just in one cell). I’ve figured out how to consolidate a selected portion of a column of references into a single cell (see my procedure below), but what I’d really like to do is to turn this into a Macro that does it instantly.



The problem is that I’d like to be able to run a Macro on a selection, that is, I’d like to select the cells and have the macro run on ONLY those cells. Then I could select the cells, run the macro, make another selection, run the macro, etc. This would speed up my work tremendously.



My problem: I can’t figure out how to make the Macro run on just a selection. The Macros I’ve recorded only run on the selections I’ve made while recording the Macro. Is there any way to make the Macro generic, so I can select cells, and then run the Macro on them?



Combining References in Excel



  1. Let’s say the references you want to combine are in E18:E27

  2. Make a new column next to the reference designator column, for consolidated references

  3. In a blank cell in the new column, where you want to combine all the values, type:

  4. =CONCATENATE(TRANSPOSE(

  5. Then select the cells that need to be combined

  6. The formula will change to =CONCATENATE(TRANSPOSE(E18:E27

  7. Don’t press enter yet.

  8. Click after the last cell reference and hit spacebar

  9. Type the & operator

  10. Type ”, ” (double quote, comma, space, double quote)

  11. This will add a comma and a space after every reference

  12. Select TRANSPOSE(E18:E27 &", " and press the F9 key (F9 replaces formulas with values)

  13. This replaces TRANSPOSE(E18:E27 &" " with its result, "E18, ","E19, ","E20, ","E21, ","E22, ","E23, ","E24, ","E25, ","E26, ","E27, "

  14. Now remove the curly brackets and

  15. The formula will now look like =CONCATENATE("E18, ","E19, ","E20, ","E21, ","E22, ","E23, ","E24, ","E25, ","E26, ","E27, "

  16. Select the entire formula, including the = sign, and hit F9

  17. Hit Enter

  18. Done!

Note: this only works for up to 256 references. Any more and the formula will thrown an error. If there's any way to change it so it will work on more than that, please let me know.



Here's the code I get just by Record Macro-ing the procedure above, with "relative references" turned on. I don't know if that will help me or not.



Sub ConsolidateReferencesMacro2()
'
' ConsolidateReferencesMacro2 Macro
'

'
ActiveCell.Offset(-1, 3).Range("A1").Select
ActiveCell.FormulaR1C1 = _
"U102, U103, U104, U105, U199, U200, U201, U202, U204, U205, U206,
U207, U232, U233, U234, U235, U245, U246, U44, U45, U65, U66, "
With ActiveCell.Characters(Start:=1, Length:=128).Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub


I don't see "Concactencate" and "Transpose" so I don't know what is actually happening in this bit of code.







excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:38







WhiteLancer64

















asked Nov 12 '18 at 23:57









WhiteLancer64WhiteLancer64

34




34












  • Would probably be easier to break the code down a bit. Might be an idea to assign your range to a variable like MyRange = “E18:E27”. Then make a loop, For each c in Range(MyRange).Cells.

    – Patrick S
    Nov 13 '18 at 0:13











  • If you’re selecting the area yourself, then you could use MyRange = Selection.

    – Patrick S
    Nov 13 '18 at 0:24











  • I think you should post the code you've recorded. It's possible that it can be tweaked by replacing the hardcoded range address in the recording with some range variable (e.g. rangeToConsolidate), then Set rangeToConsolidate = Selection (assuming you've highlighted/selected some cells). You could assign the macro to a keyboard shortcut, which should then reduce the procedure to 1. highlight/select cells, 2. press keyboard shortcut to execute macro. But start by posting the recorded code.

    – chillin
    Nov 13 '18 at 0:25











  • OK, I have added the code from recording my procedure.

    – WhiteLancer64
    Nov 13 '18 at 17:41

















  • Would probably be easier to break the code down a bit. Might be an idea to assign your range to a variable like MyRange = “E18:E27”. Then make a loop, For each c in Range(MyRange).Cells.

    – Patrick S
    Nov 13 '18 at 0:13











  • If you’re selecting the area yourself, then you could use MyRange = Selection.

    – Patrick S
    Nov 13 '18 at 0:24











  • I think you should post the code you've recorded. It's possible that it can be tweaked by replacing the hardcoded range address in the recording with some range variable (e.g. rangeToConsolidate), then Set rangeToConsolidate = Selection (assuming you've highlighted/selected some cells). You could assign the macro to a keyboard shortcut, which should then reduce the procedure to 1. highlight/select cells, 2. press keyboard shortcut to execute macro. But start by posting the recorded code.

    – chillin
    Nov 13 '18 at 0:25











  • OK, I have added the code from recording my procedure.

    – WhiteLancer64
    Nov 13 '18 at 17:41
















Would probably be easier to break the code down a bit. Might be an idea to assign your range to a variable like MyRange = “E18:E27”. Then make a loop, For each c in Range(MyRange).Cells.

– Patrick S
Nov 13 '18 at 0:13





Would probably be easier to break the code down a bit. Might be an idea to assign your range to a variable like MyRange = “E18:E27”. Then make a loop, For each c in Range(MyRange).Cells.

– Patrick S
Nov 13 '18 at 0:13













If you’re selecting the area yourself, then you could use MyRange = Selection.

– Patrick S
Nov 13 '18 at 0:24





If you’re selecting the area yourself, then you could use MyRange = Selection.

– Patrick S
Nov 13 '18 at 0:24













I think you should post the code you've recorded. It's possible that it can be tweaked by replacing the hardcoded range address in the recording with some range variable (e.g. rangeToConsolidate), then Set rangeToConsolidate = Selection (assuming you've highlighted/selected some cells). You could assign the macro to a keyboard shortcut, which should then reduce the procedure to 1. highlight/select cells, 2. press keyboard shortcut to execute macro. But start by posting the recorded code.

– chillin
Nov 13 '18 at 0:25





I think you should post the code you've recorded. It's possible that it can be tweaked by replacing the hardcoded range address in the recording with some range variable (e.g. rangeToConsolidate), then Set rangeToConsolidate = Selection (assuming you've highlighted/selected some cells). You could assign the macro to a keyboard shortcut, which should then reduce the procedure to 1. highlight/select cells, 2. press keyboard shortcut to execute macro. But start by posting the recorded code.

– chillin
Nov 13 '18 at 0:25













OK, I have added the code from recording my procedure.

– WhiteLancer64
Nov 13 '18 at 17:41





OK, I have added the code from recording my procedure.

– WhiteLancer64
Nov 13 '18 at 17:41












2 Answers
2






active

oldest

votes


















0














Paste following code in a module:



Option Explicit

Sub concat_result()
Selection(Selection.Count, 1).Offset(0, 1) = concat_select(Selection)
End Sub

Function concat_select(R1 As Range) As String
concat_select = Join(Application.Transpose(R1.Value2), ",")
End Function


Then you can either call the function concat_select as a standard worksheet function or use the concat_result sub after selecting your range via a button or shortcut.






share|improve this answer























  • Oh!!!! That works =D THANK YOU SO MUCH!!!!! This has been a problem for me for years and I can't explain my feelings right now xD I'm in AWE that such a simple code does pretty much exactly what I want :) The only thing I'd change is the result of the macro outputs at the bottom of the adjacent column, I'd prefer if it would output at the top, that is to say, if the column is A15 to A20, then the results would be at B15. I don't even know if that's possible to do! And again, so many, many, many thanks!!

    – WhiteLancer64
    Nov 13 '18 at 18:09











  • To put the result next to the first selected cell, just replace Selection(Selection.Count, 1).Offset(0, 1) by Selection(1, 1).Offset(0, 1)

    – Display name
    Nov 13 '18 at 21:13











  • That is perfection itself! Again, thank you very, very much :)

    – WhiteLancer64
    Nov 14 '18 at 15:01


















0














I’m not 100% sure if I understand everything correctly, but as mentioned in my comments, it seems as if you could take advantage of using loops.



As I can see you’re new to the VBA-game.
I therefore explain some things to you, dom’t know if you already know some terms..



Looping in Excel can be used for ranges, for great results. This will let you be able to look in each cell, and determine a value, format or whatever you want, based on this cell.



You told me your range was in a selection, and that you want to make your code beside it.



Your code could therefore be something like:



Sub ConsolidateValues()
Dim c As Range

MyRange = Application.Selection.Address(False, False, xlA1)
FrstC = Left(MyRange, Application.WorksheetFunction.Find(":", MyRange) - 1)
CVal = ""

For Each c In Selection

CValue = c
CVal = CVal & CValue & ", "

Next c

Range(FrstC).Offset(0, 1).EntireColumn.Insert
FrstCOff = Range(FrstC).Offset(0, 1).Address(False, False, xlA1)
Range(FrstCOff).Value = CVal

End Sub


This should consolidate your Range into one cell to the right of “E17”.






share|improve this answer

























  • Thank you for your help! Sadly, I pasted your code in but upon attempting to use it, Excel told me it had a "Compile Error: Syntax Error" "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 17:53











  • When did it make this error??

    – Patrick S
    Nov 13 '18 at 17:55











  • "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 18:12











  • Sorry, I made something wrong… Look at it again, I have just edited the code :)

    – Patrick S
    Nov 13 '18 at 18:31











  • Note that it would probably be easier for you to not select the data. You can make excel select the data for you. Might be easier and more efficient on the long run, since you need to select the data here :)

    – Patrick S
    Nov 13 '18 at 18:35










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271836%2fhow-to-create-a-macro-that-runs-only-on-selected-cells%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Paste following code in a module:



Option Explicit

Sub concat_result()
Selection(Selection.Count, 1).Offset(0, 1) = concat_select(Selection)
End Sub

Function concat_select(R1 As Range) As String
concat_select = Join(Application.Transpose(R1.Value2), ",")
End Function


Then you can either call the function concat_select as a standard worksheet function or use the concat_result sub after selecting your range via a button or shortcut.






share|improve this answer























  • Oh!!!! That works =D THANK YOU SO MUCH!!!!! This has been a problem for me for years and I can't explain my feelings right now xD I'm in AWE that such a simple code does pretty much exactly what I want :) The only thing I'd change is the result of the macro outputs at the bottom of the adjacent column, I'd prefer if it would output at the top, that is to say, if the column is A15 to A20, then the results would be at B15. I don't even know if that's possible to do! And again, so many, many, many thanks!!

    – WhiteLancer64
    Nov 13 '18 at 18:09











  • To put the result next to the first selected cell, just replace Selection(Selection.Count, 1).Offset(0, 1) by Selection(1, 1).Offset(0, 1)

    – Display name
    Nov 13 '18 at 21:13











  • That is perfection itself! Again, thank you very, very much :)

    – WhiteLancer64
    Nov 14 '18 at 15:01















0














Paste following code in a module:



Option Explicit

Sub concat_result()
Selection(Selection.Count, 1).Offset(0, 1) = concat_select(Selection)
End Sub

Function concat_select(R1 As Range) As String
concat_select = Join(Application.Transpose(R1.Value2), ",")
End Function


Then you can either call the function concat_select as a standard worksheet function or use the concat_result sub after selecting your range via a button or shortcut.






share|improve this answer























  • Oh!!!! That works =D THANK YOU SO MUCH!!!!! This has been a problem for me for years and I can't explain my feelings right now xD I'm in AWE that such a simple code does pretty much exactly what I want :) The only thing I'd change is the result of the macro outputs at the bottom of the adjacent column, I'd prefer if it would output at the top, that is to say, if the column is A15 to A20, then the results would be at B15. I don't even know if that's possible to do! And again, so many, many, many thanks!!

    – WhiteLancer64
    Nov 13 '18 at 18:09











  • To put the result next to the first selected cell, just replace Selection(Selection.Count, 1).Offset(0, 1) by Selection(1, 1).Offset(0, 1)

    – Display name
    Nov 13 '18 at 21:13











  • That is perfection itself! Again, thank you very, very much :)

    – WhiteLancer64
    Nov 14 '18 at 15:01













0












0








0







Paste following code in a module:



Option Explicit

Sub concat_result()
Selection(Selection.Count, 1).Offset(0, 1) = concat_select(Selection)
End Sub

Function concat_select(R1 As Range) As String
concat_select = Join(Application.Transpose(R1.Value2), ",")
End Function


Then you can either call the function concat_select as a standard worksheet function or use the concat_result sub after selecting your range via a button or shortcut.






share|improve this answer













Paste following code in a module:



Option Explicit

Sub concat_result()
Selection(Selection.Count, 1).Offset(0, 1) = concat_select(Selection)
End Sub

Function concat_select(R1 As Range) As String
concat_select = Join(Application.Transpose(R1.Value2), ",")
End Function


Then you can either call the function concat_select as a standard worksheet function or use the concat_result sub after selecting your range via a button or shortcut.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 0:44









Display nameDisplay name

53416




53416












  • Oh!!!! That works =D THANK YOU SO MUCH!!!!! This has been a problem for me for years and I can't explain my feelings right now xD I'm in AWE that such a simple code does pretty much exactly what I want :) The only thing I'd change is the result of the macro outputs at the bottom of the adjacent column, I'd prefer if it would output at the top, that is to say, if the column is A15 to A20, then the results would be at B15. I don't even know if that's possible to do! And again, so many, many, many thanks!!

    – WhiteLancer64
    Nov 13 '18 at 18:09











  • To put the result next to the first selected cell, just replace Selection(Selection.Count, 1).Offset(0, 1) by Selection(1, 1).Offset(0, 1)

    – Display name
    Nov 13 '18 at 21:13











  • That is perfection itself! Again, thank you very, very much :)

    – WhiteLancer64
    Nov 14 '18 at 15:01

















  • Oh!!!! That works =D THANK YOU SO MUCH!!!!! This has been a problem for me for years and I can't explain my feelings right now xD I'm in AWE that such a simple code does pretty much exactly what I want :) The only thing I'd change is the result of the macro outputs at the bottom of the adjacent column, I'd prefer if it would output at the top, that is to say, if the column is A15 to A20, then the results would be at B15. I don't even know if that's possible to do! And again, so many, many, many thanks!!

    – WhiteLancer64
    Nov 13 '18 at 18:09











  • To put the result next to the first selected cell, just replace Selection(Selection.Count, 1).Offset(0, 1) by Selection(1, 1).Offset(0, 1)

    – Display name
    Nov 13 '18 at 21:13











  • That is perfection itself! Again, thank you very, very much :)

    – WhiteLancer64
    Nov 14 '18 at 15:01
















Oh!!!! That works =D THANK YOU SO MUCH!!!!! This has been a problem for me for years and I can't explain my feelings right now xD I'm in AWE that such a simple code does pretty much exactly what I want :) The only thing I'd change is the result of the macro outputs at the bottom of the adjacent column, I'd prefer if it would output at the top, that is to say, if the column is A15 to A20, then the results would be at B15. I don't even know if that's possible to do! And again, so many, many, many thanks!!

– WhiteLancer64
Nov 13 '18 at 18:09





Oh!!!! That works =D THANK YOU SO MUCH!!!!! This has been a problem for me for years and I can't explain my feelings right now xD I'm in AWE that such a simple code does pretty much exactly what I want :) The only thing I'd change is the result of the macro outputs at the bottom of the adjacent column, I'd prefer if it would output at the top, that is to say, if the column is A15 to A20, then the results would be at B15. I don't even know if that's possible to do! And again, so many, many, many thanks!!

– WhiteLancer64
Nov 13 '18 at 18:09













To put the result next to the first selected cell, just replace Selection(Selection.Count, 1).Offset(0, 1) by Selection(1, 1).Offset(0, 1)

– Display name
Nov 13 '18 at 21:13





To put the result next to the first selected cell, just replace Selection(Selection.Count, 1).Offset(0, 1) by Selection(1, 1).Offset(0, 1)

– Display name
Nov 13 '18 at 21:13













That is perfection itself! Again, thank you very, very much :)

– WhiteLancer64
Nov 14 '18 at 15:01





That is perfection itself! Again, thank you very, very much :)

– WhiteLancer64
Nov 14 '18 at 15:01













0














I’m not 100% sure if I understand everything correctly, but as mentioned in my comments, it seems as if you could take advantage of using loops.



As I can see you’re new to the VBA-game.
I therefore explain some things to you, dom’t know if you already know some terms..



Looping in Excel can be used for ranges, for great results. This will let you be able to look in each cell, and determine a value, format or whatever you want, based on this cell.



You told me your range was in a selection, and that you want to make your code beside it.



Your code could therefore be something like:



Sub ConsolidateValues()
Dim c As Range

MyRange = Application.Selection.Address(False, False, xlA1)
FrstC = Left(MyRange, Application.WorksheetFunction.Find(":", MyRange) - 1)
CVal = ""

For Each c In Selection

CValue = c
CVal = CVal & CValue & ", "

Next c

Range(FrstC).Offset(0, 1).EntireColumn.Insert
FrstCOff = Range(FrstC).Offset(0, 1).Address(False, False, xlA1)
Range(FrstCOff).Value = CVal

End Sub


This should consolidate your Range into one cell to the right of “E17”.






share|improve this answer

























  • Thank you for your help! Sadly, I pasted your code in but upon attempting to use it, Excel told me it had a "Compile Error: Syntax Error" "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 17:53











  • When did it make this error??

    – Patrick S
    Nov 13 '18 at 17:55











  • "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 18:12











  • Sorry, I made something wrong… Look at it again, I have just edited the code :)

    – Patrick S
    Nov 13 '18 at 18:31











  • Note that it would probably be easier for you to not select the data. You can make excel select the data for you. Might be easier and more efficient on the long run, since you need to select the data here :)

    – Patrick S
    Nov 13 '18 at 18:35















0














I’m not 100% sure if I understand everything correctly, but as mentioned in my comments, it seems as if you could take advantage of using loops.



As I can see you’re new to the VBA-game.
I therefore explain some things to you, dom’t know if you already know some terms..



Looping in Excel can be used for ranges, for great results. This will let you be able to look in each cell, and determine a value, format or whatever you want, based on this cell.



You told me your range was in a selection, and that you want to make your code beside it.



Your code could therefore be something like:



Sub ConsolidateValues()
Dim c As Range

MyRange = Application.Selection.Address(False, False, xlA1)
FrstC = Left(MyRange, Application.WorksheetFunction.Find(":", MyRange) - 1)
CVal = ""

For Each c In Selection

CValue = c
CVal = CVal & CValue & ", "

Next c

Range(FrstC).Offset(0, 1).EntireColumn.Insert
FrstCOff = Range(FrstC).Offset(0, 1).Address(False, False, xlA1)
Range(FrstCOff).Value = CVal

End Sub


This should consolidate your Range into one cell to the right of “E17”.






share|improve this answer

























  • Thank you for your help! Sadly, I pasted your code in but upon attempting to use it, Excel told me it had a "Compile Error: Syntax Error" "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 17:53











  • When did it make this error??

    – Patrick S
    Nov 13 '18 at 17:55











  • "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 18:12











  • Sorry, I made something wrong… Look at it again, I have just edited the code :)

    – Patrick S
    Nov 13 '18 at 18:31











  • Note that it would probably be easier for you to not select the data. You can make excel select the data for you. Might be easier and more efficient on the long run, since you need to select the data here :)

    – Patrick S
    Nov 13 '18 at 18:35













0












0








0







I’m not 100% sure if I understand everything correctly, but as mentioned in my comments, it seems as if you could take advantage of using loops.



As I can see you’re new to the VBA-game.
I therefore explain some things to you, dom’t know if you already know some terms..



Looping in Excel can be used for ranges, for great results. This will let you be able to look in each cell, and determine a value, format or whatever you want, based on this cell.



You told me your range was in a selection, and that you want to make your code beside it.



Your code could therefore be something like:



Sub ConsolidateValues()
Dim c As Range

MyRange = Application.Selection.Address(False, False, xlA1)
FrstC = Left(MyRange, Application.WorksheetFunction.Find(":", MyRange) - 1)
CVal = ""

For Each c In Selection

CValue = c
CVal = CVal & CValue & ", "

Next c

Range(FrstC).Offset(0, 1).EntireColumn.Insert
FrstCOff = Range(FrstC).Offset(0, 1).Address(False, False, xlA1)
Range(FrstCOff).Value = CVal

End Sub


This should consolidate your Range into one cell to the right of “E17”.






share|improve this answer















I’m not 100% sure if I understand everything correctly, but as mentioned in my comments, it seems as if you could take advantage of using loops.



As I can see you’re new to the VBA-game.
I therefore explain some things to you, dom’t know if you already know some terms..



Looping in Excel can be used for ranges, for great results. This will let you be able to look in each cell, and determine a value, format or whatever you want, based on this cell.



You told me your range was in a selection, and that you want to make your code beside it.



Your code could therefore be something like:



Sub ConsolidateValues()
Dim c As Range

MyRange = Application.Selection.Address(False, False, xlA1)
FrstC = Left(MyRange, Application.WorksheetFunction.Find(":", MyRange) - 1)
CVal = ""

For Each c In Selection

CValue = c
CVal = CVal & CValue & ", "

Next c

Range(FrstC).Offset(0, 1).EntireColumn.Insert
FrstCOff = Range(FrstC).Offset(0, 1).Address(False, False, xlA1)
Range(FrstCOff).Value = CVal

End Sub


This should consolidate your Range into one cell to the right of “E17”.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 18:40

























answered Nov 13 '18 at 0:51









Patrick SPatrick S

526




526












  • Thank you for your help! Sadly, I pasted your code in but upon attempting to use it, Excel told me it had a "Compile Error: Syntax Error" "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 17:53











  • When did it make this error??

    – Patrick S
    Nov 13 '18 at 17:55











  • "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 18:12











  • Sorry, I made something wrong… Look at it again, I have just edited the code :)

    – Patrick S
    Nov 13 '18 at 18:31











  • Note that it would probably be easier for you to not select the data. You can make excel select the data for you. Might be easier and more efficient on the long run, since you need to select the data here :)

    – Patrick S
    Nov 13 '18 at 18:35

















  • Thank you for your help! Sadly, I pasted your code in but upon attempting to use it, Excel told me it had a "Compile Error: Syntax Error" "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 17:53











  • When did it make this error??

    – Patrick S
    Nov 13 '18 at 17:55











  • "Sub ConsolidateValues()" was highlighted.

    – WhiteLancer64
    Nov 13 '18 at 18:12











  • Sorry, I made something wrong… Look at it again, I have just edited the code :)

    – Patrick S
    Nov 13 '18 at 18:31











  • Note that it would probably be easier for you to not select the data. You can make excel select the data for you. Might be easier and more efficient on the long run, since you need to select the data here :)

    – Patrick S
    Nov 13 '18 at 18:35
















Thank you for your help! Sadly, I pasted your code in but upon attempting to use it, Excel told me it had a "Compile Error: Syntax Error" "Sub ConsolidateValues()" was highlighted.

– WhiteLancer64
Nov 13 '18 at 17:53





Thank you for your help! Sadly, I pasted your code in but upon attempting to use it, Excel told me it had a "Compile Error: Syntax Error" "Sub ConsolidateValues()" was highlighted.

– WhiteLancer64
Nov 13 '18 at 17:53













When did it make this error??

– Patrick S
Nov 13 '18 at 17:55





When did it make this error??

– Patrick S
Nov 13 '18 at 17:55













"Sub ConsolidateValues()" was highlighted.

– WhiteLancer64
Nov 13 '18 at 18:12





"Sub ConsolidateValues()" was highlighted.

– WhiteLancer64
Nov 13 '18 at 18:12













Sorry, I made something wrong… Look at it again, I have just edited the code :)

– Patrick S
Nov 13 '18 at 18:31





Sorry, I made something wrong… Look at it again, I have just edited the code :)

– Patrick S
Nov 13 '18 at 18:31













Note that it would probably be easier for you to not select the data. You can make excel select the data for you. Might be easier and more efficient on the long run, since you need to select the data here :)

– Patrick S
Nov 13 '18 at 18:35





Note that it would probably be easier for you to not select the data. You can make excel select the data for you. Might be easier and more efficient on the long run, since you need to select the data here :)

– Patrick S
Nov 13 '18 at 18:35

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271836%2fhow-to-create-a-macro-that-runs-only-on-selected-cells%23new-answer', 'question_page');

);

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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo