Home

Automatically transferring, organizing, and analyzing sets of data from one csv file into an xlsm

%3CLINGO-SUB%20id%3D%22lingo-sub-815726%22%20slang%3D%22en-US%22%3EAutomatically%20transferring%2C%20organizing%2C%20and%20analyzing%20sets%20of%20data%20from%20one%20csv%20file%20into%20an%20xlsm%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-815726%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20machine%20that%20automatically%20collects%20readings%20from%20sensors%20and%20puts%20them%20into%20a%20csv%20file.%20Each%20time%20I%20run%20my%20machine%2C%20it%20collects%2068%20(but%20sometimes%2069)%20rows%20of%20readings%20and%20puts%20them%20into%203%20columns%20(time%2C%20force%2C%20tension%20in%20that%20order).%20For%20example%2C%20the%20raw%20data%20for%20one%20set%20of%20readings%20would%20go%20from%20cell%20A1%20to%20C69%20if%20there%20are%2068%20readings.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20to%20be%20able%20to%20automatically%20copy%20this%20data%20into%20a%20.xlsm%20file%20where%20%3CSTRONG%3Eeach%20set%3C%2FSTRONG%3E%3CSTRONG%3Eof%20data%3C%2FSTRONG%3E%26nbsp%3Bcan%20then%20be%20automatically%20and%20separately%20analyzed.%20My%20critera%20for%20analysis%20are%20that%20a%20graph%20of%20data%20is%20generated%2C%20the%20maximum%20of%20the%20force%20column%20is%20displayed%2C%20and%20the%20difference%20between%20peak%20tension%20and%20initial%20tension%20is%20displayed.%3C%2FP%3E%3CP%3EThe%20raw%20data%20in%20the%20csv%20continuously%20adds%20to%20columns%20A-C%2C%20but%20when%20it%20is%20copied%20I%20would%20like%20each%20set%20of%20data%20to%20start%20every%204%20columns%20in%20the%20analysis%20template.%20So%20the%20first%20data%20set%20is%20A-C%2C%20the%20second%20is%20D-F%2C%20etc.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20logic%20I%20had%20in%20mind%20for%20the%20VBA%20would%20be%20a%20conditional%20that%20checks%20if%20the%20next%2068%20rows%20of%20the%20csv%20are%20empty%2C%20and%20if%20they%20are%20not%2C%20then%20the%20data%20is%20transferred.%20Then%2C%20another%20conditional%20would%20check%20if%20the%20cells%20in%20the%20analysis%20template%20are%20empty%2C%20and%20if%20they%20aren't%2C%20then%20the%20analysis%20criteria%20is%20generated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESomeone%20with%20a%20somewhat%20similar%20problem%20to%20me%20found%20this%20code%2C%20although%20I'm%20a%20beginner%20to%20VBA%20and%20do%20not%20know%20exactly%20how%20to%20modify%20it%20to%20meet%20my%20needs.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20CopyValueDown()%20%0A%0ADim%20lRow%20As%20Integer%0A%0A%20%20%20%20lRow%20%3D%20Sheets(%22Data%22).Range(%22A%22%20%26amp%3B%20Rows.Count).End(xlUp).Row%0A%20%20%20%20If%20lRow%20%26gt%3B%201%20Then%20%0A%20%20%20%20%20%20%20%20Sheets(%22Report%22).Range(%22A2%3AB2%22).AutoFill%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Destination%3A%3DRange(%22A2%3AB%22%20%26amp%3B%20lRow)%0A%20%20%20%20end%20if%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20main%20problem%20I'm%20having%20with%20this%20is%20that%20the%20data%20is%20sometimes%2069%20readings%20due%20to%20the%20extremely%20fast%20sensor%20read%20time.%20I%20have%20tried%20writing%20a%20line%20to%20the%20serial%20monitor%20that%20creates%20a%20space%20between%20each%20set%20of%20data%20so%20I%20could%20check%20for%20that%20to%20differentiate%20between%20data%20sets%2C%20but%20something%20about%20my%20baud%20rate%20causes%20it%20to%20print%20strange%20characters%20instead%20of%20a%20space%2C%20so%20I%20have%20to%20check%20if%20column%20B%20or%20C%20is%20empty%2C%20since%20A%20is%20still%20populated%20by%20strange%20characters.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20created%20a%20template%20for%20the%20data%20analysis%20file%20with%20just%20one%20set%20of%20data%20that%20I%20will%20attach.%20I%20will%20also%20attach%20the%20raw%20data%20csv%20file%20which%20contains%205%20readings%2C%20and%20also%20demonstrates%20how%20the%20readings%20are%20inconsistent%20and%20occasionally%20take%20up%2069%20rows%20instead%20of%2068.%20The%20readings%20can%20be%20differentiated%20by%20a%20human%20by%20the%20time%20resetting%20and%20the%20tension%20dropping.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-815726%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-815907%22%20slang%3D%22en-US%22%3ERe%3A%20Automatically%20transferring%2C%20organizing%2C%20and%20analyzing%20sets%20of%20data%20from%20one%20csv%20file%20into%20an%20xls%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-815907%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F395513%22%20target%3D%22_blank%22%3E%40wicked1591%3C%2FA%3E%26nbsp%3BHave%20you%20tried%20Power%20Query%3F%20Specifically%20the%20%3CA%20href%3D%22https%3A%2F%2Fsupport.office.com%2Fen-us%2Farticle%2FCombine-files-in-a-folder-with-Combine-Binaries-Power-Query-94b8023c-2e66-4f6b-8c78-6a00041c90e4%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECombine%20Binaries%3C%2FA%3Eoption%3F%20You%20can%20save%20your%20.csv%20files%20to%20a%20single%20folder%2C%20then%20use%20PQ%20to%20import%20and%20append%20the%20data%20to%20the%20reporting%20workbook.%20As%20you%20add%20new%20files%2C%20just%20refresh%2C%20and%20PQ%20will%20update%20only%20the%20newest%20additions.%20PQ%20will%20also%20append%20the%20filename%20to%20each%20dataset%2C%20so%20that%20will%20allow%20you%20to%20make%20your%20differentiation%20between%20them.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20will%20eliminate%20copy%2Fpasting%2C%20and%20you%20can%20create%20summaries%20with%20PivotTables%20and%20charts.%20If%20you%20add%20a%20slicer%20for%20dataset%2C%20then%20you%20can%20quickly%20switch%20views.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHTH%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-816036%22%20slang%3D%22en-US%22%3ERe%3A%20Automatically%20transferring%2C%20organizing%2C%20and%20analyzing%20sets%20of%20data%20from%20one%20csv%20file%20into%20an%20xls%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-816036%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F10827%22%20target%3D%22_blank%22%3E%40Smitty%20Smith%3C%2FA%3E%26nbsp%3B%20Thanks%20for%20the%20reply.%20I%20don't%20think%20power%20query%20is%20right%20for%20me%20because%20all%20of%20my%20data%20is%20coming%20from%20one%20csv%20file%2C%20and%20I%20don't%20really%20have%20a%20time%20efficient%20way%20to%20separate%20the%20data%20sets%20into%20different%20files.%3C%2FP%3E%3CP%3EI%20have%20done%20some%20thinking%20to%20simplify%20the%20problem%20though%2C%20and%20this%20is%20what%20I%20really%20need%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20to%20take%2068%20cells%20by%203%20columns%20from%20the%20csv%2C%20copy%20that%2C%20and%20paste%20it%20in%20a%20xls%20file%20every%204th%20column.%3C%2FP%3E%3CP%3ESo%2C%20it%20would%20take%20cells%20A1%3AC69%20and%20copy%20them%20to%20A1%20in%20another%20sheet.%20Then%2C%20cells%20A70%3AC137%20would%20copy%20to%20A4%20in%20the%20other%20sheet.%20The%20problem%20I%20have%20is%20that%20this%20example%20only%20transfers%202%20data%20sets.%20I%20know%20how%20to%20copy%20these%20specific%20cells%2C%20but%20what%20I%20don't%20know%20how%20to%20do%20and%20what%20to%20do%20is%20have%20it%20function%20for%20an%20indefinite%20number%20of%20cells.%20So%20every%2068x3%20cells%2C%20copy%20into%20the%204th%20column%20over%20in%20the%20same%20row.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20VBA%20function%20that%20would%20take%20X%20cells%20and%20cut%2Fcopy%20them%20to%20every%20Yth%20column%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-816081%22%20slang%3D%22en-US%22%3ERe%3A%20Automatically%20transferring%2C%20organizing%2C%20and%20analyzing%20sets%20of%20data%20from%20one%20csv%20file%20into%20an%20xls%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-816081%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F395513%22%20target%3D%22_blank%22%3E%40wicked1591%3C%2FA%3E%26nbsp%3BYou%20might%20need%20to%20play%20with%20the%20offsets%20a%20bit%2C%20but%20this%20should%20get%20you%20started%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20foo()%0A'%20%20%20Copy%20from%0A%20%20%20%20'%20%20%20A1%3AC69%0A%20%20%20%20'%20%20%20A70%3A%20C137%2C%20etc%0A%20%20%20%20%0A%20%20%20%20Dim%20i%20As%20Long%0A%20%20%20%20Dim%20x%20As%20Long%0A%20%20%20%20%0A%20%20%20%20Dim%20lr%20As%20Long%0A%20%20%20%20Dim%20lc%20As%20Long%0A%20%20%20%20%0A%20%20%20%20lr%20%3D%20Cells(Rows.Count%2C%20%22A%22).End(xlUp).Row%0A%20%20%20%20x%20%3D%2069%0A%20%20%20%20%0A%20%20%20%20For%20i%20%3D%201%20To%20lr%20Step%2069%0A%20%20%20%20%20%20%20%20lc%20%3D%20Cells(1%2C%20Columns.Count).End(xlToLeft).Offset(%2C%202).Column%0A%20%20%20%20%20%20%20%20Range(%22A%22%20%26amp%3B%20i%20%26amp%3B%20%22%3AC%22%20%26amp%3B%20x).Copy%20Cells(1%2C%20lc)%0A%20%20%20%20%20%20%20%20x%20%3D%20x%20%2B%2069%0A%20%20%20%20Next%20i%0A%20%20%20%20%20%20%20%20%0AEnd%20Sub%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
wicked1591
New Contributor

I have a machine that automatically collects readings from sensors and puts them into a csv file. Each time I run my machine, it collects 68 (but sometimes 69) rows of readings and puts them into 3 columns (time, force, tension in that order). For example, the raw data for one set of readings would go from cell A1 to C69 if there are 68 readings.

 

I want to be able to automatically copy this data into a .xlsm file where each set of data can then be automatically and separately analyzed. My critera for analysis are that a graph of data is generated, the maximum of the force column is displayed, and the difference between peak tension and initial tension is displayed.

The raw data in the csv continuously adds to columns A-C, but when it is copied I would like each set of data to start every 4 columns in the analysis template. So the first data set is A-C, the second is D-F, etc. 

 

The logic I had in mind for the VBA would be a conditional that checks if the next 68 rows of the csv are empty, and if they are not, then the data is transferred. Then, another conditional would check if the cells in the analysis template are empty, and if they aren't, then the analysis criteria is generated.

 

Someone with a somewhat similar problem to me found this code, although I'm a beginner to VBA and do not know exactly how to modify it to meet my needs.

 

Sub CopyValueDown() 

Dim lRow As Integer

    lRow = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
    If lRow > 1 Then 
        Sheets("Report").Range("A2:B2").AutoFill _
                  Destination:=Range("A2:B" & lRow)
    end if
End Sub

 

 

The main problem I'm having with this is that the data is sometimes 69 readings due to the extremely fast sensor read time. I have tried writing a line to the serial monitor that creates a space between each set of data so I could check for that to differentiate between data sets, but something about my baud rate causes it to print strange characters instead of a space, so I have to check if column B or C is empty, since A is still populated by strange characters.

 

I have created a template for the data analysis file with just one set of data that I will attach. I will also attach the raw data csv file which contains 5 readings, and also demonstrates how the readings are inconsistent and occasionally take up 69 rows instead of 68. The readings can be differentiated by a human by the time resetting and the tension dropping.

3 Replies

@wicked1591 Have you tried Power Query? Specifically the Combine Binaries option? You can save your .csv files to a single folder, then use PQ to import and append the data to the reporting workbook. As you add new files, just refresh, and PQ will update only the newest additions. PQ will also append the filename to each dataset, so that will allow you to make your differentiation between them.

 

It will eliminate copy/pasting, and you can create summaries with PivotTables and charts. If you add a slicer for dataset, then you can quickly switch views.

 

HTH

@Smitty Smith  Thanks for the reply. I don't think power query is right for me because all of my data is coming from one csv file, and I don't really have a time efficient way to separate the data sets into different files.

I have done some thinking to simplify the problem though, and this is what I really need:

 

I want to take 68 cells by 3 columns from the csv, copy that, and paste it in a xls file every 4th column.

So, it would take cells A1:C69 and copy them to A1 in another sheet. Then, cells A70:C137 would copy to A4 in the other sheet. The problem I have is that this example only transfers 2 data sets. I know how to copy these specific cells, but what I don't know how to do and what to do is have it function for an indefinite number of cells. So every 68x3 cells, copy into the 4th column over in the same row.

 

Is there a VBA function that would take X cells and cut/copy them to every Yth column?

@wicked1591 You might need to play with the offsets a bit, but this should get you started:

 

Sub foo()
'   Copy from
    '   A1:C69
    '   A70: C137, etc
    
    Dim i As Long
    Dim x As Long
    
    Dim lr As Long
    Dim lc As Long
    
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    x = 69
    
    For i = 1 To lr Step 69
        lc = Cells(1, Columns.Count).End(xlToLeft).Offset(, 2).Column
        Range("A" & i & ":C" & x).Copy Cells(1, lc)
        x = x + 69
    Next i
        
End Sub

 

Related Conversations
Extentions Synchronization
Deleted in Discussions on
3 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
35 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
9 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies