I'm trying to make the inventory scan process a bit simple.
We scan the incoming shipment. The scanner sends a string to Excel something like 'XYZ123'.
What I'm trying to do is, instead of the user selecting the column manually, I'd like excel to send the scanned string to the respective column.
For example, let us say there are 3 possible columns 'PO', 'Item', 'SN'
PO will start with 'XYZ'
Item will start with 'ABC'
SN with start with 'LMN'
If the scanned value starts with 'ABC', it has to be sent to the 'Item' column.
Is this possible? I tried playing with the Search, Exact and few other formulas, nothing worked so far.
Thank you,
Sub test_inventory_sorting()
Dim v_arr(1 To 3) As String, v As Variant
v_arr(1) = "XYZ1234"
v_arr(2) = "LMN73456"
v_arr(3) = "ABCzxcv"
For Each v In v_arr
Debug.Print v; " goes to "; get_col(CStr(v))
Next v
End Sub
Function get_col(ByVal p_val As String) As String
If p_val Like "ABC*" Then
get_col = "SN"
ElseIf p_val Like "LMN*" Then
get_col = "Item"
ElseIf p_val Like "XYZ*" Then
get_col = "PO"
Else
get_col = "Invalid"
End If
End Function
Related
I'm working with government harmonized codes. They're formatted as 10 numbers with periods between the 4th and 5th characters, and six-seventh characters like this "1234.56.7890". I'm trying to do some validation work so when a user enters a number without the periods, which is often the case, it puts the periods in for them.
The harmonized code is a variable in this instance named dimmed tv as a string.
Here's the code I'm using:
tv = Format(tv, "####.##.####")
Problem is, when I input 1234567890, it converts it instead to 1234567890.. with the two periods at the end. Any idea how I can get it to convert it to 1234.56.7890 as my code implies it should?
I'd do it like this:
Sub tester()
Dim e
For Each e In Array("1234.56.7899", "123456.7899", "1234.567899", _
"1234567899", "123A567899", "123456789")
Debug.Print e, ValidTv(e)
Next e
End Sub
'check format and return normalized value if possible
' return empty string if valid value can't be created
Function ValidTv(ByVal tv As String) As String
If tv Like "####.##.####" Then
ValidTv = tv
Else
tv = Replace(tv, ".", "")
If tv Like "##########" Then
ValidTv = Left(tv, 4) & "." & Mid(tv, 5, 2) & "." & Right(tv, 4)
End If
End If
End Function
I have through an API fetched my data as an XML, and I wish to cycle through nodes (there are several of the same type) and add them to certain fields/a table.
Example from the XML-file:
<HistRating
xmlns="">
<EndrAr>2020</EndrAr>
<EndrMnd>7</EndrMnd>
<Rating>A</Rating>
</HistRating>
<HistRating
xmlns="">
<EndrAr>2019</EndrAr>
<EndrMnd>6</EndrMnd>
<Rating>A</Rating>
</HistRating>
I have tried the following format (at this point the XML I need is in a string in xmlDoc xmlDoc = CreateObject("MSXML2.DOMDocument.6.0"). Fully aware that this is not a really "sexy" way to write it, but I'm new at this game:
Set nodeXML = xmlDoc.getElementsByTagName("EndrAr")
Range("G1").Value = nodeXML(1).Text
Range("H1").Value = nodeXML(2).Text
Range("I1").Value = nodeXML(3).Text
Set nodeXML = xmlDoc.getElementsByTagName("EndrMnd")
Range("G2").Value = nodeXML(1).Text
Range("H2").Value = nodeXML(2).Text
Range("I2").Value = nodeXML(3).Text
Set nodeXML = xmlDoc.getElementsByTagName("Rating")
Range("G3").Value = nodeXML(1).Text
Range("H3").Value = nodeXML(2).Text
Range("I3").Value = nodeXML(3).Text
This works great as long as all three items are there. Unfortunately that is not given. If it is a new company i.e. (3) wont exist (there is one line per year above), and I would like to either set the cell to Blank or No value or something.
The result from when I run the above code:
But if I try to add a line 4 to test what happens if value does not exists I get the following (for obvious reasons)
What I would love some help with is:
Can I by some "magic" add a ifmissing (tried it, but could not get it to work)?
Other ways to add a if variable is not found, input following into cell
Or are there a complete different way I should have solved this?
This is to add accounting data from last X available years (where X is ie 4, or less if not 4 is available) from 30 nodes.
You could use an Error trapping Function. Note in the code below we choose not to use the returned boolean.
Dim myTest as String
.
.
TryReadingXmlNode nodeXML,1, myText
Range("G1").Value = myText
.
.
Public Function TryReadingXmlNode(ByVal ipNode as object, ByVal ipIndex as Long, ByRef opText as string) as boolean
On Error Resume Next
opText=ipNode.Item(ipIndex).Text
TryReadingXmlNode=Len(opText)>0
If err.number>0 then opText="NoValue"
on Error Goto 0
End Function
Start by querying all of the HistRating elements, then loop over that collection:
Const MAX_YEARS As Long = 4
Dim ratings, rating, c As Range, i as Long
Set c= Range("A1")
Set ratings = xmlDoc.getElementsByTagName("HistRating")
For Each rating in ratings
c.offset(0, i) = rating.getElementsByTagName("EndrAr")(0).Text
c.offset(1, i) = rating.getElementsByTagName("EndrMnd")(0).Text
c.offset(2, i) = rating.getElementsByTagName("Rating")(0).Text
i = i + 1
If i >= MAX_YEARS Then Exit For 'exit if processed enough nodes
Next rating
I have a list of text strings (titles) and a list of "bad" keywords I want to identify within those titles.
The problem I've run into is how to identify individual text within the string. Several simple formulas can do this for an exact match, but I need it to identify words within the titles.
For example, in "Software sales representative" I need it to recognize "software" is on the list and return the word "bad".
I've browsed other questions on here and there's nothing that answers this specifically on Stacked.
The other similar questions have said this can be done with a asterisk/ wildcard, but this does not work. I know conditional formatting can do this, but I need a formula since I have over 100 "bad" keywords.
This is as far as I've gotten:
=IF(COUNTIF(K:K,"*"&E2&"*"),"Bad","Okay")
But this formula still only returns exact matches.
Column E is the titles. Column K is the keywords.
You will need to use this formula:
=IF(SUMPRODUCT(--(ISNUMBER(SEARCH($K$1:INDEX(K:K,MATCH("zzz",K:K)),E2)))),"Bad","Okay")
Put this Function in a module and call it from a cell like this =ContainsBadwords(badwordsrange,rangetotest;false;false)
Public Function ContainsBadwords(BadWordList As range, ByVal SuspectRange As range, Optional ExactMatch As Boolean = True, Optional CaseSensitive As Boolean = True)
For Each ArrayItem In SuspectRange
If Not CaseSensitive Then ArrayItem = Strings.lcase(ArrayItem)
For Each Subwhat In BadWordList
If ExactMatch Then
test = (ArrayItem = Subwhat)
Else
test = InStr(ArrayItem, Subwhat) >= 1
End If
If test Then: ContainsBadwords = test: Exit Function
Next Subwhat
If test Then: ContainsBadwords = test: Exit Function
n = n + 1
Next ArrayItem
End Function
Returns TRUE for blasphemy or FALSE if no bad words exist.
At work I am required to reformat incorrect Addresses on a weekly basis from records in our Salesforce instance. We gather the incorrectly formatted addresses using a Report and export them to an Excel file. My job is simply to manipulate the data in the file to format them properly then reinsert them into the database.
Typically the addresses are formatted as so:
5 Sesame Street, Anytown, Anyplace
Separating these can be done easily by hand, but I typically have to work with hundreds of addresses at a time, and using default excel formulas tends to require lots of wrangling multiple cells at once to break it up into fragments.
Thus I wrote a custom formula to run through the cell and return a specific fragment of the string based on the "Comma Number" given. So if I give a Comma Number of 1, I would get "5 Sesame Street", 2 would get me "Anytown", etc.
Here is my code so far:
Public Function fragmentAddress(address As String, numberofcommas As Integer) As String
seen = 1
lastComma = -1
Dim x As Long
Dim frag As Long
For x = 0 To Len(address)
If Mid(address, x, 1) = "," & numberofcommas = seen Then
Exit For
ElseIf Mid(address, x, 1) = "," & numberofcommas <> seen Then
seen = seen + 1
lastComma = x
End If
Next
frag = Mid(address, lastComma + 1, seen - lastComma)
fragmentAddress = frag
I have not implemented the ability to handle the final value yet, but it does not give me any outputs, only outputting a "#VALUE!" error when I attempt to give it the input
=fragmentAddress("3 Ashley Close, Charlton Kings",1)
I have some experience with programming, but this is my first time writing anything in VBA.
Any help would be appreciated, thank you.
Not exactly sure what your question is, but this is simpler:
Public Function GetAddressFragment(ByVal Address As String, ByVal Index As Integer) As String
Dim addr() As String
addr = Split(Address, ",")
On Error Resume Next
GetAddressFragment = Trim(addr(Index - 1))
End Function
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