error put multiple shapes in array when move multiple buttons

leap out

Active Member
Joined
Dec 4, 2020
Messages
288
Office Version
  1. 2016
  2. 2010
I try moving multiple shapes to last empty column when select cell but it gives error in this line
VBA Code:
For Each NofB In Shapes(Array("INSROW", "INSMONTH", "DELROW"))
all of the shapes are buttons


VBA Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim LastColFind As Long
Dim NofB As Variant
    For Each NofB In Shapes(Array("INSROW", "INSMONTH", "DELROW"))

    LastColFind = ActiveSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

    With Target
    
        Me.Shapes("BUTTON " & NofB).Top = .Top
        Me.Shapes("BUTTON " & NofB).Left = ActiveSheet.Cells(1, LastColFind + 1).Left + 2
    End With
    Next NofB
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try the following instead . . .

VBA Code:
For Each NofB In Array("INSROW", "INSMONTH", "DELROW")

Hope this helps!
 
Upvote 0
thanks, now move the error to this line
VBA Code:
 Me.Shapes("BUTTON " & NofB).Top = .Top
 
Upvote 0
It looks like it couldn't find a shape named either "BUTTON INSROW", "BUTTON INSMONTH", or "BUTTON DELROW" on your sheet. Do you in fact have three shapes with those names? If so, check for spelling mistakes.
 
Upvote 0
Hi Domenic !
thanks for inform me about this
It looks like it couldn't find a shape named either "BUTTON INSROW", "BUTTON INSMONTH", or "BUTTON DELROW" on your sheet
I don't seem to understand the code well, yes you're right the buttons name are not matched . I thought word BUTTON is not relating for shapes names . I thought this properties should indicate button name as is known .
in reality the shepas names as is in line array , it doesn't contain word BUTTON precede for each name . I try to change this
VBA Code:
Me.Shapes("BUTTON " & NofB).Top = .Top
        Me.Shapes("BUTTON " & NofB).Left = ActiveSheet.Cells(1, LastColFind + 1).Left + 2
to
VBA Code:
Me.Shapes(NofB).Top = .Top
        Me.Shapes(NofB).Left = ActiveSheet.Cells(1, LastColFind + 1).Left + 2
but shows the same error in array line .
I note if I correct as you suggestion shows me another problem .

orginal data
sh1.JPG


The buttons will be placed on top of each other
sh2.JPG


what I want moving the button to last empty columns like this
sh3.JPG

thanks for your help .
 
Upvote 0
With your buttons named INSROW, INSMONTH, and DELROW, try...

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    
    Dim LastColFind As Long
    LastColFind = Me.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    
    Dim NofB As Variant
    For Each NofB In Array("INSROW", "INSMONTH", "DELROW")
        Me.Shapes(NofB).Left = Me.Cells(1, LastColFind + 1).Left + 2
    Next NofB
End Sub

Note, however, this code will run every time you select a cell in your worksheet. Is this your intent?
 
Upvote 0
Solution
Note, however, this code will run every time you select a cell in your worksheet. Is this your intent?
yes because every time I will insert new column with fill data ,then will move the buttons to empty column
your vesion works excellently(y)
thanks very much .:)
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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