First off: Thanks a lot for this web site and all the contributions you guys make! I found a lot of help here for the use of VBA, which I practise in MS Excel.
This time I was a little stuck... :-) Now it works (10.01.2017), see code below.
I try to keep it very brief here. If more details are needed, please tell me. Situation:
- Multiple
.csvfiles as output of an analysis machine ; - Headers = first line, analytical values starting from 2nd line;
- Number of headers in the first line variable (depending on meas. method);
- Names of the headers always identical (elements and sigma Errors);
- Only selected columns needed (elements);
- In order of their atomic weight (alphabetical doesn't make sense here);
- Merged in a single Excel sheet (= new output sheet) by a click-buttons and MsgBoxes guided VBA worksheet for the "apply only" user.
Further info:
I am not a programmer or developper but a self-taught VBA novice.
What did I have so far?
A VBA file which was able to:
- Import selected (user select, by prompt window)
.csvfiles and apend them after one another.
--> Thanks to this guys "How-To": http://www.rjsherman.com/vba/2014/import-txt-or-csv-file-into-excel-via-a-macro-querytable/.
Trouble was that the code I used afterwards to copy data - in order to create a usable data sheet for me - was non flexible and needed the entire data from the .csvfiles to be sortet alphabetically.
In case of chemical elements, this was bugging me.
Thanks to the community here I could find the code below and only had to find a way to make it loop through all my data.
I achieved this by adapting nCopyRow = ActiveCell.Row to being a loop where nCopyRow = i and i is looped from row 2 to LastRow.
The comparing/pasting code I've found in this forum is the following: https://stackoverflow.com/users/3561813/user3561813. Thank you for that!
Now here is the minimally adapted version:
Sub b_compareandpaste()
'
'Thanks to user3561813 from Stackoverflow for providing a solution to this problem as defined here:
'https://stackoverflow.com/questions/28055026/storing-headers-in-string-array-vba
'Define variables
Dim wsOrigin As Worksheet
Dim wsDest As Worksheet
Dim nCopyRow As Long
Dim nPasteRow As Long
Dim rngFnd As Range
Dim rngDestSearch As Range
Dim cel As Range
Dim LastRow As Long, i As Long
'Define LastRow in Sheet(2) of the ActiveWorkbook
LastRow = ActiveWorkbook.Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row
Const ORIGIN_ROW_HEADERS = 1
Const DEST_ROW_HEADERS = 1
Set wsOrigin = Sheets("Kontrolltabelle")
Set wsDest = Sheets("RoHS-Tabelle")
'Here only the active cell got copied and sorted into the new columns
''nCopyRow = ActiveCell.Row
'This had to be modified in order to loop through all results from the *.CSV Import!
For i = 2 To LastRow
nCopyRow = i
'setting up a nested loop to go through all rows provided by the .CSV file
nPasteRow = wsDest.Cells(Rows.Count, 1).End(xlUp).Row + 1
'
Set rngDestSearch = Intersect(wsDest.UsedRange, wsDest.Rows(DEST_ROW_HEADERS))
For Each cel In Intersect(wsOrigin.UsedRange, wsOrigin.Rows(ORIGIN_ROW_HEADERS))
On Error Resume Next
'the LookAt parameter had to be defined here as xlWhole and not left to standard xlPart. Otherwise Index became Indium (In) and
'Units became Nickel (Ni) :-)
Set rngFnd = rngDestSearch.Find(cel.Value, LookAt:=xlWhole, MatchCase:=True)
'
If rngFnd Is Nothing Then
'Do Nothing as Header Does not Exist
Else
wsDest.Cells(nPasteRow, rngFnd.Column).Value = wsOrigin.Cells(nCopyRow, cel.Column).Value
End If
'Here is why my sheet1 got filled with 0's, when the loop on the nCopyRow failed: :-)
On Error GoTo 0
Set rngFnd = Nothing
Next cel
Next i
'
End Sub
Now with this adaptation of nCopyRow = ActiveCell.Row into a loop, all rows provided by a .CSVFile containing data are checked and copied to another sheet.
The copying is done only when the headers of the origin and destination sheet "overlap", which means user written headers in the destination sheet allow copying of only relevant data.
Thanks to the community, coulnd't have done it without you and I hope this bit may help someone else.
Greetings, A.
EDIT5:
Made it! Adapted the entire post accordingly.
Thank's everyone.