Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 26

Thread: Solved: Need Help : VBA in EXCEL / (if possible in DIAdem software)

  1. #1

    Solved: Need Help : VBA in EXCEL / (if possible in DIAdem software)

    Hello everyone! I am newbie with VBA Script (i have no knowledge at all about VBA before but i have a project which requires me to use VBA script). I need help to solve the problem i've got here..

    So the project i have is like this..
    My university has this photovoltaic modules , which record solar/sun radiation each minute everyday for the whole year. The data is saved per day in a .txt file , which means for one year i will have 365 .txt files. Each file, when opened in Excel , has 23 columns, and the only column that i want to use is column A(minute) and column X(which has the value of sun radiation).. Other columns can be ignored.

    What i have to do now is, i have to calculate how many minutes for the whole year, that the sun gives the radiation with values of 200 W/m2 , 300W/m2, 400W/m2 and so on and transfer the whole thing into a graph with Solar Radiation in X-axes, and Minutes at Y-axes.

    My problem now here, the lecturer requires me to find a way, where i can import all my 365 Files all at one time with just a click and do all the calculation automatically.. because the only solution i have now is, to do the calculation per day and do it 365 times for the whole year.

    And he suggested me to use DIAdem software because this program is more suitable for macro data, and again, the software is totally NEW to me. i've called National Instruments(the company which produces this software) and they gave me a hint on how to solve this :

    Call ASCIIConfigLoad("C:\Test.stp")
    Call ASCIILoad(UseFileList,0)

    and the function above is to used inside a loop, since i need to import the data all at the same time..

    But i have no idea what this is and how to begin / end the script.
    I need help pleaseeeee.. Your help is very very much appreciated!!!! =)

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Could you attach a sample .txt file (or two)? Say one mid-summer and one mid-winter?

    Most of the effort will be importing or looking at the data; 60 x 24 x 365 rows is within the number of rows for Excel 2003 if imported, then producing the graph shouldn't be too difficult with the Histogram part of the Data Analysis Pack.
    We might be able to scan/query the files instead of importing them.
    I don't know anything about DIAdem software either.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,736
    Location
    Also be interested to know 1) that the file name can be used to determine the observation date (e.g. 20090315.txt for 15 Mar 2009), and 2) that all the observation files can be in a single folder to allow easy looping

    3) "200 W/m2 , 300W/m2, 400W/m2 and so on" -- what does the 'and so on' mean? multiples of 100W/M^2?

    Paul




    Paul

  4. #4
    Quote Originally Posted by p45cal
    Could you attach a sample .txt file (or two)? Say one mid-summer and one mid-winter?

    Most of the effort will be importing or looking at the data; 60 x 24 x 365 rows is within the number of rows for Excel 2003 if imported, then producing the graph shouldn't be too difficult with the Histogram part of the Data Analysis Pack.
    We might be able to scan/query the files instead of importing them.
    I don't know anything about DIAdem software either.
    hello,
    ive tried uploading the files but it says the files are invalid because its in " .txt" format.. Can i email you the files instead?

  5. #5
    Quote Originally Posted by Paul_Hossler
    Also be interested to know 1) that the file name can be used to determine the observation date (e.g. 20090315.txt for 15 Mar 2009), and 2) that all the observation files can be in a single folder to allow easy looping

    3) "200 W/m2 , 300W/m2, 400W/m2 and so on" -- what does the 'and so on' mean? multiples of 100W/M^2?

    Paul




    Paul
    Hye,

    1) Yes the files are named according to dates, like this : 20090315.txt.. So i have 365 files with different dates for each day of the year.. from each file, i just need to look at column A (Minute) and Column X (solar radiation)..

    This data records for example : At minute 1 , the sun radiation gives value of 305W/m^2, at minute 2 sunradiation of 410W/m^2, .. until the last minute of the day which is minute 1440..

    2) So how do i do this single loop thing? :-s How can i import only these 2 columns (Column A and Column X) from each file and have them only in one single sheet. So that i can compare , the values of sun radiation for every day at each minute just by looking at one single sheet.

    3) Since the sun gives 1440 values per day( because we have 1440 minutes per day and the module records the value of sun radiation per minute) ,
    i have to calculate from range of 200W/m^2 to 1500W/m^2 ,

    How many times (here equal to how many minute) did the sun give value of 200W/m^2 , how many times did the sun give value of 300W/m^2, how many times did the sun give value of 400W/m^2 ... until how many times did the sun give value of 1500W/m^2..

  6. #6

    Updates . Here are the Files example

    I have 365 files in a folder named PhysikalischeWerte. Each files are named in dates order like above starting 01.01.2008 until 31.12.2008.
    Like ive mentioned, they already contained data in so many columns, but i just want the column A (which minute) and Column X(the sun radiation value of the day).

    I need a loop that will open each file and just copy the data from Column X (other columns can be ignored) and copy all data to a new file / sheet when opened in excel . (So all 365) files will be one file.

    Thanksss so muchhh for your help.. But i've no idea how to begin.. :-s

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    When I open one of those files, column X is blank.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Oh IM SORRY!! its an example file i received from my lecturer. So in this case, i need to use the column A and Column B!

    sorryyy!!!

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Assuming there is something in column X, try this

    [vba]

    Public Sub Test()
    Dim mpFolder As String
    Dim mpFile As String
    Dim mpThis As Worksheet
    Dim mpLastRow As Long
    Dim mpNextRow As Long

    Set mpThis = ActiveSheet
    With Application.FileDialog(msoFileDialogFolderPicker)

    .AllowMultiSelect = False
    If .Show = -1 Then

    mpFolder = .SelectedItems(1)

    mpFile = Dir(mpFolder & Application.PathSeparator & "*.txt")
    If mpFile <> "" Then

    mpNextRow = 1
    Do

    Workbooks.Open mpFolder & Application.PathSeparator & mpFile
    mpLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Cells(8, "X").Resize(mpLastRow - 7).Copy mpThis.Cells(mpNextRow, "A")
    mpNextRow = mpNextRow + mpLastRow - 7
    ActiveWorkbook.Close saveChanges:=False
    mpFile = Dir()

    Loop Until mpFile = ""
    End If
    End If
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Thanks! i will try thisss!

    Since im very very new to this, i have a few (stupid) questions to ask.. Im sorry , ive no ideaaa about VBA at all..
    from the vba script u've given me.. Where / how should i use this Script in Excel? :-s and nowhere in the script stated which folder i would like to use, in this case "folder PhysikalischeWerte". So, is there anything in the script that i should change / add ?

    Thankss so much!!!

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Put that code in a standard code module. When you run it it will allow you to browse to the desiured directory, then it opens all text file in that selected directory.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    oh okayy..and erk where is the standard code module.? :-s
    and what happens when, since i have 365 files, meaning in the end i will have 365 cells(from the Column X) + 1 column A = 366 cells.

    I think Excel can only have up to 250+ cells right? :-s
    what do i do/

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I was appending them in rows, not new columns.

    What do you do now?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    Sorry i cant quite understand. So according to the Script above, each Cell X(ColumnX) will be copied and inserted in the new sheet as one Row is it?

    My Data has a very ver longgggg Columns and Row. Meaning I have about 1440 rows (each row represent the values of each minute) and many Columns which represent The Power, The Voltage , etc..

    But i only want 2 columns , the Minute and the Sun Radiation ( Column A and X)..

    So i cannot use the script above?

  15. #15
    If u were appending them in rows, but i want them to be in columns..
    Maybe i should change in your script the word Rows to Cells , and Cells to Rows? Will this work?

    Thankss!

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, you need to re-align the loop

    [vba]

    Public Sub Test()
    Dim mpFolder As String
    Dim mpFile As String
    Dim mpThis As Worksheet
    Dim mpLastRow As Long
    Dim mpNextCol As Long

    Set mpThis = ActiveSheet
    With Application.FileDialog(msoFileDialogFolderPicker)

    .AllowMultiSelect = False
    If .Show = -1 Then

    mpFolder = .SelectedItems(1)

    mpFile = Dir(mpFolder & Application.PathSeparator & "*.txt")
    If mpFile <> "" Then

    mpNextCol = 1
    Do

    Workbooks.Open mpFolder & Application.PathSeparator & mpFile
    mpLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Cells(8, "A").Resize(mpLastRow - 7,2).Copy mpThis.Cells(1,mpNextCol)
    mpNextCol = mpNextCol + 2
    ActiveWorkbook.Close saveChanges:=False
    mpFile = Dir()
    Loop Until mpFile = ""
    End If
    End If
    End With
    End Sub
    [/vba]

    but you can only handle 122 files in pre-2007 Excel with this approach.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  17. #17
    Thank you so much !! i have tried the Script and it works!!! thanks so much to youu!

    And how now, that when i run this script, they successfully imported the columns i needed in one sheet. In this case, since i have 4 files, at the i will have 8 columns in the new sheet. Column A, B, C, D, E, F, G, H.

    What do i edit, when i say i just want the column A, B, D, F and H? Because the column C, E and G is just the same with column A?

    Thanksss so much i dont know how to thank you enough!!!

  18. #18
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Try the button in the attached, a variant of XLD's code.

    It creates a fresh sheet each time you try it.

    You can see on the sheet with the button on it that the first few results are in the millions. What's the decimal separator on your machine? Would you write three and a half as 3.5 or 3,5 ?

    The macro takes the date from the name of the file - the first 10 characters - and combines it with the times so you should have true excel date/times in column A.
    It only grabs data from lines in the files that begin with data that has 2 colons in the first field. If need to see everything in date order (if the files aren't processed in date order) you just need to sort on column A.

    If this results in proper power values in Column B then it's onto creating the Histogram..

    I'll wait for your response
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  19. #19
    Hello,
    I will use a comma instead of a dot as decimal (3,5) because it is how its written here in Germany.

    I have tried this Script and It works.
    Public Sub Test()
    Dim mpFolder As String
    Dim mpFile As String
    Dim mpThis As Worksheet
    Dim mpLastRow As Long
    Dim mpNextCol As Long

    Set mpThis = ActiveSheet
    With Application.FileDialog(msoFileDialogFolderPicker)

    .AllowMultiSelect = False
    If .Show = -1 Then

    mpFolder = .SelectedItems(1)

    mpFile = Dir(mpFolder & Application.PathSeparator & "*.txt")
    If mpFile <> "" Then

    mpNextCol = 1
    Do

    Workbooks.Open mpFolder & Application.PathSeparator & mpFile
    mpLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Cells(8, "A").Resize(mpLastRow - 7,2).Copy mpThis.Cells(1,mpNextCol)
    mpNextCol = mpNextCol + 2
    ActiveWorkbook.Close saveChanges:=False
    mpFile = Dir()
    Loop Until mpFile = ""
    End If
    End If
    End With
    End Sub


    Only that, I will have 8 Columns in the new sheet which are :

    Column A (Minute) ; Column B (Sun Radiation on that Minute from column A on 20.09.2009)

    Column C (Minute) ; Column D ( Sun Radiation on that Minute from Column C on 21.09.2009)

    and this goes on til 23.09.2009..

    just that the Column A, C , E and G have same values of the minute (data redundant).. how do i avoid this? meaning for day 1 i want to copy 2 columns (Column A and B) and for other days i just want to copy column B..

    How do i Edit that?

  20. #20
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    re:" i just want the column A, B, D, F and H? Because the column C, E and G is just the same with column A? "

    1. The data probably isn't as clean as you think it is. If you look at the resultant file you'll see that lower down the times do not match. On 22.9.09 data jumps from 8:08 to 8:17.

    2. Putting the data in many columns rather than just 2 means
    (a) you won't fit a year's data on one sheet.
    (b) it'll be more difficult to extract the data for a chart.

    Have you tried the file I attached?
    Aaaggh! I made a mistake which screwed up the folder browsing. Try the attached.
    Last edited by p45cal; 11-09-2009 at 08:55 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •