I am developing macro in which i am expecting string such as (present in text file):
QUALAPP#QUALAPPC#GENRESOUS#NOMAPP#NOMAGENCE#PRENOMAPP#ADR1APP#ADR2APP~
ADR3APP#ADR4APP#VILLEAPP#CPAPP#PAYSAPP#TELAPP#NUMCONTRAT#DATEEFFCTR~
in which each field is delimited by # and each record is delimited by ~ to be in tabular format.
Such as each field will be in consecutive cell and new record start in new row.
I have macro which currently requires copying data in one cell. But as string size increases beyond some limit I am unable to paste it. So, please provide other easy option.
If the size of the string exceeds the capacitiy of a single cell, then read the data in one character at a time and parse the data into fields as you go.
Assign the string to a variable (let's call it "my_var") and then
arr_1 = Split(my_var, "~", -1, vbTextCompare)
For x = LBound(arr_1,1) to UBound(arr_1,1)
activecell = arr_1(x)
activecell.offset(1,0).select
Next
this creates an array and each element is one of your records; it then places each record in a new row. You could then go back through and split each row based on "#" and place the split data in adjacent columns.
Related
I have a userform that has a list of numbers entered in column style with no separator other than being on a separate line. Entry into the user form will look something like this:
2172223333
2172223334
2172223335
2172223336
2172223337
How do I use the SPLIT function to separate this bulk entry into the user form into separate cells in the same column on a spreadsheet? I'm familiar with how it works if there is a comma or space to look for between the terms but will it somehow recognize a separate line as a delineator and if so, what symbol do I use to represent that in the code?
I want to be able to read this input and have it entered in a workbook, say ("workbookA") and sheets(1) and put it in Column A starting at row 2 and going downward staying in column A.
Try this:
myString = ...
data = split(myString, vbcrlf)
workbooks("workbookA").worksheets(1).range("A2").resize(ubound(data)+1,1).value = application.transpose(data)
myString is the String with your data.
In VBA you have the constants vbLf, vbCr, vbCrLf for linefeed, carriage return and linefeed + carriage return (https://en.wikipedia.org/wiki/Newline), which represent the characters.
I have a column which contains productIDs which are similar to numbers
ProductIDS
46257547467
65464564656
47682542896
68683582757
I also converted it to text using =TEXT(cellNO,"0") and it works fine for that time. But next time after few hours when I open the file it reads that column as numbers like this:
ProductIDS
4.62E+10
6.54E+10
4.76E+10
6.86E+10
How can I permanently convert it to text so that when I open it every time it shows me the column as text ? Thanks.
It sounds like you're attempting to generate unique IDs within an Excel sheet (for use in another application, maybe...?). Using a formula here won't do much good if a) new rows are being added (particularly intermingled with existing rows), or b) the formula is based on the context of the sheet.
I've written a little sub that generates a unique ID for a given column, auto-populating it when something about that row gets populated. It works for a range of inserted rows too, not just the current row.
Private Sub Worksheet_Change(ByVal targetRange As Range)
' Confirm that the change is occuring where I want to track it
If targetRange.Column = Range("WhereChangeOccurs").Column Then
' Capture the target column holding the IDs
Dim IDRangeColumn As Integer
IDRangeColumn = Range("IDRange").Column
' Loop through each row of the target range, inserting an ID literal
For Each thisrow In targetRange.Rows
' update the ID with
' DateTime (to milliseconds) plus
' unique GUID based on system
Cells(thisrow.Row, IDRangeColumn).Value = Format(Now, "yyyy-mm-dd: hh.mm.ssss-") & Mid$(CreateObject("Scriptlet.TypeLib").GUID, 2, 18)
Next
End If
End Sub
It's pretty fast, and the uniqueness holds up even when inserting multiple rows (so far checked to about 50-100 rows at a time). This has the benefit of always generating a unique ID for a row, without needing Copy-Paste-Values; no human intervention required.
Note that the ", 18)" in the ID generation can be a longer number if you'd rather use that for uniqueness; I wanted something readable (date/time) at the beginning simply for human understandability.
I a newbee in field of excel formula and need your help in a complex formula, where i need to extract phone numbers from a string of random text. This does not have a fix format for the string
Example set off strings:
Dring to data add9724516002
add 08107936777 to me pler
8000069633 plz add. Me
9000088106 mujhe bhi add karo dosto
I have already tried many formulas but nothing seem to work. Only thing fixed is the length of number, it should be either 10 digits or 11 (including initial 0)
You could use a RegExp via VBA
(which seems to be coming to Excel as a formula option sometime down the track, see uservoice
code
Function GetCode(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "\d{10,11}\b"
If .test(strIn) Then
GetCode = .Execute(strIn)(0)
Else
GetCode = "no match"
End If
End With
End Function
If they all look like the strings you have provided above, you could use Text to Columns. Let's say all of those strings were in A1:A4.
Select all four cells
Data - "Text to Columns"
Delimited
Use "space" to separate values
Finish
You will now have a large majority of your phone numbers pulled out, and it will look something like this:
(I've added a row above the data that makes every column its own set of data.. Column 1,2,3,4,5,and 6. I've also added another column in place of column A, Sort. This will be useful at a later stage)
Next, select A1:G5.
Click "Insert - Table"
"My table has headers"
OK
Your range is now a table, meaning you can sort the data via ascending order. I'm assuming you have hundreds of strings that you're sorting through. When you sort via ascending order, all numbers will show up first.
In the pic below I've sorted the first column of actual data, and there are two phone numbers at the top I can pull out:
If you ever want to revert back to your original lineup of data, click the Sort column to "Ascending"
I hope this is a good workaround to avoid VBA. You may not get all of the phone numbers, but probably a good chunk. You can also copy and paste columns C:G to the bottom of column A and sort everything at once if you only need all of the phone numbers.
If the strings that have numbers and letters attached are similar, you can also look into the RIGHT and LEFT formulas to pull out the numbers from the alphanumeric strings
I have a table, let's call it my Individuals Table, much like the one below, containing a column of individuals along with their corresponding codes listed in an adjacent cell. Codes for each individual are all listed within the same adjacent cell next to the individual's name, and separated by a carriage return.
Example table
What I'd like to do is the following:
Run through the code cell for each individual
For each code in the individual's code cell, check if this code exists in a separate Codes Table
If the code exists in the Codes Table, add n+1 to the total count for that code in an adjacent cell and add the individual's name to the list of individuals with that same code in another adjacent cell.
If the code does not exist in the Codes Table, add the code to the Codes Table, add n+1 to the total count for that code in an adjacent cell and add the individual's name to the list of individuals with that same code in another adjacent cell.
Result of running the algorithm on the example table
If a similar program can achieve the same results, then I'm open to that too.
I try to give you a possible solution, by minimizing the use of VBA code.
As starting point I would do is rearranging codes for every individuals. Keeping more codes in a single cell separed by a return it is not as easy to manage like having a single code for each cell. Of course I will keep each code associated with each individual. A way to do it is with your data is by using the formula substitute and replace the returns characters with a semicolon. The formula works this:
=SUBSTITUTE(B2,CHAR(13),CHAR(59))
B2 is the cell where you are converting returns to semicolon. You will then use this formula for all values in your B column.
Once you have replaced returns with semicolon, copy and paste values and then with the function "Text to Columns" in Data tab you will convert each cell to a series of columns (depending on how many codes you had listed in the original cell of your sheet). Now you will be in a situation where the first column you have the names of individuals, and then on the same row in the subsequent columns you have all associated codes, like in the picture here below:
In order to create a complete list of all codes you can easily copy all columns with codes. Paste the codes in a suitable space (I suggest in a new worksheet), and then with some copy and paste jobs put all codes under the same column. Select all codes and with the button "Remove Duplicates" always in the Data tab you will have a list of all unique codes included in your original table.
Then you can copy and paste the column with all unique codes you created under your "Codes" column. Now you can count the codes in the converted table with this formula:
=COUNTIF($B$1:$C$4, D2)
Where first argument of COUNTIF refers to the codes in the converted table and the second argument is a code in your column "Codes" where you pasted the unique codes.
Now as far as I know there is no function in Excel to create a list of names separated by commas (but I would be glad to discover that it exists if anybody knows!!!). Therefore I created a custom one with some VBA code with the name List Individuals:
Function ListIndividuals(refCode, NameRange As Range, CodesRange As Range) As String
'Check size in row number of NameRange and CodesRange is same, otherwise give error
If NameRange.Rows.Count <> CodesRange.Rows.Count Then
ListIndividuals = CVErr(xlErrRef)
Exit Function
End If
result = ""
For Col = 1 To CodesRange.Columns.Count
For n = 1 To CodesRange.Rows.Count
If CodesRange.Cells(n, Col).Value = refCode Then
If CodesRange.Cells(n, Col).Value <> "" Then
If result = "" Then
result = NameRange(n)
Else
result = result & ", " & NameRange(n)
End If
End If
End If
Next
Next
ListIndividuals = result
End Function
So last step is to use this formula under your "Individuals" cells as follows:
=ListIndividuals(D2,$A$13:$A$16,$D$13:$E$16)
Where first argument is the Code, the second one is the list of individuals in the converted table (it should be the first column), then the third one are the columns with the codes in the converted table. As a result of this custom formula you will have the list of individuals separated by commas.
All above works on my computer, but if you need more information, please do not hesitate to contact me.
I am looking for help on filtering multiple values in one cell delimited by a comma.
For example I have a spreadsheet with the following data:
Column A (Risk) Column B (Risk Mitigation)
Risk A Requirement1, Requirement2
Risk B Requirement2, Requirement6, Requirement7
Risk C Requirement1, Requirement3, Requirement9
When I filter on the 'Requirement Mitigation' Column I would like to see check boxes for the following:
Requirement1
Requirement2
Requirement3
Requirement4
Requirement5
...
Requirement9
So for my example when I only check the Requirement 1 filter box, only 'Risk A' and 'Risk C' rows would be displayed
Currently when I filter it does by the unique cell value, which gives me
Requirement1, Requirement2
Requirement2, Requirement6, Requirement7
Requirement1, Requirement3, Requirement9
Please note that above is just an example and I have thousand of individual requirements, so it would not be as simple as having one column per requirement.
I open to any suggestions including creating vbscripts.
Any help would be greatly appreciated.
This will be tricky.
First you'll need to create a form that will contain a list box and populate that list box with the Requirements you have in their correct syntax.
Then you'll need to create a loop that will go through every cell in a column and save each of them to a string, you could make a one dimensional string array to save them all in that as well.
Then you'll need a loop that will go through each string and use the split function to seperate the strings with a delimeter, you'll need to set your delimiter to ", " to ensure it seperates the strings at the commas.
Then you'll need a loop that will run after you've made your selection from the listbox in your form. this loop will go through every string in your 2D array and check if it contains any of the requirements in your listbox. If it is not, run the code: Range("B" & x).EntireRow.Hidden = True where x is the row, you can use one of the variables in your loop for this.
If you need more specific information that this I may need to see what you've already done, it is possible as well to use this method to automatically populate the list box in your form.