Clear ComboBox Containig an Array VBA and Repopulate with Column Index - excel

I have a sigle column ComboBox that I have populated with a dynamic array; however, I'd like the code to replace the value of the combo box with respective column from my ListBox.
'Prepares the Active Escorts list box.
ivb = 0
i = 0
With frmEntry
.listboxActiveEscorts.Clear
.listboxActiveEscorts.ColumnHeads = False
.listboxActiveEscorts.ColumnCount = "15"
.listboxActiveEscorts.ColumnWidths = "0,100,100,0,0,100,100,0,0,0,0,0,100,100,80"
i = 0
'Badge # combobox properties
ReDim vbArray(0 To vbArrayCount - 1)
For i = 0 To vbArrayCount - 1
ivb = ivb + 1
vbArray(ivb - 1) = loVisBadge.Range.Cells(i + 2, 1).Value
Next i
.cbxVisitorBadgeNumber.List = vbArray
End With
This population of the ComboBox executes beautifully, in my belief, but if you see a better way to implement the dynamic list, I'm all ears.
I have a button that I will use to reset the form, which also works for the most part. When an item is selected from the list, I am able to clear all controls except for the ComboxBox containing the array of values. I will be adding two additional arrays to each of the other ComboBoxes on the form, and I imagine I am going to have the same problem: "Could not set the value property. Invalid property value"
This is the code assigned to click event of the ListBox:
Private Sub listboxActiveEscorts_Click()
With frmEntry
.cbxEscortSelectName.Value = .listboxActiveEscorts.Column(1) + " " + .listboxActiveEscorts.Column(2)
.txtCredential.Value = .listboxActiveEscorts.Column(4)
.txtEscortCompany.Value = .listboxActiveEscorts.Column(3)
.cbxVisitorName.Value = .listboxActiveEscorts.Column(5) + " " + .listboxActiveEscorts.Column(6)
.txtVECompany.Value = .listboxActiveEscorts.Column(7)
.txtVEDOB.Value = .listboxActiveEscorts.Column(8)
.txtVEIdentification.Value = .listboxActiveEscorts.Column(9)
.txtVEIDNumber.Value = .listboxActiveEscorts.Column(10)
.txtVEExpirationDate.Value = .listboxActiveEscorts.Column(11)
.txtVEStart.Value = .listboxActiveEscorts.Column(12)
.txtVEEnd.Value = .listboxActiveEscorts.Column(13)
'.cbxVisitorBadgeNumber.Value = ""
.cbxVisitorBadgeNumber = vbNullString
.cbxVisitorBadgeNumber.Value = .listboxActiveEscorts.Column(14)
End With
End Sub
What am I missing here. I tried to ReDim the array that assigned the values, but that didn't work. Is it a data type thing, perhaps?
In the picture below, you'll see the values populated in the controls, all except the visitor badge # (ComboBox which throws the error...I have commented out the line for illustration purposes, so you'll see the visitor badge # is blank).

Related

Changing Value of Combo Box to another Value in VB

I am trying to change the value of a combo box value "Black Shredded - 7.90" to just show "Black Shredded" when it is selected
Dim intIndex As Integer
Dim strString1 As String
Dim strString2 As String
strString1 = cboProduct.SelectedItem
intIndex = strString1.IndexOf(" ")
strString2 = strString1.Remove(intIndex + 9)
If cboProduct.SelectedIndex = 0 Then
cboProduct.Text = strString2
End If
I went through the values and they show as they should but it isn't changing the combobox value what could I be doing wrong?
If you have just added Strings to the ComboBox in the first place then you need to replace the existing item with the new value. This:
cboProduct.Text = strString2
should be this:
cboProduct.Items(cboProduct.SelectedIndex) = strString2
You can just use 0 rather than cboProduct.SelectedIndex, given that you have already confirmed that that is the index at that point.
Setting the Text property doesn't affect the items at all. If DropDownStyle is set to DropDown then the specified text will be displayed but no item will be selected. If DropDownStyle is set to DropDownList then the item with that text will be selected, if one exists. Either way, no item is added or changed.

Importing CustomDocumentProperties from Word to Excel using VBA

I am trying to pull CustomDocumentProperties from a Word Document (that I select using Application.GetOpenFilename) to an Excel Sheet.
I can get the to run code using the number of the item:
Set ExcelRange = Range("DataFields")
For r = 1 To ExcelRange.Rows.Count Step 1
ExcelWorkbook.Sheets("Sheet1").Cells(r, 3) = WordDoc.CustomDocumentProperties(r).Value
Next r
If I hard code the name of the custom property, it also works:
Set ExcelRange = Range("DataFields")
For r = 1 To ExcelRange.Rows.Count Step 1
ExcelWorkbook.Sheets("Sheet1").Cells(r, 3) = WordDoc.CustomDocumentProperties("Subject Name").Value
Next r
Obviously in this case, it returns the "Subject Name" 7 times).
I don't want to return all the values - only specific ones based on the values of a named range (in this case DataFields).
DataFields references A1:A7. I would like to take those cells(that contain the names of the custom properties) and paste the value of the corresponding custom properties in C1:C7
However, I can seem to get the code to return values in C1:C7 based on the values in A1:A7.
Based on my (limited) knowledge, I thought that the following would return the desired results, but it's not working for me:
Set ExcelRange = Range("DataFields")
For r = 1 To ExcelRange.Rows.Count Step 1
ExcelWorkbook.Sheets("Sheet1").Cells(r, 3) = WordDoc.CustomDocumentProperties(ExcelRange(r, 1))
Next r
Any help would be appreciated.
Thanks!

Save SM37 rows with sorting via GUI Script?

The following SAP GUI Script for SAP 740 saves the SM37 list to a text file.
Before saving the list, I'd like to sort the list by Start date and Start time.
I can display the popup to select Sort fields. On left side there is a table control with current Sort columns (job name, created by, progress, scheduled start date, scheduled start time), on right side there is a table control with all possible Sort columns, each of these left and right Sort columns can be selected, and between the two table controls there are buttons to move Sort columns from left to right or vice versa.
Now, to select Sort columns, I have to select rows of table controls, but I don't know how to do it. Can you help me?
Public Sub RunSM37Cancelled()
Dim W_Ret As Boolean
W_Ret = Attach_Session
If Not W_Ret Then
Exit Sub
End If
On Error GoTo disperr
objSess.findById("wnd[0]").maximize
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "sm37"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/txtBTCH2170-JOBNAME").Text = "*"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/txtBTCH2170-USERNAME").Text = "*"
objSess.findById("wnd[0]/usr/txtBTCH2170-USERNAME").SetFocus
objSess.findById("wnd[0]/usr/txtBTCH2170-USERNAME").caretPosition = 1
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/chkBTCH2170-SCHEDUL").Selected = False
objSess.findById("wnd[0]/usr/chkBTCH2170-READY").Selected = False
objSess.findById("wnd[0]/usr/chkBTCH2170-RUNNING").Selected = False
objSess.findById("wnd[0]/usr/chkBTCH2170-FINISHED").Selected = False
objSess.findById("wnd[0]/usr/chkBTCH2170-ABORTED").Selected = True
objSess.findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").Text = Format(Now()-1,"mm/dd/yyyy")
objSess.findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").SetFocus
objSess.findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").caretPosition = 10
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").Text = Format(Now(), "mm/dd/yyyy")
objSess.findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").SetFocus
objSess.findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").caretPosition = 10
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/tbar[1]/btn[8]").press
objSess.findById("wnd[0]/usr/lbl[80,10]").SetFocus
objSess.findById("wnd[0]/usr/lbl[80,10]").caretPosition = 5
objSess.findById("wnd[0]").sendVKey 2
objSess.findById("wnd[0]/usr/lbl[91,10]").SetFocus
objSess.findById("wnd[0]/usr/lbl[91,10]").caretPosition = 4
objSess.findById("wnd[0]").sendVKey 2
' Display sort dialog (41 = Ctrl+Shift+F5 = menu Edit > Sort in ascending order)
objSess.findById("wnd[0]/tbar[1]/btn[41]").press
' <<<< Here, how to select lines in left and right table controls?
' Downloading the file
fileobjSess.findById("wnd[0]/mbar/menu[5]/menu[5]/menu[2]/menu[1]").Select
objSess.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
objSess.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").SetFocus
objSess.findById("wnd[1]/tbar[0]/btn[0]").press
objSess.findById("wnd[1]/usr/ctxtDY_PATH").Text = ffilepath
objSess.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = ffilename
objSess.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 8
objSess.findById("wnd[1]/tbar[0]/btn[11]").press
Exit Sub
disperr:
MsgBox "Error occured while retrieving data", vbCritical + vbOKOnly
End Sub
To select a row by its position in a table control, you need to determine the name of the table control, then use the method tableControl.getAbsoluteRow(row) to get the instance of the row, and set its property Selected = True. Example :
tableControl = objSess.findById("wnd[1]/usr/tblSAPLSKBHTC_FIELD_LIST_820")
tableControl.getAbsoluteRow(6).Selected = True
Now, to answer your specific question (ALV sorting in SM37), you also need to press additional buttons to select the Sort fields.
The Sort dialog screen is made of two table controls. You may first press the button "Hide all fields" to move all fields to the right table control (fields not selected), then select the fields on the right table control (as explained in the first paragraph) and press the buttons "Show sel. fields (Ctrl+F3)" and "Copy (Enter)".
You need to know the row number of the field you want to select in the right table control. In an ABAP 7.52 system, these fields are listed in this sequence (first one is row 1, second one is row 2 and so on):
Job name
Spool list
Job documentation
created by
status
start date
start time
duration
...
You also need to know the name of the right table control. For that, you may select the menu System > Status, double click the number of "screen/dynpro" which displays the technical names of screen elements. In your case, you see TC_FIELD_LIST_820. You also see that the screen belongs to the program SAPLSKBH.
The final program would look like this:
ffilepath = "your path here"
ffilename = "Test.txt"
With objSess
.findById("wnd[0]").Maximize
.findById("wnd[0]/tbar[0]/okcd").Text = "/nsm37"
.findById("wnd[0]").sendVKey 0
.findById("wnd[0]/usr/txtBTCH2170-JOBNAME").Text = "*"
.findById("wnd[0]/usr/txtBTCH2170-USERNAME").Text = "*"
.findById("wnd[0]/usr/chkBTCH2170-SCHEDUL").Selected = False
.findById("wnd[0]/usr/chkBTCH2170-READY").Selected = False
.findById("wnd[0]/usr/chkBTCH2170-RUNNING").Selected = False
.findById("wnd[0]/usr/chkBTCH2170-FINISHED").Selected = False
.findById("wnd[0]/usr/chkBTCH2170-ABORTED").Selected = True
.findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").Text = Format(Now() - 1, "mm/dd/yyyy")
' .findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").Text = Format(Now() - 1, "dd/mm/yyyy")
.findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").Text = Format(Now(), "mm/dd/yyyy")
' .findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").Text = Format(Now(), "dd/mm/yyyy")
.findById("wnd[0]/tbar[1]/btn[8]").press
' Display sort dialog (41 = Ctrl+Shift+F5 = menu Edit > Sort in ascending order)
.findById("wnd[0]/tbar[1]/btn[41]").press
' Remove all previous sort fields (press pushbutton named "APP_FL_ALL")
.findById("wnd[1]/usr/btnAPP_FL_ALL").press
' Select sort field(s) (row 6=start date, row 7=start time)
with .findById("wnd[1]/usr/tblSAPLSKBHTC_FIELD_LIST_820")
.getAbsoluteRow(6).Selected = True
.getAbsoluteRow(7).Selected = True
end with
' Press pushbutton "Show sel. fields"
.findById("wnd[1]/usr/btnAPP_WL_SING").press
' Press key "Enter" (corresponds to pushbutton "Copy (Enter)")
.findById("wnd[1]/tbar[0]/btn[0]").press
' Downloading the file
.findById("wnd[0]/mbar/menu[5]/menu[5]/menu[2]/menu[1]").Select
.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[1]/usr/ctxtDY_PATH").Text = ffilepath
.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = ffilename
.findById("wnd[1]/tbar[0]/btn[11]").press
End With

VBA Checkbox uncheck event and multiple range load to listbox

I am working on a small project and have difficulties with the following code. got help here with the second part of the code before (intentionally included the code for load only for the first three items (adig, altay, ataysk) to check the code:
Dim adig(), altay(), altaykr(), amur(), arh(), astr(), bashk(), belgor(), bryansk(), buryat(), vladim(), volgo(), vologod(), fulllist() As Variant
adig = ActiveWorkbook.Worksheets("Cities").Range("adig").Value
altay = ActiveWorkbook.Worksheets("Cities").Range("altay").Value
altaykr = ActiveWorkbook.Worksheets("Cities").Range("altaykr").Value
fulllist =ActiveWorkbook.Worksheets("Cities").Range("fulllist").Value
If (Not ListofExcludelocations.Selected(0)) And Excludelocations.Value =True Then
For Each i In adig
NegKeyList.AddItem i
Next i
End If
If (Not ListofExcludelocations.Selected(1)) And Excludelocations.Value =true Then
For Each i In altay
NegKeyList.AddItem i
Next i
End If
If (Not ListofExcludelocations.Selected(3)) And Excludelocations.Value = True Then
For Each i In altaykr
NegKeyList.AddItem i
Next i
End If
For i = NegKeyList.ListCount - 1 To 0 Step -1
If Not IsError(Application.Match(NegKeyList.List(i), fulllist, 0)) Then
NegKeyList.RemoveItem i
End If
Next i
TextBox2.Value = NegKeyList.ListCount & " neg.keys"
Next j
End sub
the code loads items from each of the named ranges in array to the listbox1 when the checkbox is checked. This part works fine. I have difficulties with the following:
1. The second part of the code does not actually remove the items from the checkbox when it is unchecked. Could someone check what is wrong as I cannot understand it?
2. Each range (arh, astr, etc) contains different number of items. I need to make sure that if the listbox item is not selected the values from each range are loaded to listbox. The way it works now, obviously, is that I have to make separate If statement for each item. It makes the trick for little number of items, but I would like this code to be applicable for array, which has 70+ ranges in it. Can someone help me to change it in order not to need to make If statement for each item in array like it is now and rather work with For each...statement?
Found the solution by copying the values to hidden listbox.
For counter1 = NegKeyList.ListCount - 1 To 0 Step -1
For counter2 = 0 To Bcities.ListCount - 1
'InStr returns 0 when there's no match
If (CStr(NegKeyList.List(counter1))) = (CStr(Bcities.List(counter2))) Then
NegKeyList.RemoveItem (counter1)
Exit For 'Skip any more compares for the deleted Item
End If
Next counter2
Next counter1

Using excel vba to change the value of a dropdown menu on a website

I am writing an Excel macro to fill out a form on a website. I have written the code that populate the text boxes easily enough, and found code to chose radio boxes, but I am having problems with choosing info from dropdown menus.
Example 'Gender':
The combo box has three options:
Select / Male / Female
I've tried a few variations on this:
doc.getElementsByName("xs_r_gender").Item(0).Value="Male"
...but with no luck.
This is the web source code:
<td> <select name="xs_r_gender" id="xs_r_gender">
<option value="" selected>Select</option>
<option value="male">Male</option>
<option value="female">Female</option> </select></td>
Thanks.
doc.getElementById("xs_r_gender").selectedindex=1
seems to do the trick. (Where 1 represents male)
Though it means I will need to do alot of lookups to determine what the value is for the items in my dropdown. (Easy enough for Sex, where there are only two options, but I have some comboboxes with up to 50 options). If anyone knows of a faster solution, that'd be great. In the meantime, Ill start doing up some tables!!!
thanks.
Try below code assuming doc = ie.document
doc.getElementById("xs_r_gender").value = "Male"
Use this in your code to call the function below.
xOffset = SetSelect(IE.Document.all.Item("shipToStateValue"), "Texas")
doc.getElementById("shipToStateValue").selectedindex = xOffset
Then use this for your function
Function SetSelect(xComboName, xComboValue) As Integer
'Finds an option in a combobox and selects it.
Dim x As Integer
For x = 0 To xComboName.options.Length - 1
If xComboName.options(x).Text = xComboValue Then
xComboName.selectedindex = x
Exit For
End If
Next x
SetSelect = x
End Function
Thanks Stack, works for me! My solution to operate an IE HTML combobox drop down turned out to be two parts.
Part 1 was to click the pull down, here's code:
Dim eUOM1 As MSHTML.HTMLHtmlElement
Set eUOM1 = ie.document.getElementsByTagName("input")(27).NextSibling
eUOM1.Focus
eUOM1.Click
Part 2 was to choose and click the value, like this (*actual element name changed):
Dim eUOM2 As MSHTML.HTMLHtmlElement
Set eUOM2 = ie.document.getElementsByName("[*PutNameHere]")(0)
eUOM2.Value = "EA"
eUOM2.Click
Here are references:refs
You can try the querySelector method of document to apply a CSS selector of option tag with attribute value = 'male':
doc.querySelector("option[value='male']").Click
or
doc.querySelector("option[value='male']").Selected = True
Function SetSelect(s, val) As Boolean
'Selects an item (val) from a combobox (s)
'Usage:
'If Not SetSelect(IE.Document.all.Item("tspan"), "Custom") Then
'something went wrong
'Else
'continue...
'End If
Dim x As Integer
Dim r As Boolean
r = False
For x = 0 To s.Options.Length - 1
If s.Options(x).Text = val Then
s.selectedIndex = x
r = True
Exit For
End If
Next x
SetSelect = r
End Function
Try this code :
doc.getElementById("xs_r_gender").value = "Male"
doc.getElementById("xs_r_gender").FireEvent("onchange")
You can do something like this:
doc.getElementsByName("xs_r_gender").Item(1).Selected=True
or
doc.getElementById("xs_r_gender").selectedindex = 1
Where 1 is the male option (in both cases).
If the dropbox needs to fire some event in order to aknowledge your choice, it is likely that it will be the "onchange" event. You can fire it like so:
doc.getElementById("xs_r_gender").FireEvent("onchange")
If you ever want to be able to select an option based on the option's text you can use the function given by Lansman (here) .
Based on the same answer, if you want to call the option by it's value property (instead of the text, you can just change the line If xComboName.Options(x).Text = xComboValue Then to If xComboName.Options(x).value = xComboValue Then).
This should cover all bases.
Copy from Here till last line:
Sub Filldata()
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For X = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(X).document.Location
my_title = objShell.Windows(X).document.Title
If my_title Like "***Write your page name***" Then
Set IE = objShell.Windows(X)
Exit For
Else
End If
Next
With IE.document.forms("***write your form name***")
' Assuming you r picking values from MS Excel Sheet1 cell A2
i=sheet1.range("A2").value
.all("xs_r_gender").Item(i).Selected = True
End with
End sub

Resources