0

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 .csv files 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:

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.

4
  • 3
    Can you supply the code you attempted? Thanks Commented Dec 12, 2016 at 14:55
  • Added the current code (LEGO bricked). I found a possible solution to the flexibility problem provided by @user3561813 here: link see second answer. Now, I tried the code and it works beautifully with numbers and with the frist line starting from activecell. If user3561813 reads this: Could your code be modified to include cells filled with text? And make it loop through a (previously unknown) number of rows? Thanks Commented Dec 15, 2016 at 16:17
  • My bad! The code obviously also works for text. I just had to add the "LookAt:=xlWhole" argument to the Search to unscramble my table. Otherwise "Index" also became Indium (In) because of the standard xlPart. Now working on the loop for multiple rows... #workinprogress Commented Dec 15, 2016 at 16:49
  • Solved it! See above.It may fall in the "good enough for me" category but I think others might find this useful. Thanks again to @user3561813 for the bulk of the code. If somebody knows a smooth way to deal with the same question using array's, I'm all ears ehrm eyes ;-) Cheers everyone, Commented Jan 10, 2017 at 15:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.