Loop through add buttons across columns

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I've come up with what I think is getting me close.

VBA Code:
Dim i As Long, rng As Range

    Set rng = Range("E3")
    
    For i = 1 To 12

ActiveSheet.Buttons.Add(Range("E3").Left, Range("E3").Top, Range("E3").Width, Range("E3").Height).Select
Selection.OnAction = "Hide"
Selection.Characters.Text = "Hide"

    Next i

If I was trying to add text I know to use rng.offset, but I don't know how to incorporate rng.offset in the code above. Am I close?

In the end, I want a button in each cell E3:P3
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Tried this as well
VBA Code:
ActiveSheet.Buttons.Add(Range.Offset(0, i - 1)).Left,(Range.Offset(0, i - 1)).Top,(Range.Offset(0, i - 1)).Width,(Range.Offset(0, i - 1)).Height.Select
and
VBA Code:
ActiveSheet.Buttons.Add(Range(rng.Offset(0, i - 1)).Left, Range(rng.Offset(0, i - 1)).Top, Range(rng.Offset(0, i - 1)).Width, Range(rng.Offset(0, i - 1)).Height).Select
 
Upvote 0
I knew it was only a matter of time
Bingo!
VBA Code:
ActiveSheet.Buttons.Add(rng.Offset(0, i - 1).Left, rng.Offset(0, i - 1).Top, rng.Offset(0, i - 1).Width, rng.Offset(0, i - 1).Height).Select
 
Upvote 0
VBA Code:
Sub test()
'
    Dim i As Long
'
    For i = 1 To 12
        ActiveSheet.Buttons.Add(Range("E3").Offset(0, i - 1).Left, Range("E3").Offset(0, i - 1).Top, Range("E3").Offset(0, i - 1).Width, Range("E3").Offset(0, i - 1).Height).Select
        Selection.OnAction = "Hide"
        Selection.Characters.Text = "Hide"
    Next i
End Sub
 
Upvote 0
Solution
VBA Code:
Sub test()
'
    Dim i As Long
'
    For i = 1 To 12
        ActiveSheet.Buttons.Add(Range("E3").Offset(0, i - 1).Left, Range("E3").Offset(0, i - 1).Top, Range("E3").Offset(0, i - 1).Width, Range("E3").Offset(0, i - 1).Height).Select
        Selection.OnAction = "Hide"
        Selection.Characters.Text = "Hide"
    Next i
End Sub
Thanks for responding, I see that is another option. It always surprises me how many ways there is to accomplish the same thing.
 
Upvote 0
Thanks for responding, I see that is another option. It always surprises me how many ways there is to accomplish the same thing.
Often there is many ways to accomplish same goal. Thanks for responding!
 
Upvote 0

Forum statistics

Threads
1,214,544
Messages
6,120,126
Members
448,947
Latest member
test111

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top