Hello I'm trying to automate a value into a website that has two list boxes in it.
For the first List box this code works, However It doesn't work for the second list box. I can't just copy and paste this code below it for the second one even if I change the variable "a". Any help is appreciated!
For a = 1 To cats.Options.Length
If cats.Options(a).Text = "Option One" Then
cats.selectedindex = a
Exit For
End If
Next a
If you're working with multiple lists then you should come up with a re-useable block of code which you can call for any list+value.
Something like:
Function SetByTextValue(lst as object, v as string) As Boolean
Dim a as long
For a = 0 To lst.Options.Length - 1
If lst.Options(a).Text = v Then
lst.selectedindex = a
SetByTextValue = True
Exit Function
End If
Next a
End Function
Then in your main code you can do something like:
If Not SetByTextValue(cats, "Option One") Then
'not found
Else
'...proceed with next list
End If
Related
I am currently attempting to turn a form I am working on into a more dynamic one using vba in word, but I am facing two issues with the option buttons within a repeating section:
The code is not dynamic; when I run the code it does what I need it to but doesn't dynamically recalculate as I change my choice.
The option buttons do not repeat when I add a new section, and the only way for me to include them is by readding them and creating a new module specific for the new option button group.
Below is a picture of the section I am repeating and the code I am using.
enter image description here
Private Sub Yes_Click()
Dim k(0 To 3) As String
k(0) = "Select one"
k(1) = "Pass"
k(2) = "Fail"
k(3) = "N/A"
Dim i As Long
If Yes = True Then
Me.Controls.Clear
Me.Controls1.Clear
For i = 0 To 3
Me.Controls.AddItem k(i)
Me.Controls1.AddItem k(i)
Next i
End If
On Error Resume Next
Me.Controls = "Select one"
Me.Controls1 = "Select one"
If Yes = False Then
Me.Controls = "N/A"
Me.Controls1 = "N/A"
End If
On Error Resume Next
End Sub
Is there a way to approach either issues?
Thanks in advance.
I am struggling with a simple thing and cannot resolve it. I have a userform that user can populate from a textbox manually. I decided to add a checkbox as well to allow the user to populate the same listbox with a specific list of items. To do it,I made a simple checkbox with array. It works perfectly fine. But obviously keeps adding the items every time you check and uncheck it.
Private Sub Checkbox1_Click()
Dim mylist(7) As String
Dim i As Long
mylist(0) = "time"
mylist(1) = "hour"
mylist(2) = "how"
mylist(3) = "test"
mylist(4) = "number"
mylist(5) = "sent"
mylist(6) = "memo"
mylist(7) = "value"
For i = 0 To 7
If CheckBox1.Value = True Then
Finallist.AddItem mylist(i)
End If
Next i
End Sub
I can populate the list when the checkbox is checked with the code above, but struggling to remove the array items from the list when the user unchecks the listbox. I simply need to remove the same items from listbox when user unchecks the same checkbox.
I tried the following solution after the code, but seem to be making something very wrong with it, I understand. Just totally stuck....Could someone help me please?
If checkobx.value=false then
For i = 0 To 7
For j = 0 To FinalList.ListCount - 1
If InStr(Final.List(j), mylist(i)) > 0 Then
Finallist.RemoveItem mylist(i)
End If
Next j
Next i
end if
Try this (explanations in comments and untested):
If CheckBox1.Value Then ‘ if checkbox checked
For i = 0 To 7
NegKeyList.AddItem interrlist(i)
Next
Else ‘otherwise
Dim i As Long
For i = NegKeyList.ListCount - 1 To 0 Step -1 ‘ loop through listbox items from last one backwards
If Not IsError(Application.Match(NegKeyList.List(i), interrlist,0)) Then NegKeyList.RemoveItem i ‘ if current listbox item matches any interrlist array item then remove it from listbox
Next
End If
I am working on a VBA userform that includes ListBoxes.
So far, when I had to manipulate one or more, I always proceeded like this in my subs, with dlg as the dialogbox name, and it did not pose any problem, given that I never wanted to do anything complicated:
Dim List1 As Object
...
List1 = dlg.GetControl("CBXname")
...
List1.addItem("String",index)
...
Now I would like to do the following in this Sub
...
If (List1.Exists(Cell1.String) = False) Then
List1.addItem(Cell1.String,k)
End If
...
List1.Clear
...
But I can do neither since List1 is an Object. However, if I decide to declare List1 as a Listbox instead, I do not know how to get the proper control on the ListBox from the dialogbox (the current getcontrol gives me an error).
One of the issues with your code is that listbox objects do not have an "exists" property. To check if a value already exists in your listbox items, you will need to loop through the items.
dim i as integer
for i = 0 to List1.listcount - 1
if List1.column(0, i) = myvalue then
'myvalue exists in List1, skip
else
List1.additem myvalue
end if
next i
Where myvalue is whatever value you are trying to add to the listbox. But that brings us to the second issue in your code which is where you add "Cell1.String". If you are trying to add a value from a worksheet range you will need to refer to that range's value, as worksheet ranges do not have a "string" property as you use it here. Ie. Cell1 = Range("A1").value
As for getting control of the listbox, you can simply refer to the objects name as an object of the form. For example, dlg.List1, if the object's name is List1.
Here is a general purpose routine you can call for any list box. The calling code assumes a list box called ListBox1, a text box called TextBox1, and a Command Button called CommandButton. When you click on the button, it searches the listbox for the text from textbox1.
Private Function ExistsInListbox(ByRef aListBox As msforms.ListBox, ByVal Item As String) As Boolean
Dim booFound As Boolean
booFound = False
Dim t As Integer
ExistsInListbox = False
For t = 0 To aListBox.ListCount - 1 'correction, aListBox not ListBox1
If Item = aListBox.List(t) Then
'if we find a match, short-circuit the loop
booFound = True
Exit For
End If
Next
ExistsInListbox = booFound
End Function
private sub CommandButton_click()
Dim answer As String
Dim val As Boolean
val = ExistsInListbox(Me.ListBox1, TextBox1.Text)
If val Then
answer = "found"
Else
answer = "Not Found"
End If
MsgBox "found-" & answer
End Sub
I need to select all the text in a textbox of an Access form when I click (or double click) into it. i tried the following code, unsuccessfully:
Me.txt_CompraPreco.SelStart = 0
Me.txt_CompraPreco.SelLength = Len(Me.txt_CompraPreco)
thanks in advance.
You can use the code shown below. If it doesn't work, place a breakpoint at the first line of code. If it doesn't stop on your breakpoint, then your event is not recognized.
Option Compare Database
Option Explicit
Private Sub txt_CompraPreco_Click()
If Len(Me.txt_CompraPreco & "") = 0 Then Exit Sub
Me.txt_CompraPreco.SelStart = 0
Me.txt_CompraPreco.SelLength = Len(Me.txt_CompraPreco)
End Sub
I was looking for a solution regarding this problem, I have the same issue, however, I have a solution to it, I'm not sure if it's efficient, but here's my code:
'Declare a flag
Public flagDblClick As Boolean
'Double click event
Private Sub txtbox_DblClick (Cancel As Integer)
flagDblClick = True
End Sub
'Mouse up Event
Private Sub txtbox_MouseUp(Button As Integer, Shift As Integer, X as Single, Y as Single)
If flagDblClick Then
flagDblClick = False
txtBox.SelStart = 0
txtBox.SelLength = Len(txtBox.Value)
End If
End Sub
This code will resolve your problem (use with userform).
txt_CompraPreco.SetFocus
Me.txt_CompraPreco.SelStart = 0
Me.txt_CompraPreco.SelLength = Len(Me.txt_CompraPreco)
My trial and error found this.
If your textfield is formatted as a Standard Number and you have set the decimal places to a certain length, you will run into trouble when you enter a single digit. For example if your decimal places in the field properties is set to 2 and you enter "1", you will display "1.00". To get the entire field (1.00) selected, you must specify the .Text property when you determine the .SelLength (not the default .Value property)
Me.txtYourFieldname_GotFocus
Me.txtYourFieldName.SelStart = 0
Me.txtYourFieldName.SelLength = Len(Me.txtYourFieldName.Text)
End Sub
This works for me:
Dim bSelect As Boolean
Private Sub fieldX_Click()
If bSelect Then
'Select text only at first mouse click then user can click again
'and is able to put mouse pointer where he prefers
Me.fieldX.SelStart = 0
Me.fieldX.SelLength = Len(Me.fieldX)
bSelect = False
End If
End Sub
Private Sub fieldX_GotFocus()
bSelect = True
'Select text if field got focus via keyboard, Enter or TAB
'this is not enough if field got focus via mouse click
Me.fieldX.SelStart = 0
Me.fieldX.SelLength = Len(Me.fieldX)
End Sub
Ok, so I have a series of input boxes pop up for the user and I know I want to check if the user hits cancel or "X" then I will have to check its return value.
EX answer = inputbox("lelele") if answer = "" then end else end if.
My problem is I have 4 input boxes in a row and I don't want to have to do a separate if statement for every input box so is there a way I can check all three in some sort of try catch block or while loop?
Below is the code I am actually using. Keep in mind that I am trying to catch a cancel every step along the way, so any time anyone clicks cancel the program immediately stops running.
'column you want to first select for copying
ColSelect = InputBox("which column do you want to select ColCopyFrom")
'the column you are comparing it to
ColCompare = InputBox("which column do you want to compare to ")
'where you are copying data from
ColCopyFrom = InputBox("which column do you want to copy data ColCopyFrom")
'where you are copying data to
ColCopyTo = InputBox("which column do you want to copy data to")
<
This is what I want to do for every box
if ColSelect = "" then
exit
else
'do nothing
end if
if ColCompare = "" then
exit
else
end if
You could wrap the inputbox into a function passing the prompt as a parameter like this and use END to break:
Function myInputBox(prompt As String) As String
Dim ib As String
ib = InputBox(prompt)
If ...
'... do some checking here
Else
End 'stop dead here
End if
myInputBox = ib
End Function
On the other hand, you might think about using a UserForm instead for enhanced usability.
Could you create a function and call it to check for each input? By creating a function once you can call upon it as many times as necessary, in your case 4 times. When you call the function you can then substitute the variable to check against
'first inputbox
Call checker(ColSelect)
'second input box
Call checker(ColCompare)
'etc
This is the function to check and you will see the variable name is cancel. This will take the variable entered (in the example ColSelect) and then give it a new name. This can be anything you like
sub checker(cancel)
if cancel = "" then
exit
else
'do nothing
end if
end Sub