+ Reply to Thread
Results 1 to 17 of 17

Can a Command Button's position be kept moving along the TargetCell ROW in the same Column

Hybrid View

  1. #1
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Can a Command Button's position be kept moving along the TargetCell ROW in the same Column

    Can a Command Button's position be kept moving along the Target Cell ROW in the same Column?

    Dear Forum,

    Is it possible to achieve the above as I need the Command Bitton Handy can it be moving in the same Column but let's say I am at the ROw 10 I need it to be next to it in the Column B

    Warm REgards
    e4excel

  2. #2
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    you could use a code like this, but you will need to adjust the numbers to make it match your row heights

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveSheet.Shapes("Button 1").Top = 15 * Target.Row - 15
    End Sub
    If you liked my solution, please click on the Star -- to add to my reputation

    If your issue as been resolved, please clearly state so and mark the thread as [SOLVED] using the thread tools just above the first post.

  3. #3
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Ok No issues Dave,

    I shall start a new thread but please do help me on that..

    I will link this thread to the new one..

    Warm REgards
    e4excel
    Thanks once again for your help Dave adn Dgagnon

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Try This,
    It goes in the worksheet module
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Column <> 2 Then
            CommandButton1.Visible = False
            Exit Sub
        End If
        Dim ScRow As Long
        Dim ScCol As Integer
        ScRow = ActiveCell.Row
        ScCol = ActiveCell.Column
    
        With ActiveSheet.Shapes("CommandButton1")
            .Visible = True
            .Top = Cells(ScRow, ScCol).Top
            .Left = Cells(ScRow, ScCol + 1).Left
        End With
    
    End Sub
    Last edited by davesexcel; 04-22-2012 at 02:18 PM. Reason: Fancier

  5. #5
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Hi Dgagnon,

    Thnks for the help but I am not able to manage the Placement as I actually I require it almost at the same level as the selected Column B next to it...somewhere Column C..
    But it does move which is important..

    Hi Dave,

    Nice Avatar..
    Actually your code is absolutely wonderful, I mean I had hardly explained well this time and you produced a real gem..

    I am inspired with this and was thinking of having two buttons enclosed in a small user form, is that possible too..

    Application :
    I am entering the Term Deposit information and sometimes some FD's are continued so I just would go in the selected cell and then I would hvae this Button Visible ( User Form ) Visible which has two buttons one Command Button would be to continue with the same Tenure and the other Command Button would be to continue with a new tenure...

    WIll that be possible, unfortunately my file is not ready but is it ok this way as technically the the thread is SOLVED but I just realized that having two buttons would serve a better purpose...the user form containing these 3 Command Button needs to be moving..

    Warm Regards
    e4excel

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    The Avatar originally had no hat, until Teylyn gave it one for Christmas a few years ago.

    I am not entirely sure what you are asking.
    If you make a UserForm with two buttons, then in the button that becomes visible would have the code
    userform1.show

  7. #7
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Hi Dave,

    I have created a File which has a User Form with 4 Command Buttons and I need the user Form to be visible on clicking inside the column exactly the same way you have already done except that this time it will be an User Form...

    I also need help on the 4 Command Buttons, as you see this is Sheet which is used to record Fixed Deposit Entries..Usually the FD's are to be renewed incase we fail to submit the physical receipts on time, this is carried automatically by the bank which means that they just renew the F.D with the same details excpet that the Interest Rate changes subject to the prevailing interest rate..

    1. Renew F.D. - What I need with this Button is to just copy the contents of the Target Cells CEll B ( Account Holder ), Cell C ( Bank Name ), Cell E (FDR Acct No), Cell F ( FDR No ), Cell G ( Int Rate ), Cell I ( Maturity Date ) and Cell K ( Maturity Amount ) and paste in the last row...
    Most of the data would get pasted in the same columns i.e CEll B ( Account Holder ), Cell C ( Bank Name ), Cell E (FDR Acct No), Cell F ( FDR No ), Cell G ( Int Rate ) but some data changes its location as this FD is renewed so now the earlier Maturity Data becomes the opening Date and the New Maturity Date should be exactly the difference between the actual opening and Maturity Date so in this case it will be 182 days in my first example ....
    Also, the Investing Amount would be the maturity Amount...
    This FD is automatically created by the Bank and if you fail to report in 14 days and want to submit the Fd in your saving account then you lose the interest for those many days..

    2. Continue F.D. - Same like above except that the Maturiy Date will be entered by the user manually as this FD is opened voluntarilly and not compulsorily

    3. Delete Last Entry - Delete Last Row..

    4. Delete Range : lets say the selected row is 8 and the data is filled till the 10 row then I need the deletion to take place from the row 8,9 and 10..
    Attached Files Attached Files

  8. #8
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    You can use this code to show the command button when column B is selected
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Column <> 2 Then Exit Sub
    
        With UserForm1
            .StartUpPosition = 0
            .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
            .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
            .Show
        End With
    
    End Sub
    For your first Command button you do not indicate what last row you are referring to so I will assume it is the 1st empty row in Sheet2 Column A.
    Private Sub CommandButton1_Click()
        Dim Sh1 As Worksheet, Sht2 As Worksheet, Rws As Long
        Set sht1 = Worksheets(1)
        Set Sht2 = Worksheets(2)
        Rws = Sht2.Cells(Rows.Count, "A").End(xlUp).Row + 1
        Application.ScreenUpdating = 0
        ActiveCell.Range("A1:B1,D1:F1").Copy
        Sht2.Cells(Rws, 1).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = 0
        UserForm1.Hide
    
    End Sub
    From this you should be able to get your other codes to work.

  9. #9
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Dear Dave,

    Thanks for the help again and I am sorry that I was not clear enough but I require the contents to be copied in the same sheet in the last unfilled row as you mentioned..
    So in the present sheet If I clicked on any of the two buttons

    Renew FD - The aforesaid contents should get copy-pasted in the 11th Row , if the seelected cell is B2 then the contents in the cell A2,B2,E2,F2,G2 will get copied to the same set of cells i.e cell A11 ,B11,E11,F11,G11 and the contents in the cells cell I2 to cell cell H11 and in the cell I11 will be the same difference of days as it is between the opening date and the Maturity date in the Target cell.
    The original Maturity Amount in the cell K2 now should go in the cell D11 as it now becomes the Amount ..

    Continue FD - Everything same except that the Maturity Date should be blank..

    One very important thing I realised is that the user from should only be visible till the rows the data is filled and not in the blank rows and the user form has the Renew FD enabled by default can this be normal?

    Please also help me for the delete last entry from the same sheet and the delete range as mentioned earlier that is if im on the 8th row and there are 10rwos filled then all the three rwos beginning from 8 below 9 and 10 get deleted..

    Thanks in advance...

    Warm Regards
    e4exce
    Last edited by e4excel; 04-27-2012 at 05:41 AM.

  10. #10
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Copy and paste non-contiguous cells

    You can count the cells first before showing the userform
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Column <> 2 Then Exit Sub
        If WorksheetFunction.CountA(Target.Offset(0, -1).Range("A1:L1")) < 10 Then Exit Sub
        
        With UserForm1
            .StartUpPosition = 0
            .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
            .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
            .Show
        End With
    
    End Sub
    Replace the code in the command button1 with this
    Private Sub CommandButton1_Click()
        Dim Rws As Long
        Rws = Cells(Rows.Count, "B").End(xlUp).Row + 1
        
        ActiveCell.Range("A1:B1").Copy Destination:=Cells(Rws, 2)
        ActiveCell.Range("D1:F1").Copy Destination:=Cells(Rws, 5)
        UserForm1.Hide
    
    End Sub
    Show us your attempts for the other questions.

  11. #11
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Show us your attempts for the other questions.
    Dear Dave,

    I have made a few changes in the code you provided as well as added some new code for the command buttons, please advise if thats correct though its working..

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Column <> 2 Or Target.Row = 1 Then Exit Sub
        If WorksheetFunction.CountA(Target.Offset(0, -1).Range("A1:L1")) < 10 Then Exit Sub
        
        With UserForm1
            .StartUpPosition = 0
            .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
            .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
            .Show
        End With
    
    End Sub
    I also wanted to know about a strange thing that when I tried using the column names as it is it gave an incorrect answer..
    I mean I had to use one column behind to refer the actual column.. in the code for the first button..

    Now there's a slight problem as the copied values are getting copied with formulas and that creates a circulr reference so how do I amend that as the Maturity amount of the original active row becomes the Amount in the Renewed or COntinued FD..

    Can you please make the existing formulas in the columns A, col K and Col L as values after using the formula as I want these formulas to also appear when the REnewed FD and Continue FD is in progress..lets say I have dragged the formulas till the row 11 that is for 10 records and then I add a FD by Renew FD then I need the formula in the column A to show the Sr. No as well as the col K to show the Maturity Amount and the col L the interest..

    I dont know how to write these formulas in this button click event..


    Please find my codes:

    Private Sub CommandButton1_Click()
        Dim Rws As Long
        Rws = Cells(Rows.Count, "B").End(xlUp).Row + 1
        
        ActiveCell.Range("A1:B1").Copy Destination:=Cells(Rws, 2)
        ActiveCell.Range("J1").Copy Destination:=Cells(Rws, 4)
        ActiveCell.Range("D1:F1").Copy Destination:=Cells(Rws, 5)
        ActiveCell.Range("H1").Copy Destination:=Cells(Rws, 8)
        Cells(Rws, 9).Value = Application.WorksheetFunction.Text(Cells(Rws, 8).Value + DateDiff("d", ActiveCell.Range("G1"), ActiveCell.Range("H1")), "dd-mmmm-yy")
        UserForm1.Hide
    
    End Sub
    
    Private Sub CommandButton2_Click()
        Dim Rws As Long
        Rws = Cells(Rows.Count, "B").End(xlUp).Row + 1
        
        ActiveCell.Range("A1:B1").Copy Destination:=Cells(Rws, 2)
        ActiveCell.Range("J1").Copy Destination:=Cells(Rws, 4)
        ActiveCell.Range("D1:F1").Copy Destination:=Cells(Rws, 5)
        ActiveCell.Range("H1").Copy Destination:=Cells(Rws, 8)
        UserForm1.Hide
    End Sub
    Private Sub CommandButton3_Click()
    
    Dim lastrow As Long
    lastrow = ActiveSheet.UsedRange.Rows.Count
    
        Range("A" & lastrow, "A" & lastrow).EntireRow.Delete
        
    End Sub
    Private Sub CommandButton4_Click()
    Dim activerow As Long
    Dim lastrow As Long
    
    activerow = ActiveCell.Row
    lastrow = ActiveSheet.UsedRange.Rows.Count
    
        Range("B" & activerow, "B" & lastrow).EntireRow.Delete
        
    End Sub
    Last edited by e4excel; 04-29-2012 at 04:00 PM.

  12. #12
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Please also advise on the formatting for the FOnt colour as well as the Borders to exist till the data is entered beginning from the column B..

  13. #13
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    WHat I meant is that can there be a formula in the row and then it gets copied to the value as I keep on getting the circular reference error

  14. #14
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Bump No Response

    Bump No Response

  15. #15
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Why are you bumping? you said it works.

  16. #16
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Dear Dave,

    It did work but it creates a circular reference as the entire cell gets copied alongwith the formula for the Maturity Amount which becomes the Principal AMount when the button is clicked..so can you please suggest a slight modification for that as well as how do I maintain the formatting in the columns since there are some columns which change places and therefore the formatting changes as the original while i need to maintain the same formatting like the column has..

    It works and I agree to it and your help was indeed valuable as well as the links you provided are also very helpful...but I just need to better it..

    Lets say i have data only till the 11th row i.e 10 records excluding the column headings so naturally my formulas in the columns A and the column K and COlumn L are also till the 11th Row..

    Is there some method wherein I can have these formulas also get copied when I run the command button click..
    I need help on these areas so that this work would be completely air-tight.

    I want the Data Vaildation in the columns B and Columns C alongwith the Formula in the col A for Sr.No and the formula in the col K for the Maturity Amount as well as the Formula in the column L for the Interest to get written when the data gets copied to the last row..

    I would appreciate if you could just help me with just 1 Data Validation and 1 Formula and I will take a cue from there.

    Bottomline is I need help on some formatting and the Formulas..

    Thanks in advance..

    Warm Regars
    e4excel

  17. #17
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Can a Command Button's position be kept moving along the TargetCell ROW in the same Co

    Please mark this thread as solved as the original question has been answerd.

    Then start a new thread.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1