Greeting, have a little bit of a dilemma. I'm making a VBA that will allow the user to open two separate text files and use them to graph data. So far, I've been successful with getting the two text files open in one spreadsheet. The problem I'm having is using the data from one text file (12 digit numbers) and comparing that to the other text file, which is a log of data. The first text file has a bunch of irrelevant information, but I only need to take the date and 12 digit number recorded. The second text file has a list of 12 digit numbers which I want to check against the first text file and get a count of how many times each specific number came up. Using that, I want to just have a regular count of times it has occurred. I need this to be a VBA because the user that will be opening this macro won't have any programming knowledge so it needs to be very simple.
Also to note, the badge numbers CAN be different based on the text file uploaded, but they are always added in the same located of the spreadsheet.
Thanks.
EDIT: Tried to make a rough sketch of what I'm talking about
| A | B | C | D |
| 1 | DATA WITH NUMBER| ...........|........... |NUMBER TO COMPARE WITH |
| 2 | DATA WITH NUMBER|
I'm not sure I fully grasp what you want, but it sounds like you want to use the Dictionary class in VBA.
You would need to add a reference to the MS Scripting Runtime library, and your code to read the second input file (to count occurrences) could look something like this:
Dim counts As Object
Dim splitValue As Variant
Dim inputLine as String
Set counts = CreateObject("Scripting.Dictionary")
Open "yourfile.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, inputLine
splitValues = Split(inputLine, vbTab)
If counts.Exists(splitValues(0)) Then
counts(splitValues(0)) = counts(splitValues(0)) + 1
Else
counts.Add splitValues(0), 1
End If
Loop
Close #1
Then, when you read the first file, you could essentially call the dictionary to find how many occurrences of the badge number appeared in the second file.
occurrences = counts(badgeNumber)
Again, I may be off. If you can clarify what both inputs look like and what you want the final output to look like, it would help.
Related
Hello I want some help with a code to search through the text file and to separate the text from a separator value followed by a rogue(stopping) value.
Each line of text in the text file is: for example with sample data:
DAC11010|This is a Desk and a chair|$100;.
In the above example has the | sign as a seperator value and ;sign as a rogue(stopping) value. The separator value separate the text DAC11010 , This is a Desk and a chair , $100 the rogue(stopping) value means that it it is the EOLN(end of line) and ready to check the next line.
The separated text goes to 3 ListBoxes and the data added one after the other. The DAC11010 goes to the lstItemCode , This is a desk and chair goes to the lstDescription , $100 goes to the lstPrice.
I would like if someone would volunteer to help me program the code in vb.net. I hope I made myself clear.
Thank you [shannon].
If you haven't already done so, please turn on Option Strict. This is a 2 part process. First for the current project - In Solution Explorer double click My Project. Choose Compile on the left. In the Option Strict drop-down select ON. Second for future projects - Go to the Tools Menu -> Options -> Projects and Solutions -> VB Defaults. In the Option Strict drop-down select ON. This will save you from bugs at runtime.
I will not write the code for you. That is not the purpose of Stack Overflow. We help with specific problems you are having with your code. You don't appear to have any code.
I will explain one way your goal might be accomplished.
Declare 3 New List(Of String) variables to hold the data for the three list boxes. We will be looping through the text files lines but we don't want to update the user interface until after the loop so we will collect the data in memory.
You will need to import System.IO to use methods of the File class. You can use File.ReadAllLines to get an array of strings; each member of the array will contain one line from the text file.
Next, you will want to loop through the lines splitting each line into 3 sections. Use the For Each...Next type of loop. You can use the String.Split method passing in your "|"c parameter. The c tells the compiler that this is a Char not a string because .Split is expecting a Char Now you can use the .Add method to add the sections to the appropriate list.
After the loop, you can assign the 3 list to the .DataSource property of the appropriate ListBox.
Now write the code and if you have any problems ask a new question showing the specific code where you are having the problem.
I am generating a document with itext7 in vb.net. The document is a payment reminder. The document contains 1 to many positions (I do not know how many positions the document will have at the end). Sometimes it could happen, that a position contains no amount. And I want to remove this position.
The reminder positions looks for example like this:
CustomerName | Amount |
---------------|-------------|
Jupiter Jones | 1,100.12 |
Peter Crenshaw | 0.00 |
Bob Andrews | 231.02 |
---------------|-------------|
Sum | 1,331.14 |
The lines are added to the table with
table.AddCell(cell1)
table.AddCell(cell2)
This example is very simplified...sadly it is because of the data structure and the calculations that have to be made, that I do not know whether the position line has to be printed before starting the line (the first cell of the line) or not.
So is it possible to "remember" a kind of index or handle or anything else, so that I am able to remove certain objects from the itext7 document before I "send" it to the renderer?
The table should look like this after the "removement":
CustomerName | Amount |
---------------|-------------|
Jupiter Jones | 1,100.12 |
Bob Andrews | 231.02 |
---------------|-------------|
Sum | 1,331.14 |
I hope you understand, what I mean...
To clarify some facts and answer the questions blow:
I wrote a wrapper-class (dll) for itext7 to create different types of pdfs with it. A whole table-creation contains of
obj.TableBegin({Cell1Width, Cell2Width})
while objMySqlDataReader.Read
Dim calculatedAmount as Single = 0
//do some calculations
objCell1.Text = objMySqlDataReader("CustomerName")
objCell2.Text = calculatedAmount
obj.TableCreateRow({objCell1, objCell2})
End While
obj.TableEnd()
Method TableBegin
Private Sub TableBegin(ByVal ColWidths as Single())
table = new Table(ColWidths)
//some formattings and so on
End Sub
Method TableCreateRow
Private Sub TableCreateRow(byval arrCells as MyOwnCellType())
For Each varCell As MyOwnCellType In arrCells
Dim c As Cell = New Cell()
Dim p As Paragraph = New Paragraph(varCell.Text)
c.Add(p)
//followed by some formatting lines like c.SetBackgroudColor
//or c.SetBorder
table.AddCell(c)
Next
End Sub
Method TableEnd
Private Sub TableEnd()
document.Add(table)
End Sub
The moment when I know whether a row as to be printed or not is variable. Sometimes I know it just after "printing" both cells of the row. But it could happen that I want to remove a table-row after the table has been "finished".
In another "document-type" a have a very similar problem, but there I need to remove a whole page of the document including Paragraphs, Tables, Images....
My utopian idea was, that I "save" the handle of each "possible to be removed object" in a list or array and after recognizing wheter the objects have to be printed or not I can either clear this array or remove all objects within this array by something like document.remove(handle)
Another utopian idea was, that I "collect" all "possible to be removed / printed objects" in any kind of array or list and after recognizing whether printing or not I can "push" these objects to the document or discard them.
It is difficult to discribe and even much more difficult to understand why I do not know "printing or not" before printing. But believe me - I do not know earlier. ;-)
Any idea or help is very appreciated.
I am here for seeking any advice or opinion as I want to loop through every excel files in folder. As you can see from my attached picture, my excel files are different both in file types (.xls <> .xlsx) and filename (especially on 2018). I also need to loop through "Revised" or "revised" files as well since it is possibly that any file will be revised next time.
And yes, I also did some research on this. My understanding is I need to modify all of the file names into the pattern one before build up a VBA to loop. At first, I thought about decomposing all filenames and put it back in pattern form, but it sound too idealistic. Another way is using the date in each file to label the workbook name, but again I found that those date had different styles. Some files label the date by using string such as "January 2012" or "March 2014", while the others using the date form such as "19/08/2013".
Therefore, I would be appreciated if anyone could suggest me on;
How can I handle with the different file name (.xls and .xlsx) within the same VBA?
How should I deal with these different file names (some files have "revised" at the back; some do not have "-" between "Cons" and date; and some use month name instead of number)? Are they any pattern that I overlook?
Please noted that I am just a newbie VBA coder, so it would be great if you left your answers with an explanation or any kind of examples.
Many thanks.
--------------------------------------------------------[EDIT]-------------------------------------------------------------------
First of all sorry for my poor explanation before. I provided too few information to understand overall picture. Let's start this over again.
My data are about steel consumption which release from the authority
every month. My task is to gather all of these data (such as
production, import, export and consumption of every data in each
row) and generate into time series pattern (please see attached
excel screen)
As it is possibly that these data will be revised anytime, I thus
decide to download all of these file every time in every month (one
file per one month). In addition to those revised file, the
authority will unexpectedly rename those file for example, from
"Cons 201601.xlsx" into "Cons 201601 - revised.xlsx)". This make me
more difficult to work on this (please see attached folder for
reference).
Moreover, this authority seems to have a problem with file naming as
they had different pattern of filename in the past compare to the
present ones. Example is per below table; Cons 201701-Revised.xlsx
Cons 201710-Revised1.xlsx
Cons 201711.xlsx
Cons-200902.xls
Cons-201212_revise.xls
Cons-201401-revised.xls
I mention above file name in order to create a VBA to loop through
these file, select some content and paste into another workbook in
chronological order. This means that I cannot use "Loop while or Do
while function" in my VBA. At first I decided to use two integer
variables, both of which were set for years and months
(e.g. For i = 2009 to 2018 and For j = 1 to 12) in order to created the system of filename (such as filename = "Cons" & "-" & i & j). But,
as I stated before, non-patterned name by the authority had
prevented me from creating this kind of loop.
I also tried to use the date in cell B2 in figure 1 to label the
date in order to create the loop which I already explained before.
However, again, the authority did not use the same pattern to date
month and year. After I checked with many file, these are example of
the date style in cell B2 January 2012 (string)
February 2009 (string)
Jan-16 (1/1/2016 date in custom format)
Given above limitations, could you guys again suggest me any possible
way to create chronological loop so as to copy and paste data to another
workbook to form a set of time series data for each product?
Thank you for your kind help :)
Firstly, use FileSystemObject (include a reference to Microsoft Scripting Runtime in your VBA project) which has some helpful functions within it. You could always code your own, but why reinvent the wheel in this case?
Don't have time to codes something this morning, so here is the pseudocode:
Open a Folder using your known filepath
Loop through all the files in the Folder (For each f in Folder.Files
extract the date code from your filename (e.g. using RegEx)
Add to a collection (e.g. array or Dictionary item) of the filename and the extracted date code (your key).
(end loop)
Sort your collection based on the extracted date code
This now gives you an ordered list of files, which you can open in turn and extract the data. An added bonus is that the key in the collection gives you a consistent date representation which you can use as an index in your collated information.
If you just want to loop through all files in folder use this:
dim file as variant
file = Dir("<PathToFolder>")
While (file <> "")
'Your logic here
file = Dir
wend
I have a huge text file with lots of numbers divided into different sections and I want to extract only certain values. It is something like this
step 1
787268 4.29430E-01
787269 4.05248E-01
787270 3.99742E-01
787271 3.99136E-01
787272 3.98422E-01
787273 3.97019E-01
787274 3.95237E-01
step 2
787268 4.29430E-01
787269 4.05248E-01
787270 3.99742E-01
787271 3.99136E-01
787272 3.98422E-01
787273 3.97019E-01
787274 3.95237E-01
I want to copy into my excel file only the two columns in the step 2 section.
So I need a VBA code that allows me to search for a particular string and after it finds it copy and paste all the raws until the next step.
Any pieces of code?
Thanks
Stefano
You can use this website to help you find what you need, a simple Google search can go a long way.
I would suggest using an if found section to find the spot where you need to copy over.
Ex.
If (Range("A1").Value = "YOUR TEXT HERE") Then
'''' COPY OVER DATA
End If
I am looking for help in trying to do the following.
I have large amount of data in Excel (100K+ rows) which have many columns, in one of the columns (lets just say col A) there is a string of data in each cell that lists file path info indicating a java installation. I need to search within that string to find common characters that start and end the same but will have different data in-between, so wildcard would be needed to identify. After identifying the data in the string search for each cell in the specified column (col A) I need to copy and paste the data identified to a different column located just right of the data (col B) on same worksheet for each row. So would look something like this:
Example
COLUMN A (original data string)
C:\app\Java\jre7\bin\...
C:\Program Files (x86)\Java\jre6\bin\...
C:\app\JAVA\jdk1.7.0._21\jre\bin\...
C:\JAVA\JDK-1_5_0_16_i586\jre\bin\...
COLUMN B (copy & paste to here)
jre7
jre6
jdk1.7.0._21
JDK-1_5_0_16_i586
Would need wildcard search to pick up anything specific between the \ & \ that starts with letter j and would be followed by two letters which could be re or dk but would have to allow for character or number to follow this in any length. Would be something like \j??*#*\ for the search always starting with same first two characters, followed by two letters, then possibly another character or nothing at all before a version number, then can be no characters or many after the version number.
As you can see from examples I am trying to pick up the version info with version numbers in them and do not want to get dir info with jre or jdk only in them, since most of my data have these dir listed somewhere in the file path string.
Then copying this info and pasting into Col B as shown in example is what I am trying to do.
Any help would be greatly appreciated as this is a manual process that would benefit greatly from automating.
There's actually a really quick way to do this using formulas. You could combine all of these into one formula if you wish, but I spread it into four simple formulas with the fourth giving you the answer.
Assuming the first string is in cell A1:
B1 = =SEARCH("java",A1)
C1 = =FIND("\",A1,E1)
D1 = =FIND("\",A1,F1+1)
E1 = =MID(A1,F1+1,G1-F1-1)
E1 will have your answer. Autofill down the columns and then copy>paste values in column E and delete columns B-D.