I have three columns (First Name , Last Name and phone Number) in my collection that I got from excel using "copy as collection" functionality of Excel VBO.
Please note that there is extra space at the end of First name and Phone number. I want to trim these spaces. Please suggest how to do that. Please also note that I have not defined the column names in my collection. I am directly copying it from excel so that if any changes done in excel then it reflect in the collection dynamically.
Thanks...
There are multiple action in collection manipulation object. You need to use read field action and rename field action with some logic.
You can for use action:
Object: Utility - Collection Manipulation
Action: Trim Collection
If you don't have it in your object, then you can add it to your object. Please see code below:
Dim i as integer
Dim j as integer
'For Each Column As DataColumn In Input_Collection.Columns
For j = 0 to Input_Collection.Columns.Count -1
For i = 0 to Input_Collection.Rows.Count -1
'dim x as string =CStr(Input_Collection.Rows(i)(j)).Trim()
Input_Collection.Rows(i)(j)= CStr(Input_Collection.Rows(i)(j)).Trim()
Next
Next
Output_Collection = Input_Collection
Or you may use
Object: Utility - Collection Manipulation
Action: Remove dot from headers
and in the code change "." to " "
Please find the exact code below and paste it in the code stage:
For Each Column As DataColumn In Input_Collection.Columns
Column.ColumnName=Microsoft.Visualbasic.Replace(Column.ColumnName," ","")
Next
Output_Collection = Input_Collection
Related
I am a newbie working on my first excel vba application. One of my user forms has a text box that the user enters data into. The data is likely to be a number that has leading zeros. I am placing the input in a string and trying to format it as text but both things I tried to not work. Any help would be appreciated.
Here are the two things I tried after search on line for how to format text in VBA code
txtString.NumberFormat = "#"
txtString.Value = Format(txtString.Value,"'0")
Thanks for any help.
More detailed question:
My application has 15 user forms and a workbook with 19 sheets in it. The first 5 sheets are excel worksheets that are used as databases. There are 2 worksheets that are inventory databases (account for 2 different types of inventory), there is a worksheet that tracks orders, there is a work sheet that tracks test results for products in inventory, and there is a worksheet to track the label information that must go on order. When the order is generated the user enters a package tag which is likely to be a number with leading zeros. The entry with leading zeros is stored in the orders database correctly. A different user from generates the label information that must go on the product. To do this the application displays orders that need labels and then when the user selects the order they want to generate the label the application searches the order database to get info to put on label and places this in a variable within the module associated with the generate label user form. It gets data in this fashion from each of the other databases to have all of the label information together. It then writes these variables to the database that has the label info in it. When it does this the leading zero get stripped off. I done several searches to find ways to do this and I have tried many of them and cannot seem to get any to work. I was hoping to fix this with the format method because I have to use it with other things I pull from the database like %s. The stripping of the leading zeros occurs when I store the value in the worksheet that has the label info. It does not matter if I set the cell in the label worksheet from a variable or directly from the orders workbook the leading zeros get stripped off.
Thanks!
Assuming your input is a string. Converts string to value you can work with. Calculates how many zeros to precede with in case it is not consistent.
Sub PrecedingZeros()
Dim strng As String
Dim lng As Integer
Dim fmt As String
Dim i As Integer
With Selection
strng = .Value
lng = Len(strng)
.NumberFormat = "#"
fmt = "0"
If lng >= 2 Then
For i = 2 To lng
fmt = fmt + "0"
Next i
End If
.NumberFormat = fmt
.Value = CSng(strng)
End With
End Sub
All
Thanks for your help. I ended up prepending a "'" to the text string every time I set my internal variable and that kept the leading zeros in place. This worked so I dropped the format idea.
Thanks again!
Bruce
How can I break up text within a cell with vba? I exported emails to an excel file using a vba and the information exported in one of the cells is formatted as seen below:
Name * xxxxxx
Country of residence * xxxxxx Email * xxxxx#gmail.com mailto:xxxxxxx#gmail.com
Mobile phone number * 0xxxxxx
Do you want to become a member of Assoc? Yes Check all that apply *
Members
Education
Ethical Conduct
Events
Regulation
I tried the solution below and it’s not working.
From article: If you need to build a formula to remove these line breaks all you need to know is that this ‘character’ is character 10 in Excel. You can create this character in an Excel cell with the formula =CHAR(10).
So to remove it we can use the SUBSTITUTE formula and replace CHAR(10) with nothing ( shown as “”).
https://www.auditexcel.co.za/blog/removing-line-breaks-from-cells-the-alt-enters/#:~:text=Building%20a%20formula%20to%20remove%20the%20ALT%20ENTER%20line%20breaks,-If%20you%20need&text=You%20can%20create%20this%20character,cell%20with%20no%20line%20breaks.
My understanding is that you dump an email into 1 excel cell and are hoping to separate a series of strings [Country, Email, Etc.] that are separated by a line break?
I suggest using the split function to separate the strings into an array, then loop through that array to put the information in the desired cells. Mind you this will only work if the items are in the same order everytime, if the order can change then you will need to add a data verification step. i.e. if inStr("#",[Range]) then its an email...
Split([string to split], [delimiter])
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/split-function
Dim strEmail as String 'Email dump
Dim arrEmail() as String 'Array for looping
Dim ItemsInArray as Integer 'Used to hold array count
Dim i as Integer 'Counter
strEmail = ActiveSheet.Cells("[Column,Row]") 'Cell your email dumps to
arrEmail = Split(strEmail, char(10)) 'Populate array
ItemsInArray = UBound(arrEmail) 'Get upper bound of array (total item count)
For i = 0 to ItemsInArray
ActiveSheet.Cells("[Column,Row]") = arrEmail(i)
Column + 1
Next i
when i = 0 its a country code
when i = 1 its an email
when i = 2 its a phone #
etc....
I am using xml in VBA which is taking data from an API. I have a nested XMl I am calling but one creates a new array. I can see how to add each nested xml to a cell in excel where they have one value but how would I send the array to cells so that each time an event happened it was added to the next column in the same row?
Debug.Print result("month")("day")("events")("elapsed")
elapsed is an array which holds a time for each event and there are multiple events per day maybe up to 10
WrkSht.Cells(Count, 1).Value = result("elapsed")
would print the first event but I would like the second event to appear in say next column and I cannot find how to code that etc
I am sure this is probably simple but I have searched and cannot see an explanation of how to handle this.
Thanks
This may work:
Dim a as Variant, b as Long
Set a = result("elapsed")
For b = 0 to UBound(a)
WrkSht.Cells(cnt, 1).Offset(,b).Value = a(b)
Next
If not, this most likely will:
Dim a as Object, b as Object, c as Long
Set a = result("elapsed")
For each b in a
WrkSht.Cells(cnt, 1).Offset(,c).Value = b
c = c + 1
Next
Also note that I changed the Count variable name to avoid confusion with the native Count method.
I have List A and B in excel and would like to compare ALL the items in List A with ALL the records in List B and if they match or partial match return the value of B in 3rd column. Hopefully demonstrated in the attached.
example
The easiest way to achieve it is to use VBA. Please find below example function which you can use in the same way as Excel functions:
Public Function findArea(item As String, areaRng As Range) As String
Dim i As Long
Dim ARR_area() As Variant
ARR_area = areaRng.Value2
For i = LBound(ARR_area) To UBound(ARR_area)
If (item Like "*" & ARR_area(i, 1) & "*") Then
findArea = ARR_area(i, 1)
GoTo endFunc
End If
Next i
endFunc:
End Function
Where:
item - Item which you want to check vs. areas
area - range of areas you want to check.
See usage example:
To achieve this result without you would need to format table to pivot view, where in rows you would have item and in rows area - as the value you can check matching for each combination. Nevertheless in this particular example I would recommend to use VBA.
Hope it helped.
I have two unordered sets of data here:
blah blah:2020:50::7.1:45
movie blah:blahbah, The:1914:54:
I want to extract all the data to the left of the year (aka, 1915 and 1914).
What excel formula would I use for this?
I tried this formula
=IF(ISNUMBER(SEARCH(":",A1)),MID(A1,SEARCH(":",A1),300),A1)
these were the results below:
: blahblah, The:1914:54::7
:1915:50::7.1:45:
This is because there is a colon in the movie title.
The results I need consistently are:
:1914:54::7.9:17::
:1915:50::7.1:45::
Can someone help with this?
You can use Regular Expressions, make sure you include a reference for it in your VBA editor. The following UDF will do the job.
Function ExtractNumber(cell As Range) As String
ExtractNumber = ""
Dim rex As New RegExp
rex.Pattern = "(:\d{4}:\d{2}::\d\.\d:\d{2}::\d:\d:\d:\d:\d:\d:\d)"
rex.Global = True
Dim mtch As Object, sbmtch As Object
For Each mtch In rex.Execute(cell.Value)
ExtractNumber = ExtractNumber & mtch.SubMatches(0)
Next mtch
End Function
Without VBA:
In reality you don't want to find the : You want to find either :1 or :2 since the year will either start with 1 or 2This formula should do it:
=MID(A1,MIN(IFERROR(FIND(":1",A1,1),9999),IFERROR(FIND(":2",A1),9999)),9999)
Look for a four digit string, in a certain range, bounded by colons.
For example:
=MID(A1,MIN(FIND(":" &ROW(INDIRECT("1900:2100"))&":",A1 &":" &ROW(INDIRECT("1900:2100"))&":")),99)
entered as an array formula by holding down ctrl-shift while hitting Enter would ensure years in the range 1900 to 2100. Change those values as appropriate for your data. The 99 at the end represents the longest possible string. Again, that can be increased as required.
You can use the same approach to return just the left hand part, up to the colon preceding the year:
=LEFT(A1,-1+MIN(FIND(":" &ROW(INDIRECT("1900:2100"))&":",A1 &":" &ROW(INDIRECT("1900:2100"))&":")))
Here is a screen shot, showing the original data in B1:B2, with the results of the first part in B4:B5, and the formula for B4 showing in the formula bar.
The results for the 2nd part are in B7:B9