Would like help and advice on working with codes and buttons on MS-Excel











up vote
0
down vote

favorite












I am currently working on an Excel document where I would like to click on a cell and have a box pop-up with options of more command buttons to choose from. When the user clicks on one of the buttons within the user form the button it would then move a selection of that line of A:G to another sheet. I will have multiple lines and would like to code to adjust to the box where the initial command was initiated from. I have the first button and the user form complete and it is working well. The issue is on the next set of buttons to copy, paste and delete the line. Here is what I have so far. Any help would be appreciated.



Private Sub FireChief_Click()
Dim i As Long: i = 1
Dim n As Long
Dim nLastRow As Long
Dim lastRow As Integer
With ActiveSheet
For n = nReference To nLastRow Step 1

ActiveSheet.UsedRange

Set r = ActiveSheet.UsedRange
nLastRow = r.Rows.Count + r.Row + 1
Set Reference = Selection
Next Copy_to_Range()
ActiveWorksheets.Range("Reference").Copy
Destination = Worksheets("Fire Chief").Range("nLastRow")
Worksheets("Fire Chief").Columns("A:G").AutoFit

End Sub


So instead of the code listed above, I tried to rework it with some of the input that was provided. Basically this in a way would work like an Access document, and when the button was clicked it would do all of the work for the end user. I work in a fire dept and would like to make it easier on those who have to use it. There are 10 sheets that I have to work with and once I figure out one I will rewrite to match for the other. The new code is below



Option Explicit
Sub BaseFireMarshal_Click()

End Sub

Sub FireChief_Click()
Dim i As Long: i = 1
Dim n As Long
Dim nLastRow As Long
Dim lastRow As Integer
Dim B As Integer
Dim R As Range
Set R = Sheet5.Range("A2:G999")
If Worksheets("ActiveSheet").Range("A:XFD").Copy_ = True Then
Dim continue As Boolean
continue = True
If continue = True Then

Worksheets("Sheet5").Activate
B = Worksheets("Sheet5").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet5").Cells(B + 1, 1).Select
ActiveSheet.Paste
Worksheets("Fire Chief").Columns("A:G").AutoFit
Worksheets("Sheet5").Deactivate
End If

If Application.CutCopyMode = False Then
ThisWorkbook.Worksheets("ActiveSheet").Cells(1, 1).Select
End If

End Sub



Private Sub cmdClose_Click()
Unload Me
End Sub









share|improve this question




















  • 2




    I don't complete understand your question but here are a few pointers that might help improve your existing code: Firstly, Always use Option Explicit in your code. Second, you have nLastRow variable in quotes in your Destination. If you put your variable in quotes, it's then treated as string and you don't get the value of the variable. Third, never use ActiveSheet. Qualify your sheet. ActiveSheet is dangerous because you cannot always be sure if ActiveSheet is always the one you expect
    – Zac
    Nov 9 at 16:23















up vote
0
down vote

favorite












I am currently working on an Excel document where I would like to click on a cell and have a box pop-up with options of more command buttons to choose from. When the user clicks on one of the buttons within the user form the button it would then move a selection of that line of A:G to another sheet. I will have multiple lines and would like to code to adjust to the box where the initial command was initiated from. I have the first button and the user form complete and it is working well. The issue is on the next set of buttons to copy, paste and delete the line. Here is what I have so far. Any help would be appreciated.



Private Sub FireChief_Click()
Dim i As Long: i = 1
Dim n As Long
Dim nLastRow As Long
Dim lastRow As Integer
With ActiveSheet
For n = nReference To nLastRow Step 1

ActiveSheet.UsedRange

Set r = ActiveSheet.UsedRange
nLastRow = r.Rows.Count + r.Row + 1
Set Reference = Selection
Next Copy_to_Range()
ActiveWorksheets.Range("Reference").Copy
Destination = Worksheets("Fire Chief").Range("nLastRow")
Worksheets("Fire Chief").Columns("A:G").AutoFit

End Sub


So instead of the code listed above, I tried to rework it with some of the input that was provided. Basically this in a way would work like an Access document, and when the button was clicked it would do all of the work for the end user. I work in a fire dept and would like to make it easier on those who have to use it. There are 10 sheets that I have to work with and once I figure out one I will rewrite to match for the other. The new code is below



Option Explicit
Sub BaseFireMarshal_Click()

End Sub

Sub FireChief_Click()
Dim i As Long: i = 1
Dim n As Long
Dim nLastRow As Long
Dim lastRow As Integer
Dim B As Integer
Dim R As Range
Set R = Sheet5.Range("A2:G999")
If Worksheets("ActiveSheet").Range("A:XFD").Copy_ = True Then
Dim continue As Boolean
continue = True
If continue = True Then

Worksheets("Sheet5").Activate
B = Worksheets("Sheet5").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet5").Cells(B + 1, 1).Select
ActiveSheet.Paste
Worksheets("Fire Chief").Columns("A:G").AutoFit
Worksheets("Sheet5").Deactivate
End If

If Application.CutCopyMode = False Then
ThisWorkbook.Worksheets("ActiveSheet").Cells(1, 1).Select
End If

End Sub



Private Sub cmdClose_Click()
Unload Me
End Sub









share|improve this question




















  • 2




    I don't complete understand your question but here are a few pointers that might help improve your existing code: Firstly, Always use Option Explicit in your code. Second, you have nLastRow variable in quotes in your Destination. If you put your variable in quotes, it's then treated as string and you don't get the value of the variable. Third, never use ActiveSheet. Qualify your sheet. ActiveSheet is dangerous because you cannot always be sure if ActiveSheet is always the one you expect
    – Zac
    Nov 9 at 16:23













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am currently working on an Excel document where I would like to click on a cell and have a box pop-up with options of more command buttons to choose from. When the user clicks on one of the buttons within the user form the button it would then move a selection of that line of A:G to another sheet. I will have multiple lines and would like to code to adjust to the box where the initial command was initiated from. I have the first button and the user form complete and it is working well. The issue is on the next set of buttons to copy, paste and delete the line. Here is what I have so far. Any help would be appreciated.



Private Sub FireChief_Click()
Dim i As Long: i = 1
Dim n As Long
Dim nLastRow As Long
Dim lastRow As Integer
With ActiveSheet
For n = nReference To nLastRow Step 1

ActiveSheet.UsedRange

Set r = ActiveSheet.UsedRange
nLastRow = r.Rows.Count + r.Row + 1
Set Reference = Selection
Next Copy_to_Range()
ActiveWorksheets.Range("Reference").Copy
Destination = Worksheets("Fire Chief").Range("nLastRow")
Worksheets("Fire Chief").Columns("A:G").AutoFit

End Sub


So instead of the code listed above, I tried to rework it with some of the input that was provided. Basically this in a way would work like an Access document, and when the button was clicked it would do all of the work for the end user. I work in a fire dept and would like to make it easier on those who have to use it. There are 10 sheets that I have to work with and once I figure out one I will rewrite to match for the other. The new code is below



Option Explicit
Sub BaseFireMarshal_Click()

End Sub

Sub FireChief_Click()
Dim i As Long: i = 1
Dim n As Long
Dim nLastRow As Long
Dim lastRow As Integer
Dim B As Integer
Dim R As Range
Set R = Sheet5.Range("A2:G999")
If Worksheets("ActiveSheet").Range("A:XFD").Copy_ = True Then
Dim continue As Boolean
continue = True
If continue = True Then

Worksheets("Sheet5").Activate
B = Worksheets("Sheet5").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet5").Cells(B + 1, 1).Select
ActiveSheet.Paste
Worksheets("Fire Chief").Columns("A:G").AutoFit
Worksheets("Sheet5").Deactivate
End If

If Application.CutCopyMode = False Then
ThisWorkbook.Worksheets("ActiveSheet").Cells(1, 1).Select
End If

End Sub



Private Sub cmdClose_Click()
Unload Me
End Sub









share|improve this question















I am currently working on an Excel document where I would like to click on a cell and have a box pop-up with options of more command buttons to choose from. When the user clicks on one of the buttons within the user form the button it would then move a selection of that line of A:G to another sheet. I will have multiple lines and would like to code to adjust to the box where the initial command was initiated from. I have the first button and the user form complete and it is working well. The issue is on the next set of buttons to copy, paste and delete the line. Here is what I have so far. Any help would be appreciated.



Private Sub FireChief_Click()
Dim i As Long: i = 1
Dim n As Long
Dim nLastRow As Long
Dim lastRow As Integer
With ActiveSheet
For n = nReference To nLastRow Step 1

ActiveSheet.UsedRange

Set r = ActiveSheet.UsedRange
nLastRow = r.Rows.Count + r.Row + 1
Set Reference = Selection
Next Copy_to_Range()
ActiveWorksheets.Range("Reference").Copy
Destination = Worksheets("Fire Chief").Range("nLastRow")
Worksheets("Fire Chief").Columns("A:G").AutoFit

End Sub


So instead of the code listed above, I tried to rework it with some of the input that was provided. Basically this in a way would work like an Access document, and when the button was clicked it would do all of the work for the end user. I work in a fire dept and would like to make it easier on those who have to use it. There are 10 sheets that I have to work with and once I figure out one I will rewrite to match for the other. The new code is below



Option Explicit
Sub BaseFireMarshal_Click()

End Sub

Sub FireChief_Click()
Dim i As Long: i = 1
Dim n As Long
Dim nLastRow As Long
Dim lastRow As Integer
Dim B As Integer
Dim R As Range
Set R = Sheet5.Range("A2:G999")
If Worksheets("ActiveSheet").Range("A:XFD").Copy_ = True Then
Dim continue As Boolean
continue = True
If continue = True Then

Worksheets("Sheet5").Activate
B = Worksheets("Sheet5").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet5").Cells(B + 1, 1).Select
ActiveSheet.Paste
Worksheets("Fire Chief").Columns("A:G").AutoFit
Worksheets("Sheet5").Deactivate
End If

If Application.CutCopyMode = False Then
ThisWorkbook.Worksheets("ActiveSheet").Cells(1, 1).Select
End If

End Sub



Private Sub cmdClose_Click()
Unload Me
End Sub






excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 13:49

























asked Nov 9 at 16:13









edisonffa

12




12








  • 2




    I don't complete understand your question but here are a few pointers that might help improve your existing code: Firstly, Always use Option Explicit in your code. Second, you have nLastRow variable in quotes in your Destination. If you put your variable in quotes, it's then treated as string and you don't get the value of the variable. Third, never use ActiveSheet. Qualify your sheet. ActiveSheet is dangerous because you cannot always be sure if ActiveSheet is always the one you expect
    – Zac
    Nov 9 at 16:23














  • 2




    I don't complete understand your question but here are a few pointers that might help improve your existing code: Firstly, Always use Option Explicit in your code. Second, you have nLastRow variable in quotes in your Destination. If you put your variable in quotes, it's then treated as string and you don't get the value of the variable. Third, never use ActiveSheet. Qualify your sheet. ActiveSheet is dangerous because you cannot always be sure if ActiveSheet is always the one you expect
    – Zac
    Nov 9 at 16:23








2




2




I don't complete understand your question but here are a few pointers that might help improve your existing code: Firstly, Always use Option Explicit in your code. Second, you have nLastRow variable in quotes in your Destination. If you put your variable in quotes, it's then treated as string and you don't get the value of the variable. Third, never use ActiveSheet. Qualify your sheet. ActiveSheet is dangerous because you cannot always be sure if ActiveSheet is always the one you expect
– Zac
Nov 9 at 16:23




I don't complete understand your question but here are a few pointers that might help improve your existing code: Firstly, Always use Option Explicit in your code. Second, you have nLastRow variable in quotes in your Destination. If you put your variable in quotes, it's then treated as string and you don't get the value of the variable. Third, never use ActiveSheet. Qualify your sheet. ActiveSheet is dangerous because you cannot always be sure if ActiveSheet is always the one you expect
– Zac
Nov 9 at 16:23












1 Answer
1






active

oldest

votes

















up vote
0
down vote














I would like to click on a cell and have a box pop-up with options of more command buttons to choose from.




you could do this with grouping columns, from the "data" tab. essentially it collapses/expands a portion of the worksheet.




the button it would then move a selection of that line of A:G to another sheet




cut and paste




would like to code to adjust to the box where the initial command was initiated from




gonna wanna use "selection.column" or "selection.row" in your VBA code for this.




Here is what I have so far.




as for your code, I never use named ranges. I always use range(cells(row,column),cells(row,column)) because then its dynamic.



Try doing that and get back to us.






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',
    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%2f53229420%2fwould-like-help-and-advice-on-working-with-codes-and-buttons-on-ms-excel%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








    up vote
    0
    down vote














    I would like to click on a cell and have a box pop-up with options of more command buttons to choose from.




    you could do this with grouping columns, from the "data" tab. essentially it collapses/expands a portion of the worksheet.




    the button it would then move a selection of that line of A:G to another sheet




    cut and paste




    would like to code to adjust to the box where the initial command was initiated from




    gonna wanna use "selection.column" or "selection.row" in your VBA code for this.




    Here is what I have so far.




    as for your code, I never use named ranges. I always use range(cells(row,column),cells(row,column)) because then its dynamic.



    Try doing that and get back to us.






    share|improve this answer

























      up vote
      0
      down vote














      I would like to click on a cell and have a box pop-up with options of more command buttons to choose from.




      you could do this with grouping columns, from the "data" tab. essentially it collapses/expands a portion of the worksheet.




      the button it would then move a selection of that line of A:G to another sheet




      cut and paste




      would like to code to adjust to the box where the initial command was initiated from




      gonna wanna use "selection.column" or "selection.row" in your VBA code for this.




      Here is what I have so far.




      as for your code, I never use named ranges. I always use range(cells(row,column),cells(row,column)) because then its dynamic.



      Try doing that and get back to us.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote










        I would like to click on a cell and have a box pop-up with options of more command buttons to choose from.




        you could do this with grouping columns, from the "data" tab. essentially it collapses/expands a portion of the worksheet.




        the button it would then move a selection of that line of A:G to another sheet




        cut and paste




        would like to code to adjust to the box where the initial command was initiated from




        gonna wanna use "selection.column" or "selection.row" in your VBA code for this.




        Here is what I have so far.




        as for your code, I never use named ranges. I always use range(cells(row,column),cells(row,column)) because then its dynamic.



        Try doing that and get back to us.






        share|improve this answer













        I would like to click on a cell and have a box pop-up with options of more command buttons to choose from.




        you could do this with grouping columns, from the "data" tab. essentially it collapses/expands a portion of the worksheet.




        the button it would then move a selection of that line of A:G to another sheet




        cut and paste




        would like to code to adjust to the box where the initial command was initiated from




        gonna wanna use "selection.column" or "selection.row" in your VBA code for this.




        Here is what I have so far.




        as for your code, I never use named ranges. I always use range(cells(row,column),cells(row,column)) because then its dynamic.



        Try doing that and get back to us.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 16:53









        Kurt VonOhlen

        387




        387






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229420%2fwould-like-help-and-advice-on-working-with-codes-and-buttons-on-ms-excel%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

            Landwehr

            Reims

            Schenkenzell