Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Macro - VB - Next automatic number

0

I used the following on some form

iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _

    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

Is there a way to make it for a button without creating a userform.

Answer
Discuss

Discussion

Hi BBB.
I'm not clear what you're tring to do here. Please edit/expand your question to say more. Add an Excel file if possible
John_Ru (rep: 6142) Jan 16, '21 at 12:32 pm
Add to Discussion

Answers

1

To insert a button on a worksheet please follow these steps.

  1. Select the Ribbon's Developer tab.
    1. If you don't have that tab on your ribbon select File > Options.> Customize ribbon
    2. Under Main tabs on the right locate the Developer tab and enable it.
  2. Open the Insert menu in the ribbon's Controls menu and click on the icon for Command Button among the ActiveX Controls (it's the first one on the left).
  3. Left-click and hold on the sheet in the approximate position where you want the button and drag the rectangle that forms to the approximate size you want for your button. Release when done.
  4. Click on the new button and then on the Properties button in the ribbon's Control menu. In the pane that opens you can see the button's properties. Set the Width and Height properties to adjust the size, the Left and Top properties to re-specify the location. You can give the button another Caption and another name.
    1. I draw your attention to the PrintObject and TakeFocusOnClick properties. If the latter is set to False the button will not be selected when clicked, meaning the selection on the sheet doesn't change, which is usually exactly what you want.
  5. The above action has enabled design mode. The Design Mode button is next to the Insert button on the ribbon, now gray underlaid. When you have finished designing your command button click this button to exit design mode.
    1. Thereafter the command button will execute its function when clicked. To return to design mode click the Design Mode button again.
    2. The properties wiindow now shows the worksheet's properties. Close it.
  6. Now press Alt+F11 to switch to the VB Editor and locate the code module for the worksheet on which you just inserted the command button.
  7. There are two drop-downs at the top of that window with the default values of "General" and "Declarations" pre-selected.
  8. Open the drop-down on the left. You will find your new button listed under the name you gave to it. Click that name.
  9. This action will create the event procedure which will run whenever the button is clicked.
  10. Paste the code below into that procedure.
    ' select next cell in column A
    Cells(Rows.Count, "A").End(xlUp).Offset(1).Select

Now when you go back to your worksheet and click the button the cell below the last used cell in column A will be selected. You can modify this code to do other things.

Discuss

Discussion

That's do it, Variatus!
John_Ru (rep: 6142) Jan 17, '21 at 6:37 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login