PDA

View Full Version : Rename buttons using loop and "i + 1"



cplindem
03-03-2014, 12:18 PM
I am trying to rename a series of buttons (shapes) based on a series of cells in another worksheet. I'm trying to use a loop to look at a specific cell for the first button, then one cell over for the next button, and so on. I am currently using "For Each" and "i + 1" to try to do this, but it's not quite working. Either I should use a different type of loop or I have "i + 1" in the wrong spot in my code....I can't figure it out.


Sub ButtonNames()


Dim i As Integer

Dim ButtonText As String
ButtonText = Sheets("Purpose of Call").Range("D36").Offset(0, i)

Dim SummaryButton As Shape

For Each SummaryButton In Sheets("Summaries").Shapes.Range(Array("Button1", "Button2", "Button3"))
i = i + 1
SummaryButton.OLEFormat.Object.Text = ButtonText
Next SummaryButton

End Sub
Any help is greatly appreciated.

westconn1
03-03-2014, 01:09 PM
try like

For Each SummaryButton In Sheets("Summaries").Shapes.Range(Array("Button1", "Button2", "Button3"))

SummaryButton.OLEFormat.Object.Text = Sheets("Purpose of Call").Range("D36").Offset(0, i)
i = i + 1
Next SummaryButton

cplindem
03-03-2014, 01:25 PM
Aha, so I can't have the "ButtonText" portion outside of the For Each because that's what references the "i" variable?

(This is not a problem - i'm just trying to understand it)

westconn1
03-04-2014, 03:40 AM
before the loop i is always 0, as no other value is assigned to it

cplindem
03-04-2014, 09:33 AM
Yeah, I understand that. That's why I have the Offset for cell D36 set to zero at first. I want to look at cell D36, then cell E36, and so on.

westconn1
03-05-2014, 01:58 AM
I want to look at cell D36, then cell E36, and so on.then move i = i + 1 to after the summary button text, so i = 0 on the first iteration

cplindem
03-05-2014, 10:39 AM
Got it. Thanks