Help Deciphering VB module

men5j2s

Board Regular
Joined
Apr 26, 2016
Messages
59
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have been set a task that involves modifying the way that a macro works for a client.. whilst I know how to do what i want to do, i do not know how to get excel to do it for me, so I will be learning VBA over the coming days :)

My first step is to understand what the author has written in the first place so I know what I'm working with.

I have annotated the code to try to explain what I think its saying.

would someone be willing to give up a little time to check my annotations and expand upon anything that I haven't understood or have missed? the simplicity of my annotations should be a good indication of my level; of experience in VBA (close to zero)?

This is what I have so far, I'll work on the other modules as the days pass..... The whole program takes a .csv file and converts it to a .xlsx file and organises data in a meaningful way and plots a graph from it (the graph has a zoom function too) and certain points of interest are read from the graph and reported in numbers.

There is probably a fair bit that I haven't understood, so help would be great and really appreciated!

my comments follow '-JS-

is it easier to upload my workbook?

Code:
Option Explicit


Sub Open_CSVs()


    Dim FSO As Scripting.FileSystemObject
    Dim FSO2 As Object
    Dim MyFolder As String
    Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
    Dim r As Long
    Dim MyFile As String
    
    Application.ScreenUpdating = False '-JS- suspend screen updating
    
If Range("Source").Value = "" Then '-JS- "source" is the range in which the root directory is specified by the user
    MyFolder = GetFolder("C:\") '-JS- If no root directory was specified, the default will be C:\
Else
    MyFolder = GetFolder(Range("Source").Value) '-JS- If root directory is specified, specification is prioritised
End If


ThisWorkbook.VBProject.VBComponents("Graph_Scaling").Export (MyFolder & "\Graph_Scaling.bas") '-JS- Exports The rescaling module as a .bas file (to the root directory?)
         
   Set FSO = New Scripting.FileSystemObject '-JS- Sets abbreviation for access to file system object functions
    Set SourceFolder = FSO.GetFolder(MyFolder) '-JS- Source folder is set as the root directory (with file system object functions enabled)
    For Each SubFolder In SourceFolder.SubFolders
        MyFile = Dir(SubFolder.Path & "\Exports\*.csv") '-JS- sets "MyFile" as the .csv file within each sub-sub-folder ("Exports") in the source folder.


'-JS- I think this section searches for a folder called exports and converts a .csv file to a .xslsb file,
'     then it processed the file by calling another subroutine ("show bounce" which is within the previously exported module "graph_scaling"),
'     then continues this process for any nyumber of folders in the root directory.


        Do While MyFile <> "" '-JS- If files DO NOT exist, then do the following
            On Error GoTo Done '-JS- If files DO exist, then skip to "Done" (~16 lines below)
            Set FSO2 = CreateObject("scripting.filesystemobject")
            If FSO2.FileExists(SubFolder.Path & "\Exports\" & Replace(MyFile, "csv", "xlsb")) = True Then GoTo Done '-JS- Convert .csv file to .xslb, but is both already exist, then go to "Done"
            DoEvents '-JS- awaits completion of previous task before proceeding
            Workbooks.Open (SubFolder.Path & "\Exports\" & MyFile) '-JS- open the .xlsb workbook
            DoEvents '-JS- wait till workbook is open before proceeding
            
            Do While ActiveWorkbook.Name <> MyFile
                Application.Wait Now + TimeValue("00:00:01") '-JS- Once the workbook is opne, wait this length of time before proceeding
                DoEvents
                Workbooks(MyFile).Activate '-JS- Once the workbook is open, ensure it is the active workbook
                
            Loop
            DoEvents
            ActiveWorkbook.VBProject.VBComponents.Import (MyFolder & "\Graph_Scaling.bas") '-JS- Imports the module into the active workbook, presumably so that the subroutine within it cal ne applied to the active workbook.
            Call show_bounce '-JS- calls and runs the sub "show bounce"
            ActiveWorkbook.SaveAs SubFolder.Path & "\Exports\" & Replace(MyFile, ".csv", ""), FileFormat:=50 '-JS- saves processed file as a macro enabled .xslb file
            DoEvents
            Workbooks(Replace(MyFile, "csv", "xlsb")).Close False
Done: '-JS- If files DO exist, then do the following
            On Error GoTo 0 '-JS- If an error occurs, then ignore it
            MyFile = Dir
        Loop
    Next SubFolder
    
    Kill (MyFolder & "\Graph_Scaling.bas")
    Set SubFolder = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
    
    Application.ScreenUpdating = True '-JS- resore screen updating


End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Code:
If FSO2.FileExists(SubFolder.Path & "\Exports\" & Replace(MyFile, "csv", "xlsb")) = True Then GoTo Done '-JS- Convert .csv file to .xslb, but is both already exist, then go to "Done"
Does not do the conversion. merely checks to see if the xlsb file exists, if so, it skips the conversion steps below.

Code:
Workbooks.Open (SubFolder.Path & "\Exports\" & MyFile) '-JS- open the .xlsb workbook
this opens the csv file, not the xlsb file

Code:
  Do While MyFile <> "" '-JS- If files DO NOT exist, then do the following
The MyFile string will be updated (by the DIR command), so the above line will cause the loop to
continue until all of the csv files in the folder are processed.

Code:
MyFile = Dir
This gets the next csv file name from the Exports folder.
The file name mask is set the first time the DIR command is issued...
From higher up in ths sub
Code:
MyFile = Dir(SubFolder.Path & "\Exports\*.csv")
 
Upvote 0
Cross posted https://www.excelforum.com/excel-programming-vba-macros/1241828-help-deciphering-vb-module.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
My apologies. I will announce on both posts that I have cross posted.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top