?
Solved

excel vba position button to last empty row

Posted on 2011-09-05
6
Medium Priority
?
356 Views
Last Modified: 2012-05-12
Hi,

Is there a way to have a button on a sheet be moved each time a row has been filled (specific column)?

So let's say a the button is around row 10, after some inputs in D10 the button moves down to row 11 and so on....

Is that possible?

Regards
0
Comment
Question by:Mutsop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36484535
PLace the following in the code page for the sheet and then on edit to the sheet the button will move ... depending on activex or forms type buttons then use the appropriate name for your button. Right click the tab and select view code to display the editor.

Chris
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rw As Long

    rw = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
    Me.CommandButton1.Top = ActiveSheet.Rows(rw + 1).Top 'ActiveXbutton
    Me.Shapes("Button 1").Top = ActiveSheet.Rows(rw + 1).Top ' Form button
End Sub

Open in new window

0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 36484756
 
 '~~> Starting Row
    i = 2
    '~~> Find the first empty Row
    Do Until Len(Trim(Range("A" & i))) = 0
        i = i + 1
    Loop

    
    CommandButton.Top = Rows(1).Height * i

Open in new window

0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 36485501
For what it's worth the following is broadly similar to my first post but two differences:

1. Sets the row according to the last data in a specific column (A as presented) i.e the first version uses the last row with data at all irrespective if subsequently deleted.  This version targets the last row in the specific column.

2. Sets the left position of the button ... in columns B & F, (i.e. left of columns C & G)

Chris
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rw As Long

'    rw = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
    rw = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row

    With Me.CommandButton1
        ' ActiveXbutton
        .Top = ActiveSheet.Rows(rw + 1).Top
        .Left = ActiveSheet.Columns("C").Left
    End With
    With Me.Shapes("Button 1")
         ' Form button
        .Top = ActiveSheet.Rows(rw + 1).Top
        .Left = ActiveSheet.Columns("G").Left
    End With
End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Closing Comment

by:Mutsop
ID: 36486859
This works like a charm :)

But one other thing, So I open a form to input data in a cell.
So i tried using:
Private Sub CommandButtonBtw_Click()
    Dim rw As Long
    rw = ActiveSheet.Range("D" & ActiveSheet.Rows.Count).End(xlUp).row
    
    ActiveSheet.Range(rw, "D").Value = TextBoxBtw.Value
    
    Me.Hide
End Sub

Open in new window


But it gives me a 1004 error on
ActiveSheet.Range(rw, "D").Value = TextBoxBtw.Value

Open in new window


Any ideas?
0
 
LVL 3

Author Comment

by:Mutsop
ID: 36486878
never mind found it, had to change Range to Cells :)
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36486881
Either cells(row, column)
Or
Range("a"& re)

Ie you have mixed the syntax requirements
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question