VBA Excel Transform to loop - vba

Here i have my hundred line of code please enlighten me about how do i put line code into loop
here is my try but i wont work out still trying
If sp.Name Like "Rounded Rectangle*" Or sp.Name Like "Oval*" Then
For i = 11 To 100
x = i - 9
Sheet2.Shapes.Range(Array("Rounded Rectangle " + i)).TextFrame.Characters.Text = Sheet1.Range("A" + x)
Next i
End If
and repeat until X = 110
in this case how can i change it in to correct loop please advice
thank you

This is a general approach to making a loop to cover a string variable....say we want to loop over Shape("Rectangle 1")....Shape("Rectangle 2")....Shape("Rectangle 3)..... , etc.
Dim str As String, i As Long
For i = 11 To 100
str = "Rectangle " & CStr(i)
Sheets2.Shapes(str)................
Next i
and use a similar approach to make "A2"..."A3".........

simple math:
For i = 11 To 100
change to
For i = 11 To 119

Related

Why Does It Make A Difference If A Macro is Run When Slide Show Has Started

Apologies if this is a basic question, but I am a newbie at VBA and have spent the past two weeks learning the language. I am trying to make a learning resource using PowerPoint.
I made a good macro which seems to work perfectly when it is run outside the slide show. (I typically just pressed F5 within my VBA Editor to see what the code was doing.) However, when I try running the macro when the slide show is running (using an action button) it does not work as intended. To be more precise it appears to stop running and not show any errors or provide reasons why the macro has stopped.
I tried doing a Google search on this topic, but could not find anything. I would be very grateful if anyone could provide any reasons or suggest any information sources as to why macros work differently when the slide show is running as opposed to not running? Are there any standard practices to ensure that the macros will work when the slide show is run?
Thank you for any comments!
EDIT 02/07/20 - responding to a comment I attach the code below and have explained what I am trying to do in comments below
Sub DataInputMacro()
'This procedure attempts to group (and do a check) of all 20 answer tiles.
Dim oSh As Shape
Dim i As Integer 'This is for the For statement
Dim iix As Integer 'This is to store the x position of the tiles.
Dim iiy As Integer 'This is to store the y position of the tiles.
For i = 1 To 20
If i = 1 Or i = 3 Or i = 5 Or i = 7 Or i = 9 Then
iix = 9
ElseIf i = 2 Or i = 4 Or i = 6 Or i = 8 Or i = 10 Then
iix = 198
ElseIf i = 11 Or i = 13 Or i = 15 Or i = 17 Or i = 19 Then
iix = 587
Else
iix = 774
End If
If i = 1 Or i = 2 Or i = 11 Or i = 12 Then
iiy = 9
ElseIf i = 3 Or i = 4 Or i = 13 Or i = 14 Then
iiy = 113
ElseIf i = 5 Or i = 6 Or i = 15 Or i = 16 Then
iiy = 218
ElseIf i = 7 Or i = 8 Or i = 17 Or i = 18 Then
iiy = 323
Else
iiy = 428
End If
ActiveWindow.Selection.Unselect 'This ensures that nothing has already been selected on the slide (which could then get grouped with the first rectangle!)
For Each oSh In ActivePresentation.Slides("DataInput").Shapes
If IsWithinRange(oSh, iix - 1, iiy - 1, iix + 179, iiy + 97) Then
'MsgBox oSh.Name & " " & oSh.Left
oSh.Select (msoFalse)
End If
Next
If ActiveWindow.Selection.ShapeRange.Count > 1 Then
With ActiveWindow.Selection.ShapeRange.Group
.Name = "GroupAnswer" & i
.Select
End With
ElseIf ActiveWindow.Selection.ShapeRange.Type = msoGroup Then
ActiveWindow.Selection.ShapeRange.Name = "GroupAnswer" & i & "a"
ElseIf ActiveWindow.Selection.ShapeRange.Count = 1 Then
MsgBox "Sorry, but there is a set up issues of your answer boxes. Either (1) one of your answer boxes does not contain a separate textbox or image or (2) there is a missing yellow rectangle! I note for point (1) the text must be placed in a separate text box to the yellow rectangle!"
End
End If
Next i
UpdateTitle 'This is a separate function that updates the title on the front slide.
CheckFor20Groups 'This is a function that checks that there are 20 groups for the rest of the program to use!
End Sub
Shape.Select method does not work in slide view - see remarks here: Shape.Select method (PowerPoint) - so you will have to use another method for working with your shapes - try using a variable to refer to the ShapeRange you are working with rather than ActiveWindow.Selection.ShapeRange - that way you can work with them without needing to select them.

Is it possible to loop through sapgui array using a for next loop in excel vba?

I would like to loop through and read back the text boxes of each element in SAP GUI table/array:
dim Rtned_Desc as string
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,0]").SetFocus
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,0]").caretPosition = 9
Rtned_Desc = session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,0]").Text
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,1]").SetFocus
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,1]").caretPosition = 9
Rtned_Desc = session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,1]").Text
etc...
I'd like to do a "for next" loop, something like this:
for index = 0 to 10
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,index]").SetFocus
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,index]").caretPosition = 9
Rtned_Desc = session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,index]).Text
next
I have been trying to escape the 'index' variable, but have failed. Is this the right approach?
You can try the following:
for index = 0 to 10
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2," & cstr(index) & "]").SetFocus
'It is not necessarily needed.
'session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,index]").caretPosition = 9
Rtned_Desc = session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2," & cstr(index) & "]").Text
next
Regards, ScriptMan

excel vba, assigning values to variables with similar names using for next loop

Brand new to stackoverflow and am also quite new to programming. I am very grateful for this incredible resource. Thank you!
I have 30 TextBoxes on my Userform. TextBox1 TextBox2......TextBox30. I would like to assign values to all of them quickly using a For Next Loop. Just not too sure how.
Example:
For X=1 to 30
TextBox & X.Value ="Hello"
Next X
Thank you in advance for any help.
This should do it.
Private Sub UserForm_Initialize()
For x = 1 To 30
Controls("Textbox" & x).Value = "Hello " & x
Next x
End Sub
How about this:
For x = 1 To 30
Sheets(1).Shapes("textbox " & x).TextFrame.Characters.Text = "Hello"
Next x

Double For Loop in VB.NET

Dim ssi(11) As String
For i = 0 To 10
If ssi(i) = "" Then ssi(i) = "0"
For j = 0 To Val(ssi(11)) + i
ssi(i) = xuh(Val(ssi(i)))
Next
Next
If ssi(11) = "2" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10)))
ElseIf ssi(11) = "3" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10))) * (-1)
End If
I am new here and new to VB as well.
I am trying to understand this double loop in vb code.
ssi(i) is defined as a String variable. and each element is assigned to a specific number in a String. Hope I told it clearly.
My problem with this loop is below.
Since i ranges from 0 to 10, what does this j mean? Does j mean the new ssi(1-10) or another whatever number?
I think the best way to answer your question about understanding a double loop is to try looking at something simpler.
The first program I always write in each new version of BASIC that comes along is a 12 times table.
I've modified it a bit below to be a 12 x 10 table for the purpose of illustrating for you how a double loop works ... hope it helps:
For x As Integer = 1 To 12
For y As Integer = 1 To 10
Console.Write(x * y)
Console.Write(vbTab)
Next
Console.WriteLine()
Next

Trouble with looping in VBA (access) to put items on a form and position them correctly

I've been trying to make a form with 68 items while positioning all the items on a form via a loop, but this loop isn't working :( Can someone please help me get this to work? I've tried looking everywhere but can't see what to do :/
Dim Items(67) As String
For x = 0 To 67
Items(x) = "Ctl" & x
Next
#It goes from 1 here as I manually set up 1 otherwise (x - 1) wont work.
For x = 1 To 67
Form_Home.Items(x).Top = 0
Form_Home.Items(x).Left = (Form_MainScreen.Items(x - 1).Left) + (Form_MainScreen.Items(x - 1).Width)
Form_Home.Items(x).Height = 225
Form_Home.Items(x).Width = 500
Next
Thank you everyone :)
What do you mean with Form_Home.Items? if you already have controls added to your forms, probably there is a Controls collection, so you can iterate trough it and set its properties, assuming each control has a name of the form Ctl0, Ctl1, Ctl2... then you can proceed as follow:
Dim ct_name as String
Dim ct_name_before as String
For x = 1 To 67
ct_name_before = "Ctl" & CStr(Cint(x-1))
ct_name = "Ctl" & CStr(x)
Form_Home.Controls(ct_name).Top = 0
Form_Home.Controls(ct_name).Left = (Form_MainScreen.Items(ct_name_before).Left) + _
(Form_MainScreen.Items(ct_name_before).Width)
Form_Home.Controls(ct_name).Height = 225
Next
Some version of VBA allow you to create array of controls sharing all the same name and having different index, then you can iterate through the array to get each control.