Trying to use VBA macro to paste a formula to the selected cells










0














I am trying to create a macro that will paste a simple formula to add zeros on the cells that I select. This is what I have but it only works on the defined cells and I am not sure how to change the cell address to work on whatever cells the user selects.



Sub TimeConversion()
ActiveCell.Formula = "=REPT(0,4-LEN(E8)) & E8"
End Sub


Can someone tell me what I am doing wrong? I just need to be able to select various cells and run the REPT formula on them.



Thank you!










share|improve this question























  • Can they only select cells where you want a formula? Will you expect them to select other cells in the same sheet and no formula to appear?
    – QHarr
    Nov 11 at 17:54
















0














I am trying to create a macro that will paste a simple formula to add zeros on the cells that I select. This is what I have but it only works on the defined cells and I am not sure how to change the cell address to work on whatever cells the user selects.



Sub TimeConversion()
ActiveCell.Formula = "=REPT(0,4-LEN(E8)) & E8"
End Sub


Can someone tell me what I am doing wrong? I just need to be able to select various cells and run the REPT formula on them.



Thank you!










share|improve this question























  • Can they only select cells where you want a formula? Will you expect them to select other cells in the same sheet and no formula to appear?
    – QHarr
    Nov 11 at 17:54














0












0








0







I am trying to create a macro that will paste a simple formula to add zeros on the cells that I select. This is what I have but it only works on the defined cells and I am not sure how to change the cell address to work on whatever cells the user selects.



Sub TimeConversion()
ActiveCell.Formula = "=REPT(0,4-LEN(E8)) & E8"
End Sub


Can someone tell me what I am doing wrong? I just need to be able to select various cells and run the REPT formula on them.



Thank you!










share|improve this question















I am trying to create a macro that will paste a simple formula to add zeros on the cells that I select. This is what I have but it only works on the defined cells and I am not sure how to change the cell address to work on whatever cells the user selects.



Sub TimeConversion()
ActiveCell.Formula = "=REPT(0,4-LEN(E8)) & E8"
End Sub


Can someone tell me what I am doing wrong? I just need to be able to select various cells and run the REPT formula on them.



Thank you!







excel vba excel-vba excel-formula






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 17:54









Gary's Student

72.2k93662




72.2k93662










asked Nov 11 at 17:48









Josh

1




1











  • Can they only select cells where you want a formula? Will you expect them to select other cells in the same sheet and no formula to appear?
    – QHarr
    Nov 11 at 17:54

















  • Can they only select cells where you want a formula? Will you expect them to select other cells in the same sheet and no formula to appear?
    – QHarr
    Nov 11 at 17:54
















Can they only select cells where you want a formula? Will you expect them to select other cells in the same sheet and no formula to appear?
– QHarr
Nov 11 at 17:54





Can they only select cells where you want a formula? Will you expect them to select other cells in the same sheet and no formula to appear?
– QHarr
Nov 11 at 17:54













3 Answers
3






active

oldest

votes


















2














A basic principle would be to use a worksheet_SelectionChange event but you will probably want to restrict to certain cells as otherwise, any cell you select in the sheet which has the event will get the formula. Thanks to @GMalc for clarifying requirement and offering an amendment.



Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Target.NumberFormat = "0000"
Application.EnableEvents = True
End Sub


You can use Intersect and a specified range to limit to certain cells.



The code goes in the code pane of the sheet you want the formulas to appear in. Not in a standard | class module.






share|improve this answer






















  • I am trying to use this however now I get the error that "The macro may not be available in this workbook" I know you said this goes in the code pane but I'm not really sure what you mean. The formula bar?
    – Josh
    Nov 11 at 18:12










  • Select the actual worksheet tab in the workbook and right click view code > it goes in that window. The one associated with a specific sheet
    – QHarr
    Nov 11 at 18:13










  • Ok I see, I did that and I am still not getting the desired result. I need to be able to run this on various sheets and cells. Is there any way to set this up as a standard macro that can be invoked on any cells on any workbook? Thank you for you help by the way. I am pretty lost on this right now.
    – Josh
    Nov 11 at 18:17






  • 1




    Ok thank you so much for your time!
    – Josh
    Nov 11 at 18:23






  • 1




    Your macro is better than my simple one, but i added it anyway.
    – GMalc
    Nov 11 at 22:52


















1














This macro will add four zeros to the select cell/s.



For Each cell In Selection
cell.NumberFormat = "@"
cell.Value = WorksheetFunction.Rept("0", 4) & cell
Next





share|improve this answer




















  • Works perfectly! Thank you. Here is my final strip of code. I have to do this because the standard formatting does work properly for 24 hour time for some reason. Sub TimeConversion() For Each cell In Selection cell.NumberFormat = "@" cell.Value = WorksheetFunction.Rept("0", 4 - Len(cell)) & cell cell.Value = WorksheetFunction.Replace(cell, 3, 0, ":") Next End Sub
    – Josh
    Nov 12 at 2:42



















0














If you're looking out for a Solution in Excel-Formula applied in Excel-VBA, then here it is..



Sub test()
ActiveCell.Formula = "=REPT(0,4-LEN(E" & ActiveCell.Row & "))& E" & ActiveCell.Row & ""
End Sub





share|improve this answer




















    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%2f53251497%2ftrying-to-use-vba-macro-to-paste-a-formula-to-the-selected-cells%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









    2














    A basic principle would be to use a worksheet_SelectionChange event but you will probably want to restrict to certain cells as otherwise, any cell you select in the sheet which has the event will get the formula. Thanks to @GMalc for clarifying requirement and offering an amendment.



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.EnableEvents = False
    Target.NumberFormat = "0000"
    Application.EnableEvents = True
    End Sub


    You can use Intersect and a specified range to limit to certain cells.



    The code goes in the code pane of the sheet you want the formulas to appear in. Not in a standard | class module.






    share|improve this answer






















    • I am trying to use this however now I get the error that "The macro may not be available in this workbook" I know you said this goes in the code pane but I'm not really sure what you mean. The formula bar?
      – Josh
      Nov 11 at 18:12










    • Select the actual worksheet tab in the workbook and right click view code > it goes in that window. The one associated with a specific sheet
      – QHarr
      Nov 11 at 18:13










    • Ok I see, I did that and I am still not getting the desired result. I need to be able to run this on various sheets and cells. Is there any way to set this up as a standard macro that can be invoked on any cells on any workbook? Thank you for you help by the way. I am pretty lost on this right now.
      – Josh
      Nov 11 at 18:17






    • 1




      Ok thank you so much for your time!
      – Josh
      Nov 11 at 18:23






    • 1




      Your macro is better than my simple one, but i added it anyway.
      – GMalc
      Nov 11 at 22:52















    2














    A basic principle would be to use a worksheet_SelectionChange event but you will probably want to restrict to certain cells as otherwise, any cell you select in the sheet which has the event will get the formula. Thanks to @GMalc for clarifying requirement and offering an amendment.



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.EnableEvents = False
    Target.NumberFormat = "0000"
    Application.EnableEvents = True
    End Sub


    You can use Intersect and a specified range to limit to certain cells.



    The code goes in the code pane of the sheet you want the formulas to appear in. Not in a standard | class module.






    share|improve this answer






















    • I am trying to use this however now I get the error that "The macro may not be available in this workbook" I know you said this goes in the code pane but I'm not really sure what you mean. The formula bar?
      – Josh
      Nov 11 at 18:12










    • Select the actual worksheet tab in the workbook and right click view code > it goes in that window. The one associated with a specific sheet
      – QHarr
      Nov 11 at 18:13










    • Ok I see, I did that and I am still not getting the desired result. I need to be able to run this on various sheets and cells. Is there any way to set this up as a standard macro that can be invoked on any cells on any workbook? Thank you for you help by the way. I am pretty lost on this right now.
      – Josh
      Nov 11 at 18:17






    • 1




      Ok thank you so much for your time!
      – Josh
      Nov 11 at 18:23






    • 1




      Your macro is better than my simple one, but i added it anyway.
      – GMalc
      Nov 11 at 22:52













    2












    2








    2






    A basic principle would be to use a worksheet_SelectionChange event but you will probably want to restrict to certain cells as otherwise, any cell you select in the sheet which has the event will get the formula. Thanks to @GMalc for clarifying requirement and offering an amendment.



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.EnableEvents = False
    Target.NumberFormat = "0000"
    Application.EnableEvents = True
    End Sub


    You can use Intersect and a specified range to limit to certain cells.



    The code goes in the code pane of the sheet you want the formulas to appear in. Not in a standard | class module.






    share|improve this answer














    A basic principle would be to use a worksheet_SelectionChange event but you will probably want to restrict to certain cells as otherwise, any cell you select in the sheet which has the event will get the formula. Thanks to @GMalc for clarifying requirement and offering an amendment.



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.EnableEvents = False
    Target.NumberFormat = "0000"
    Application.EnableEvents = True
    End Sub


    You can use Intersect and a specified range to limit to certain cells.



    The code goes in the code pane of the sheet you want the formulas to appear in. Not in a standard | class module.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 22:26

























    answered Nov 11 at 17:56









    QHarr

    29.9k81841




    29.9k81841











    • I am trying to use this however now I get the error that "The macro may not be available in this workbook" I know you said this goes in the code pane but I'm not really sure what you mean. The formula bar?
      – Josh
      Nov 11 at 18:12










    • Select the actual worksheet tab in the workbook and right click view code > it goes in that window. The one associated with a specific sheet
      – QHarr
      Nov 11 at 18:13










    • Ok I see, I did that and I am still not getting the desired result. I need to be able to run this on various sheets and cells. Is there any way to set this up as a standard macro that can be invoked on any cells on any workbook? Thank you for you help by the way. I am pretty lost on this right now.
      – Josh
      Nov 11 at 18:17






    • 1




      Ok thank you so much for your time!
      – Josh
      Nov 11 at 18:23






    • 1




      Your macro is better than my simple one, but i added it anyway.
      – GMalc
      Nov 11 at 22:52
















    • I am trying to use this however now I get the error that "The macro may not be available in this workbook" I know you said this goes in the code pane but I'm not really sure what you mean. The formula bar?
      – Josh
      Nov 11 at 18:12










    • Select the actual worksheet tab in the workbook and right click view code > it goes in that window. The one associated with a specific sheet
      – QHarr
      Nov 11 at 18:13










    • Ok I see, I did that and I am still not getting the desired result. I need to be able to run this on various sheets and cells. Is there any way to set this up as a standard macro that can be invoked on any cells on any workbook? Thank you for you help by the way. I am pretty lost on this right now.
      – Josh
      Nov 11 at 18:17






    • 1




      Ok thank you so much for your time!
      – Josh
      Nov 11 at 18:23






    • 1




      Your macro is better than my simple one, but i added it anyway.
      – GMalc
      Nov 11 at 22:52















    I am trying to use this however now I get the error that "The macro may not be available in this workbook" I know you said this goes in the code pane but I'm not really sure what you mean. The formula bar?
    – Josh
    Nov 11 at 18:12




    I am trying to use this however now I get the error that "The macro may not be available in this workbook" I know you said this goes in the code pane but I'm not really sure what you mean. The formula bar?
    – Josh
    Nov 11 at 18:12












    Select the actual worksheet tab in the workbook and right click view code > it goes in that window. The one associated with a specific sheet
    – QHarr
    Nov 11 at 18:13




    Select the actual worksheet tab in the workbook and right click view code > it goes in that window. The one associated with a specific sheet
    – QHarr
    Nov 11 at 18:13












    Ok I see, I did that and I am still not getting the desired result. I need to be able to run this on various sheets and cells. Is there any way to set this up as a standard macro that can be invoked on any cells on any workbook? Thank you for you help by the way. I am pretty lost on this right now.
    – Josh
    Nov 11 at 18:17




    Ok I see, I did that and I am still not getting the desired result. I need to be able to run this on various sheets and cells. Is there any way to set this up as a standard macro that can be invoked on any cells on any workbook? Thank you for you help by the way. I am pretty lost on this right now.
    – Josh
    Nov 11 at 18:17




    1




    1




    Ok thank you so much for your time!
    – Josh
    Nov 11 at 18:23




    Ok thank you so much for your time!
    – Josh
    Nov 11 at 18:23




    1




    1




    Your macro is better than my simple one, but i added it anyway.
    – GMalc
    Nov 11 at 22:52




    Your macro is better than my simple one, but i added it anyway.
    – GMalc
    Nov 11 at 22:52













    1














    This macro will add four zeros to the select cell/s.



    For Each cell In Selection
    cell.NumberFormat = "@"
    cell.Value = WorksheetFunction.Rept("0", 4) & cell
    Next





    share|improve this answer




















    • Works perfectly! Thank you. Here is my final strip of code. I have to do this because the standard formatting does work properly for 24 hour time for some reason. Sub TimeConversion() For Each cell In Selection cell.NumberFormat = "@" cell.Value = WorksheetFunction.Rept("0", 4 - Len(cell)) & cell cell.Value = WorksheetFunction.Replace(cell, 3, 0, ":") Next End Sub
      – Josh
      Nov 12 at 2:42
















    1














    This macro will add four zeros to the select cell/s.



    For Each cell In Selection
    cell.NumberFormat = "@"
    cell.Value = WorksheetFunction.Rept("0", 4) & cell
    Next





    share|improve this answer




















    • Works perfectly! Thank you. Here is my final strip of code. I have to do this because the standard formatting does work properly for 24 hour time for some reason. Sub TimeConversion() For Each cell In Selection cell.NumberFormat = "@" cell.Value = WorksheetFunction.Rept("0", 4 - Len(cell)) & cell cell.Value = WorksheetFunction.Replace(cell, 3, 0, ":") Next End Sub
      – Josh
      Nov 12 at 2:42














    1












    1








    1






    This macro will add four zeros to the select cell/s.



    For Each cell In Selection
    cell.NumberFormat = "@"
    cell.Value = WorksheetFunction.Rept("0", 4) & cell
    Next





    share|improve this answer












    This macro will add four zeros to the select cell/s.



    For Each cell In Selection
    cell.NumberFormat = "@"
    cell.Value = WorksheetFunction.Rept("0", 4) & cell
    Next






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 22:48









    GMalc

    1,0131410




    1,0131410











    • Works perfectly! Thank you. Here is my final strip of code. I have to do this because the standard formatting does work properly for 24 hour time for some reason. Sub TimeConversion() For Each cell In Selection cell.NumberFormat = "@" cell.Value = WorksheetFunction.Rept("0", 4 - Len(cell)) & cell cell.Value = WorksheetFunction.Replace(cell, 3, 0, ":") Next End Sub
      – Josh
      Nov 12 at 2:42

















    • Works perfectly! Thank you. Here is my final strip of code. I have to do this because the standard formatting does work properly for 24 hour time for some reason. Sub TimeConversion() For Each cell In Selection cell.NumberFormat = "@" cell.Value = WorksheetFunction.Rept("0", 4 - Len(cell)) & cell cell.Value = WorksheetFunction.Replace(cell, 3, 0, ":") Next End Sub
      – Josh
      Nov 12 at 2:42
















    Works perfectly! Thank you. Here is my final strip of code. I have to do this because the standard formatting does work properly for 24 hour time for some reason. Sub TimeConversion() For Each cell In Selection cell.NumberFormat = "@" cell.Value = WorksheetFunction.Rept("0", 4 - Len(cell)) & cell cell.Value = WorksheetFunction.Replace(cell, 3, 0, ":") Next End Sub
    – Josh
    Nov 12 at 2:42





    Works perfectly! Thank you. Here is my final strip of code. I have to do this because the standard formatting does work properly for 24 hour time for some reason. Sub TimeConversion() For Each cell In Selection cell.NumberFormat = "@" cell.Value = WorksheetFunction.Rept("0", 4 - Len(cell)) & cell cell.Value = WorksheetFunction.Replace(cell, 3, 0, ":") Next End Sub
    – Josh
    Nov 12 at 2:42












    0














    If you're looking out for a Solution in Excel-Formula applied in Excel-VBA, then here it is..



    Sub test()
    ActiveCell.Formula = "=REPT(0,4-LEN(E" & ActiveCell.Row & "))& E" & ActiveCell.Row & ""
    End Sub





    share|improve this answer

























      0














      If you're looking out for a Solution in Excel-Formula applied in Excel-VBA, then here it is..



      Sub test()
      ActiveCell.Formula = "=REPT(0,4-LEN(E" & ActiveCell.Row & "))& E" & ActiveCell.Row & ""
      End Sub





      share|improve this answer























        0












        0








        0






        If you're looking out for a Solution in Excel-Formula applied in Excel-VBA, then here it is..



        Sub test()
        ActiveCell.Formula = "=REPT(0,4-LEN(E" & ActiveCell.Row & "))& E" & ActiveCell.Row & ""
        End Sub





        share|improve this answer












        If you're looking out for a Solution in Excel-Formula applied in Excel-VBA, then here it is..



        Sub test()
        ActiveCell.Formula = "=REPT(0,4-LEN(E" & ActiveCell.Row & "))& E" & ActiveCell.Row & ""
        End Sub






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 5:31









        Gipson Jebaraj

        314




        314



























            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.





            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251497%2ftrying-to-use-vba-macro-to-paste-a-formula-to-the-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