diff --git a/original/Worksheet Relationships.xlsm b/original/Worksheet Relationships.xlsm new file mode 100644 index 0000000..302bfbe Binary files /dev/null and b/original/Worksheet Relationships.xlsm differ diff --git a/src/Enumerations.cls b/src/Enumerations.cls new file mode 100644 index 0000000..5b45a47 --- /dev/null +++ b/src/Enumerations.cls @@ -0,0 +1,66 @@ +' Author: Edward Middleton-Smith +' Precision And Research Technology Systems Limited + +' Project: +' Technology: +' Feature: + + + +' NOTES + + + +' METHOD LAYOUT COMMENTS +' FUNCTION +' ARGUMENTS +' PROCESSING ACCELERATION +' CONSTANTS +' VARIABLE DECLARATION +' ARGUMENT VALIDATION +' VARIABLE INSTANTIATION +' METHODS +' RETURNS +' ERROR HANDLING +' PROCESSING DECELARATION + + + +' MODULE INITIALISATION +' Set array start index to 1 to match spreadsheet indices +Option Base 1 +' Forced Variable Declaration +Option Explicit + + +' Worksheet orientation +Enum orientation + ColumnHeaders = 0 + RowHeaders = 1 +End Enum + + +' Array search direction +Enum direction + x = 1 + y = 2 + Z = 3 +End Enum + + +' Different search direction +Enum dir_traverse + FORWARDS = 1 + BACKWARDS = -1 +End Enum + + +' Customer account status +Enum OverdueStatus + UNDUE = 0 + OVERDUE = 1 + SUPEROVERDUE = 2 +End Enum + + + diff --git a/src/Get_Local_Onedrive_Path.bas b/src/Get_Local_Onedrive_Path.bas new file mode 100644 index 0000000..6229b2b --- /dev/null +++ b/src/Get_Local_Onedrive_Path.bas @@ -0,0 +1,1040 @@ + +'Attribute VB_Name = "GetLocalOneDrivePath" +' Cross-platform VBA Function to get the local path of OneDrive/SharePoint +' synchronized Microsoft Office files (Works on Windows and on macOS) +' +' Author: Guido Witt-Dörring +' Created: 2022/07/01 +' Updated: 2023/06/29 +' License: MIT +' +' ———————————————————————————————————————————————————————————————— +' https://gist.github.com/guwidoe/038398b6be1b16c458365716a921814d +' https://stackoverflow.com/a/73577057/12287457 +' ———————————————————————————————————————————————————————————————— +' +' Copyright (c) 2023 Guido Witt-Dörring +' +' MIT License: +' Permission is hereby granted, free of charge, to any person obtaining a copy +' of this software and associated documentation files (the "Software"), to +' deal in the Software without restriction, including without limitation the +' rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +' sell copies of the Software, and to permit persons to whom the Software is +' furnished to do so, subject to the following conditions: +' +' The above copyright notice and this permission notice shall be included in +' all copies or substantial portions of the Software. +' +' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +' IN THE SOFTWARE. +' +'——————————————————————————————————————————————————————————————————————————————— +' COMMENTS REGARDING THE IMPLEMENTATION: +' 1) Background and Alternative +' This function was intended to be written as a single procedure without any +' dependencies, for maximum portability between projects, as it implements a +' functionality that is very commonly needed for many VBA applications +' working inside OneDrive/SharePoint synchronized directories. I followed +' this paradigm because it was not clear to me how complicated this simple +' sounding endeavour would turn out to be. +' Unfortunately, more and more complications arose, and little by little, +' the procedure turned incredibly complicated. I do not condone the coding +' style applied here, and that this is not how I usually write code, +' nevertheless, I'm not open to rewriting this code in a different style, +' because a clean implementation of this algorithm already exists, as pointed +' out in the following. +' +' If you would like to understand the underlying algorithm of how the local +' path can be found with only the Url-path as input, I recommend following +' the much cleaner implementation by Cristian Buse: +' https://github.com/cristianbuse/VBA-FileTools +' We developed the algorithm together and wrote separate implementations +' concurrently. His solution is contained inside a module-level library, +' split into many procedures and using features like private types and API- +' functions, that are not available when trying to create a single procedure +' without dependencies like below. This makes his code more readable. +' +' Both of our solutions are well tested and actively supported with bugfixes +' and improvements, so both should be equally valid choices for use in your +' project. The differences in performance/features are marginal and they can +' often be used interchangeably. If you need more file-system interaction +' functionality, use Cristians library, and if you only need GetLocalPath, +' just copy this function to any module in your project and it will work. +' +' 2) How does this function NOT work? +' Most other solutions for this problem circulating online (A list of most +' can be found here: https://stackoverflow.com/a/73577057/12287457) are using +' one of two approaches, : +' 1. they use the environment variables set by OneDrive: +' - Environ(OneDrive) +' - Environ(OneDriveCommercial) +' - Environ(OneDriveConsumer) +' and replace part of the URL with it. There are many problems with this +' approach: +' 1. They are not being set by OneDrive on MacOS. +' 2. It is unclear exactly which part of the URL needs to be replaced. +' 3. Environment variables can be changed by the user. +' 4. Only there three exist. If more onedrive accounts are logged in, +' they just overwrite the previous ones. +' or, +' 2. they use the mount points OneDrive writes to the registry here: +' - \HKEY_CURRENT_USER\Software\SyncEngines\Providers\OneDrive\ +' this also has several drawbacks: +' 1. The registry is not available on MacOS. +' 2. It's still unclear exactly what part of the URL should be replaced. +' 3. These registry keys can contain mistakes, like for example, when: +' - Synchronizing a folder called "Personal" from someone else's +' personal OneDrive +' - Synchronizing a folder called "Business1" from someone else's +' personal OneDrive and then relogging your own first Business +' OneDrive account +' - Relogging you personal OneDrive can change the "CID" property +' from a folderID formatted cid (e.g. 3DEA8A9886F05935!125) to a +' regular private cid (e.g. 3dea8a9886f05935) for synced folders +' from other people's OneDrives +' +' For these reasons, this solution uses a completely different approach to +' solve this problem. +' +' 3) How does this function work? +' This function builds the Web to Local translation dictionary by extracting +' the mount points from the OneDrive settings files. +' It reads files from... +' On Windows: +' - the "...\AppData\Local\Microsoft" directory +' On Mac: +' - the "~/Library/Containers/com.microsoft.OneDrive-mac/Data/" & _ +' "Library/Application Support" directory +' - and/or the "~/Library/Application Support" +' It reads the following files: +' - \OneDrive\settings\Personal\ClientPolicy.ini +' - \OneDrive\settings\Personal\????????????????.dat +' - \OneDrive\settings\Personal\????????????????.ini +' - \OneDrive\settings\Personal\global.ini +' - \OneDrive\settings\Personal\GroupFolders.ini +' - \OneDrive\settings\Business#\????????-????-????-????-????????????.dat +' - \OneDrive\settings\Business#\????????-????-????-????-????????????.ini +' - \OneDrive\settings\Business#\ClientPolicy*.ini +' - \OneDrive\settings\Business#\global.ini +' - \Office\CLP\* (just the filename) +' +' Where: +' - "*" ... 0 or more characters +' - "?" ... one character [0-9, a-f] +' - "#" ... one digit +' - "\" ... path separator, (= "/" on MacOS) +' - The "???..." filenames represent CIDs) +' +' On MacOS, the \Office\CLP\* exists for each Microsoft Office application +' separately. Depending on whether the application was already used in +' active syncing with OneDrive it may contain different/incomplete files. +' In the code, the path of this directory is stored inside the variable +' "clpPath". On MacOS, the defined clpPath might not exist or not contain +' all necessary files for some host applications, because Environ("HOME") +' depends on the host app. +' This is not a big problem as the function will still work, however in +' this case, specifying a preferredMountPointOwner will do nothing. +' To make sure this directory and the necessary files exist, a file must +' have been actively synchronized with OneDrive by the application whose +' "HOME" folder is returned by Environ("HOME") while being logged in +' to that application with the account whose email is given as +' preferredMountPointOwner, at some point in the past! +' +' If you are usually working with Excel but are using this function in a +' different app, you can instead use an alternative (Excels CLP folder) as +' the clpPath as it will most likely contain all the necessary information +' The alternative clpPath is commented out in the code, if you prefer to +' use Excels CLP folder per default, just un-comment the respective line +' in the code. +'——————————————————————————————————————————————————————————————————————————————— + +'——————————————————————————————————————————————————————————————————————————————— +' COMMENTS REGARDING THE USAGE: +' This function can be used as a User Defined Function (UDF) from the worksheet. +' (More on that, see "USAGE EXAMPLES") +' +' This function offers three optional parameters to the user, however using +' these should only be necessary in extremely rare situations. +' The best rule regarding their usage: Don't use them. +' +' In the following these parameters will still be explained. +' +'1) returnAll +' In some exceptional cases it is possible to map one OneDrive WebPath to +' multiple different localPaths. This can happen when multiple Business +' OneDrive accounts are logged in on one device, and multiple of these have +' access to the same OneDrive folder and they both decide to synchronize it or +' add it as link to their MySite library. +' Calling the function with returnAll:=True will return all valid localPaths +' for the given WebPath, separated by two forward slashes (//). This should be +' used with caution, as the return value of the function alone is, should +' multiple local paths exist for the input webPath, not a valid local path +' anymore. +' An example of how to obtain all of the local paths could look like this: +' Dim localPath as String, localPaths() as String +' localPath = GetLocalPath(webPath, True) +' If Not localPath Like "http*" Then +' localPaths = Split(localPath, "//") +' End If +' +'2) preferredMountPointOwner +' This parameter deals with the same problem as 'returnAll' +' If the function gets called with returnAll:=False (default), and multiple +' localPaths exist for the given WebPath, the function will just return any +' one of them, as usually, it shouldn't make a difference, because the result +' directories at both of these localPaths are mirrored versions of the same +' webPath. Nevertheless, this option lets the user choose, which mountPoint +' should be chosen if multiple localPaths are available. Each localPath is +' 'owned' by an OneDrive Account. If a WebPath is synchronized twice, this can +' only happen by synchronizing it with two different accounts, because +' OneDrive prevents you from synchronizing the same folder twice on a single +' account. Therefore, each of the different localPaths for a given WebPath +' has a unique 'owner'. preferredMountPointOwner lets the user select the +' localPath by specifying the account the localPath should be owned by. +' This is done by passing the Email address of the desired account as +' preferredMountPointOwner. +' For example, you have two different Business OneDrive accounts logged in, +' foo.bar@business1.com and foo.bar@business2.com +' Both synchronize the WebPath: +' webPath = "https://business1.sharepoint.com/sites/TestLib/Documents/" & _ + "Test/Test/Test/test.xlsm" +' +' The first one has added it as a link to his personal OneDrive, the local +' path looks like this: +' C:\Users\username\OneDrive - Business1\TestLinkParent\Test - TestLinkLib\... +' ...Test\test.xlsm +' +' The second one just synchronized it normally, the localPath looks like this: +' C:\Users\username\Business1\TestLinkLib - Test\Test\test.xlsm +' +' Calling GetLocalPath like this: +' GetLocalPath(webPath,,, "foo.bar@business1.com") will return: +' C:\Users\username\OneDrive - Business1\TestLinkParent\Test - TestLinkLib\... +' ...Test\test.xlsm +' +' Calling it like this: +' GetLocalPath(webPath,,, "foo.bar@business2.com") will return: +' C:\Users\username\Business1\TestLinkLib - Test\Test\test.xlsm +' +' And calling it like this: +' GetLocalPath(webPath,, True) will return: +' C:\Users\username\OneDrive - Business1\TestLinkParent\Test - TestLinkLib\... +' ...Test\test.xlsm//C:\Users\username\Business1\TestLinkLib - Test\Test\... +' ...test.xlsm +' +' Calling it normally like this: +' GetLocalPath(webPath) will return any one of the two localPaths, so: +' C:\Users\username\OneDrive - Business1\TestLinkParent\Test - TestLinkLib\... +' ...Test\test.xlsm +' OR +' C:\Users\username\Business1\TestLinkLib - Test\Test\test.xlsm +' +'3) rebuildCache +' The function creates a "translation" dictionary from the OneDrive settings +' files and then uses this dictionary to "translate" WebPaths to LocalPaths. +' This dictionary is implemented as a static variable to the function doesn't +' have to recreate it every time it is called. It is written on the first +' function call and reused on all the subsequent calls, making them faster. +' If the function is called with rebuildCache:=True, this dictionary will be +' rewritten, even if it was already initialized. +' Note that it is not necessary to use this parameter manually, even if a new +' MountPoint was added to the OneDrive, or a new OneDrive account was logged +' in since the last function call because the function will automatically +' determine if any of those cases occurred, without sacrificing performance. +'——————————————————————————————————————————————————————————————————————————————— +Option Explicit + +''—————————————————————————————————————————————————————————————————————————————— +'' USAGE EXAMPLES: +'' Excel: +'Private Sub TestGetLocalPathExcel() +' Debug.Print GetLocalPath(ThisWorkbook.FullName) +' Debug.Print GetLocalPath(ThisWorkbook.path) +'End Sub +' +' Usage as User Defined Function (UDF): +' You might have to replace ; with , in the formulas depending on your settings. +' Add this formula to any cell, to get the local path of the workbook: +' =GetLocalPath(LEFT(CELL("filename";A1);FIND("[";CELL("filename";A1))-1)) +' +' To get the local path including the filename (the FullName), use this formula: +' =GetLocalPath(LEFT(CELL("filename";A1);FIND("[";CELL("filename";A1))-1) & +' TEXTAFTER(TEXTBEFORE(CELL("filename";A1);"]");"[")) +' +''Word: +'Private Sub TestGetLocalPathWord() +' Debug.Print GetLocalPath(ThisDocument.FullName) +' Debug.Print GetLocalPath(ThisDocument.path) +'End Sub +' +''PowerPoint: +'Private Sub TestGetLocalPathPowerPoint() +' Debug.Print GetLocalPath(ActivePresentation.FullName) +' Debug.Print GetLocalPath(ActivePresentation.path) +'End Sub +''—————————————————————————————————————————————————————————————————————————————— + + +'This Function will convert a OneDrive/SharePoint Url path, e.g. Url containing +'https://d.docs.live.net/; .sharepoint.com/sites; my.sharepoint.com/personal/... +'to the locally synchronized path on your current pc or mac, e.g. a path like +'C:\users\username\OneDrive\ on Windows; or /Users/username/OneDrive/ on MacOS, +'if you have the remote directory locally synchronized with the OneDrive app. +'If no local path can be found, the input value will be returned unmodified. +'Author: Guido Witt-Dörring +'Source: https://gist.github.com/guwidoe/038398b6be1b16c458365716a921814d +' https://stackoverflow.com/a/73577057/12287457 +Public Function GetLocalPath(ByVal path As String, _ + Optional ByVal returnAll As Boolean = False, _ + Optional ByVal preferredMountPointOwner As String = "", _ + Optional ByVal rebuildCache As Boolean = False) _ + As String + #If Mac Then + Const vbErrPermissionDenied As Long = 70 + Const vbErrInvalidFormatInResourceFile As Long = 325 + Const syncIDFileName As String = ".849C9593-D756-4E56-8D6E-42412F2A707B" + Const isMac As Boolean = True + Const ps As String = "/" 'Application.PathSeparator doesn't work + #Else 'Windows 'in all host applications (e.g. Outlook), hence + Const ps As String = "\" 'conditional compilation is preferred here. + Const isMac As Boolean = False + #End If + Const methodName As String = "GetLocalPath" + Const vbErrFileNotFound As Long = 53 + Const vbErrOutOfMemory As Long = 7 + Const vbErrKeyAlreadyExists As Long = 457 + Static locToWebColl As Collection, lastCacheUpdate As Date + + If Not Left(path, 8) = "https://" Then GetLocalPath = path: Exit Function + + Dim webRoot As String, locRoot As String, s As String, vItem As Variant + Dim pmpo As String: pmpo = LCase$(preferredMountPointOwner) + If Not locToWebColl Is Nothing And Not rebuildCache Then + Dim resColl As Collection: Set resColl = New Collection + 'If the locToWebColl is initialized, this logic will find the local path + For Each vItem In locToWebColl + locRoot = vItem(0): webRoot = vItem(1) + If InStr(1, path, webRoot, vbTextCompare) = 1 Then _ + resColl.Add key:=vItem(2), _ + Item:=Replace(Replace(path, webRoot, locRoot, , 1), "/", ps) + Next vItem + If resColl.count > 0 Then + If returnAll Then + For Each vItem In resColl: s = s & "//" & vItem: Next vItem + GetLocalPath = Mid$(s, 3): Exit Function + End If + On Error Resume Next: GetLocalPath = resColl(pmpo): On Error GoTo 0 + If GetLocalPath <> "" Then Exit Function + GetLocalPath = resColl(1): Exit Function + End If + 'Local path was not found with cached mountpoints + GetLocalPath = path 'No Exit Function here! Check if cache needs rebuild + End If + + Dim settPaths As Collection: Set settPaths = New Collection + Dim settPath As Variant, clpPath As String + #If Mac Then 'The settings directories can be in different locations + Dim cloudStoragePath As String, cloudStoragePathExists As Boolean + s = Environ("HOME") + clpPath = s & "/Library/Application Support/Microsoft/Office/CLP/" + s = Left$(s, InStrRev(s, "/Library/Containers/", , vbBinaryCompare)) + settPaths.Add s & _ + "Library/Containers/com.microsoft.OneDrive-mac/Data/" & _ + "Library/Application Support/OneDrive/settings/" + settPaths.Add s & "Library/Application Support/OneDrive/settings/" + cloudStoragePath = s & "Library/CloudStorage/" + + 'Excels CLP folder: + 'clpPath = Left$(s, InStrRev(s, "/Library/Containers", , 0)) & _ + "Library/Containers/com.microsoft.Excel/Data/" & _ + "Library/Application Support/Microsoft/Office/CLP/" + #Else 'On Windows, the settings directories are always in this location: + settPaths.Add Environ("LOCALAPPDATA") & "\Microsoft\OneDrive\settings\" + clpPath = Environ("LOCALAPPDATA") & "\Microsoft\Office\CLP\" + #End If + + Dim i As Long + #If Mac Then 'Request access to all possible directories at once + Dim arrDirs() As Variant: ReDim arrDirs(1 To settPaths.count * 11 + 1) + For Each settPath In settPaths + For i = i + 1 To i + 9 + arrDirs(i) = settPath & "Business" & i Mod 11 + Next i + arrDirs(i) = settPath: i = i + 1 + arrDirs(i) = settPath & "Personal" + Next settPath + arrDirs(i + 1) = cloudStoragePath + Dim accessRequestInfoMsgShown As Boolean + accessRequestInfoMsgShown = GetSetting("GetLocalPath", _ + "AccessRequestInfoMsg", "Displayed", "False") = "True" + If Not accessRequestInfoMsgShown Then MsgBox "The current " _ + & "VBA Project requires access to the OneDrive settings files to " _ + & "translate a OneDrive URL to the local path of the locally " & _ + "synchronized file/folder on your Mac. Because these files are " & _ + "located outside of Excels sandbox, file-access must be granted " _ + & "explicitly. Please approve the access requests following this " _ + & "message.", vbInformation + If Not GrantAccessToMultipleFiles(arrDirs) Then _ + Err.Raise vbErrPermissionDenied, methodName + #End If + + 'Find all subdirectories in OneDrive settings folder: + Dim oneDriveSettDirs As Collection: Set oneDriveSettDirs = New Collection + For Each settPath In settPaths + Dim dirName As String: dirName = Dir(settPath, vbDirectory) + Do Until dirName = vbNullString + If dirName = "Personal" Or dirName Like "Business#" Then _ + oneDriveSettDirs.Add Item:=settPath & dirName & ps + dirName = Dir(, vbDirectory) + Loop + Next settPath + + If Not locToWebColl Is Nothing Or isMac Then + Dim requiredFiles As Collection: Set requiredFiles = New Collection + 'Get collection of all required files + Dim vDir As Variant + For Each vDir In oneDriveSettDirs + Dim cid As String: cid = IIf(vDir Like "*" & ps & "Personal" & ps, _ + "????????????*", _ + "????????-????-????-????-????????????") + Dim FileName As String: FileName = Dir(vDir, vbNormal) + Do Until FileName = vbNullString + If FileName Like cid & ".ini" _ + Or FileName Like cid & ".dat" _ + Or FileName Like "ClientPolicy*.ini" _ + Or StrComp(FileName, "GroupFolders.ini", vbTextCompare) = 0 _ + Or StrComp(FileName, "global.ini", vbTextCompare) = 0 Then _ + requiredFiles.Add Item:=vDir & FileName + FileName = Dir + Loop + Next vDir + End If + + 'This part should ensure perfect accuracy despite the mount point cache + 'while sacrificing almost no performance at all by querying FileDateTimes. + If Not locToWebColl Is Nothing And Not rebuildCache Then + 'Check if a settings file was modified since the last cache rebuild + Dim vFile As Variant + For Each vFile In requiredFiles + If FileDateTime(vFile) > lastCacheUpdate Then _ + rebuildCache = True: Exit For 'full cache refresh is required! + Next vFile + If Not rebuildCache Then Exit Function + End If + + 'If execution reaches this point, the cache will be fully rebuilt... + Dim fileNum As Long, syncID As String, b() As Byte + #If Mac Then 'Variables for manual decoding of UTF-8, UTF-32 and ANSI + Dim j As Long, k As Long, m As Long, ansi() As Byte, sAnsi As String + Dim utf16() As Byte, sUtf16 As String, utf32() As Byte + Dim utf8() As Byte, sUtf8 As String, numBytesOfCodePoint As Long + Dim codepoint As Long, lowSurrogate As Long, highSurrogate As Long + #End If + + lastCacheUpdate = Now() + #If Mac Then 'Prepare building syncIDtoSyncDir dictionary. This involves + 'reading the ".849C9593-D756-4E56-8D6E-42412F2A707B" files inside the + 'subdirs of "~/Library/CloudStorage/", list of files and access required + Dim coll As Collection: Set coll = New Collection + dirName = Dir(cloudStoragePath, vbDirectory) + Do Until dirName = vbNullString + If dirName Like "OneDrive*" Then + cloudStoragePathExists = True + vDir = cloudStoragePath & dirName & ps + vFile = cloudStoragePath & dirName & ps & syncIDFileName + coll.Add Item:=vDir + requiredFiles.Add Item:=vDir 'For pooling file access requests + requiredFiles.Add Item:=vFile + End If + dirName = Dir(, vbDirectory) + Loop + + 'Pool access request for these files and the OneDrive/settings files + If locToWebColl Is Nothing Then + Dim vFiles As Variant + If requiredFiles.count > 0 Then + ReDim vFiles(1 To requiredFiles.count) + For i = 1 To UBound(vFiles): vFiles(i) = requiredFiles(i): Next i + If Not GrantAccessToMultipleFiles(vFiles) Then _ + Err.Raise vbErrPermissionDenied, methodName + End If + End If + + 'More access might be required if some folders inside cloudStoragePath + 'don't contain the hidden file ".849C9593-D756-4E56-8D6E-42412F2A707B". + 'In that case, access to their first level subfolders is also required. + If cloudStoragePathExists Then + For i = coll.count To 1 Step -1 + Dim fAttr As Long: fAttr = 0 + On Error Resume Next + fAttr = GetAttr(coll(i) & syncIDFileName) + Dim IsFile As Boolean: IsFile = False + If Err.Number = 0 Then IsFile = Not CBool(fAttr And vbDirectory) + On Error GoTo 0 + If Not IsFile Then 'hidden file does not exist + 'Dir(path, vbHidden) is unreliable and doesn't work on some Macs + 'If Dir(coll(i) & syncIDFileName, vbHidden) = vbNullString Then + dirName = Dir(coll(i), vbDirectory) + Do Until dirName = vbNullString + If Not dirName Like ".Trash*" And dirName <> "Icon" Then + coll.Add coll(i) & dirName & ps + coll.Add coll(i) & dirName & ps & syncIDFileName, _ + coll(i) & dirName & ps '<- key for removal + End If + dirName = Dir(, vbDirectory) + Loop 'Remove the + coll.Remove i 'folder if it doesn't contain the hidden file. + End If + Next i + If coll.count > 0 Then + ReDim arrDirs(1 To coll.count) + For i = 1 To coll.count: arrDirs(i) = coll(i): Next i + If Not GrantAccessToMultipleFiles(arrDirs) Then _ + Err.Raise vbErrPermissionDenied, methodName + End If + 'Remove all files from coll (not the folders!): Reminder: + On Error Resume Next 'coll(coll(i)) = coll(i) & syncIDFileName + For i = coll.count To 1 Step -1 + coll.Remove coll(i) + Next i + On Error GoTo 0 + + 'Write syncIDtoSyncDir collection + Dim syncIDtoSyncDir As Collection + Set syncIDtoSyncDir = New Collection + For Each vDir In coll + fAttr = 0 + On Error Resume Next + fAttr = GetAttr(vDir & syncIDFileName) + IsFile = False + If Err.Number = 0 Then IsFile = Not CBool(fAttr And vbDirectory) + On Error GoTo 0 + If IsFile Then 'hidden file exists + 'Dir(path, vbHidden) is unreliable and doesn't work on some Macs + 'If Dir(vDir & syncIDFileName, vbHidden) <> vbNullString Then + fileNum = FreeFile(): s = "": vFile = vDir & syncIDFileName + 'Somehow reading these files with "Open" doesn't always work + Dim readSucceeded As Boolean: readSucceeded = False + On Error GoTo ReadFailed + Open vFile For Binary Access Read As #fileNum + ReDim b(0 To LOF(fileNum)): Get fileNum, , b: s = b + readSucceeded = True +ReadFailed: On Error GoTo -1 + Close #fileNum: fileNum = 0 + On Error GoTo 0 + If readSucceeded Then + 'Debug.Print "Used open statement to read file: " & _ + vDir & syncIDFileName + ansi = s 'If Open was used: Decode ANSI string manually: + If LenB(s) > 0 Then + ReDim utf16(0 To LenB(s) * 2 - 1): k = 0 + For j = LBound(ansi) To UBound(ansi) + utf16(k) = ansi(j): k = k + 2 + Next j + s = utf16 + Else: s = vbNullString + End If + Else 'Reading the file with "Open" failed with an error. Try + 'using AppleScript. Also avoids the manual transcoding. + 'Somehow ApplScript fails too, sometimes. Seems whenever + '"Open" works, AppleScript fails and vice versa (?!?!) + vFile = MacScript("return path to startup disk as " & _ + "string") & Replace(Mid$(vFile, 2), ps, ":") + s = MacScript("return read file """ & _ + vFile & """ as string") + 'Debug.Print "Used Apple Script to read file: " & vFile + End If + If InStr(1, s, """guid"" : """, vbBinaryCompare) Then + s = Split(s, """guid"" : """)(1) + syncID = Left$(s, InStr(1, s, """", 0) - 1) + syncIDtoSyncDir.Add key:=syncID, _ + Item:=VBA.Array(syncID, Left$(vDir, Len(vDir) - 1)) + Else + Debug.Print "Warning, empty syncIDFile encountered!" + End If + End If + Next vDir + End If + 'Now all access requests have succeeded + If Not accessRequestInfoMsgShown Then SaveSetting _ + "GetLocalPath", "AccessRequestInfoMsg", "Displayed", "True" + #End If + + 'Declare all variables that will be used in the loop over OneDrive settings + Dim line As Variant, parts() As String, N As Long, libNr As String + Dim tag As String, mainMount As String, relPath As String, email As String + Dim parentID As String, folderID As String, folderName As String + Dim folderIdPattern As String, folderType As String, keyExists As Boolean + Dim siteID As String, libID As String, webID As String, lnkID As String + Dim mainSyncID As String, syncFind As String, mainSyncFind As String + 'The following are "constants" and needed for reading the .dat files: + Dim sig1 As String: sig1 = ChrB$(2) + Dim sig2 As String * 4: MidB$(sig2, 1) = ChrB$(1) + Dim vbNullByte As String: vbNullByte = ChrB$(0) + #If Mac Then + Const sig3 As String = vbNullChar & vbNullChar + #Else 'Windows + Const sig3 As String = vbNullChar + #End If + + 'Writing locToWebColl using .ini and .dat files in the OneDrive settings: + 'Here, a Scripting.Dictionary would be nice but it is not available on Mac! + Dim lastAccountUpdates As Collection, lastAccountUpdate As Date + Set lastAccountUpdates = New Collection + Set locToWebColl = New Collection + For Each vDir In oneDriveSettDirs 'One folder per logged in OD account + dirName = Mid$(vDir, InStrRev(vDir, ps, Len(vDir) - 1, 0) + 1) + dirName = Left$(dirName, Len(dirName) - 1) + + 'Read global.ini to get cid + If Dir(vDir & "global.ini", vbNormal) = "" Then GoTo NextFolder + fileNum = FreeFile() + Open vDir & "global.ini" For Binary Access Read As #fileNum + ReDim b(0 To LOF(fileNum)): Get fileNum, , b + Close #fileNum: fileNum = 0 + #If Mac Then 'On Mac, the OneDrive settings files use UTF-8 encoding + sUtf8 = b: GoSub DecodeUTF8 + b = sUtf16 'b = StrConv(b, vbUnicode) <- UNRELIABLE + #End If + For Each line In Split(b, vbNewLine) + If line Like "cid = *" Then cid = Mid$(line, 7): Exit For + Next line + + If cid = vbNullString Then GoTo NextFolder + If (Dir(vDir & cid & ".ini") = vbNullString Or _ + Dir(vDir & cid & ".dat") = vbNullString) Then GoTo NextFolder + If dirName Like "Business#" Then + folderIdPattern = Replace(Space$(32), " ", "[a-f0-9]") + ElseIf dirName = "Personal" Then + folderIdPattern = Replace(Space$(12), " ", "[A-F0-9]") & "*!###*" + End If + + 'Get email for business accounts + '(only necessary to let user choose preferredMountPointOwner) + FileName = Dir(clpPath, vbNormal) + Do Until FileName = vbNullString + i = InStrRev(FileName, cid, , vbTextCompare) + If i > 1 And cid <> vbNullString Then _ + email = LCase$(Left$(FileName, i - 2)): Exit Do + FileName = Dir + Loop + + #If Mac Then + On Error Resume Next + lastAccountUpdate = lastAccountUpdates(dirName) + keyExists = (Err.Number = 0) + On Error GoTo 0 + If keyExists Then + If FileDateTime(vDir & cid & ".ini") < lastAccountUpdate Then + GoTo NextFolder + Else + For i = locToWebColl.count To 1 Step -1 + If locToWebColl(i)(5) = dirName Then + locToWebColl.Remove i + End If + Next i + lastAccountUpdates.Remove dirName + lastAccountUpdates.Add key:=dirName, _ + Item:=FileDateTime(vDir & cid & ".ini") + End If + Else + lastAccountUpdates.Add key:=dirName, _ + Item:=FileDateTime(vDir & cid & ".ini") + End If + #End If + + 'Read all the ClientPloicy*.ini files: + Dim cliPolColl As Collection: Set cliPolColl = New Collection + FileName = Dir(vDir, vbNormal) + Do Until FileName = vbNullString + If FileName Like "ClientPolicy*.ini" Then + fileNum = FreeFile() + Open vDir & FileName For Binary Access Read As #fileNum + ReDim b(0 To LOF(fileNum)): Get fileNum, , b + Close #fileNum: fileNum = 0 + #If Mac Then 'On Mac, OneDrive settings files use UTF-8 encoding + sUtf8 = b: GoSub DecodeUTF8 + b = sUtf16 'StrConv(b, vbUnicode)UNRELIABLE + #End If + cliPolColl.Add key:=FileName, Item:=New Collection + For Each line In Split(b, vbNewLine) + If InStr(1, line, " = ", vbBinaryCompare) Then + tag = Left$(line, InStr(1, line, " = ", 0) - 1) + s = Mid$(line, InStr(1, line, " = ", 0) + 3) + Select Case tag + Case "DavUrlNamespace" + cliPolColl(FileName).Add key:=tag, Item:=s + Case "SiteID", "IrmLibraryId", "WebID" 'Only used for + s = Replace(LCase$(s), "-", "") 'backup method later + If Len(s) > 3 Then s = Mid$(s, 2, Len(s) - 2) + cliPolColl(FileName).Add key:=tag, Item:=s + End Select + End If + Next line + End If + FileName = Dir + Loop + + 'Read cid.dat file + Const chunkOverlap As Long = 1000 + Const maxDirName As Long = 255 + Dim buffSize As Long: buffSize = -1 'Buffer uninitialized +Try: On Error GoTo Catch + Dim odFolders As Collection: Set odFolders = New Collection + Dim lastChunkEndPos As Long: lastChunkEndPos = 1 + Dim lastFileUpdate As Date: lastFileUpdate = FileDateTime(vDir & _ + cid & ".dat") + i = 0 'i = current reading pos. + Do + 'Ensure file is not changed while reading it + If FileDateTime(vDir & cid & ".dat") > lastFileUpdate Then GoTo Try + fileNum = FreeFile + Open vDir & cid & ".dat" For Binary Access Read As #fileNum + Dim lenDatFile As Long: lenDatFile = LOF(fileNum) + If buffSize = -1 Then buffSize = lenDatFile 'Initialize buffer + 'Overallocate a bit so read chunks overlap to recognize all dirs + ReDim b(0 To buffSize + chunkOverlap) + Get fileNum, lastChunkEndPos, b: s = b + Dim SIZE As Long: SIZE = LenB(s) + Close #fileNum: fileNum = 0 + lastChunkEndPos = lastChunkEndPos + buffSize + + For vItem = 16 To 8 Step -8 + i = InStrB(vItem + 1, s, sig2, 0) 'Sarch pattern in cid.dat + Do While i > vItem And i < SIZE - 168 'and confirm with another + If StrComp(MidB$(s, i - vItem, 1), sig1, 0) = 0 Then 'one + i = i + 8: N = InStrB(i, s, vbNullByte, 0) - i + If N < 0 Then N = 0 'i:Start pos, n: Length + If N > 39 Then N = 39 + #If Mac Then 'StrConv doesn't work reliably on Mac -> + ansi = MidB$(s, i, N) 'Decode ANSI string manually: + j = UBound(ansi) - LBound(ansi) + 1 + If j > 0 Then + ReDim utf16(0 To j * 2 - 1): k = 0 + For j = LBound(ansi) To UBound(ansi) + utf16(k) = ansi(j): k = k + 2 + Next j + folderID = utf16 + Else: folderID = vbNullString + End If + #Else 'Windows + folderID = StrConv(MidB$(s, i, N), vbUnicode) + #End If + i = i + 39: N = InStrB(i, s, vbNullByte, 0) - i + If N < 0 Then N = 0 + If N > 39 Then N = 39 + #If Mac Then 'StrConv doesn't work reliably on Mac -> + ansi = MidB$(s, i, N) 'Decode ANSI string manually: + j = UBound(ansi) - LBound(ansi) + 1 + If j > 0 Then + ReDim utf16(0 To j * 2 - 1): k = 0 + For j = LBound(ansi) To UBound(ansi) + utf16(k) = ansi(j): k = k + 2 + Next j + parentID = utf16 + Else: parentID = vbNullString + End If + #Else 'Windows + parentID = StrConv(MidB$(s, i, N), vbUnicode) + #End If + i = i + 121 + N = InStr(-Int(-(i - 1) / 2) + 1, s, sig3) * 2 - i - 1 + If N > maxDirName * 2 Then N = maxDirName * 2 + If N < 0 Then N = 0 + If folderID Like folderIdPattern _ + And parentID Like folderIdPattern Then + #If Mac Then 'Encoding of folder names is UTF-32-LE + Do While N Mod 4 > 0 + If N > maxDirName * 4 Then Exit Do + N = InStr(-Int(-(i + N) / 2) + 1, s, sig3) _ + * 2 - i - 1 + Loop + If N > maxDirName * 4 Then N = maxDirName * 4 + utf32 = MidB$(s, i, N) + 'UTF-32 can only be converted manually to UTF-16 + ReDim utf16(LBound(utf32) To UBound(utf32)) + j = LBound(utf32): k = LBound(utf32) + Do While j < UBound(utf32) + If utf32(j + 2) + utf32(j + 3) = 0 Then + utf16(k) = utf32(j) + utf16(k + 1) = utf32(j + 1) + k = k + 2 + Else + If utf32(j + 3) <> 0 Then Err.Raise _ + vbErrInvalidFormatInResourceFile, _ + methodName + codepoint = utf32(j + 2) * &H10000 + _ + utf32(j + 1) * &H100& + _ + utf32(j) + m = codepoint - &H10000 + highSurrogate = &HD800& Or (m \ &H400&) + lowSurrogate = &HDC00& Or (m And &H3FF) + utf16(k) = highSurrogate And &HFF& + utf16(k + 1) = highSurrogate \ &H100& + utf16(k + 2) = lowSurrogate And &HFF& + utf16(k + 3) = lowSurrogate \ &H100& + k = k + 4 + End If + j = j + 4 + Loop + If k > LBound(utf16) Then + ReDim Preserve utf16(LBound(utf16) To k - 1) + folderName = utf16 + Else: folderName = vbNullString + End If + #Else 'On Windows encoding is UTF-16-LE + folderName = MidB$(s, i, N) + #End If + 'VBA.Array() instead of just Array() is used in this + 'function because it ignores Option Base 1 + odFolders.Add VBA.Array(parentID, folderName), _ + folderID + End If + End If + i = InStrB(i + 1, s, sig2, 0) 'Find next sig2 in cid.dat + Loop + If odFolders.count > 0 Then Exit For + Next vItem + Loop Until lastChunkEndPos >= lenDatFile _ + Or buffSize >= lenDatFile + GoTo Continue +Catch: + Select Case Err.Number + Case vbErrKeyAlreadyExists + 'This can happen at chunk boundries, folder might get added twice: + odFolders.Remove folderID 'Make sure the folder gets added new again + Resume 'to avoid folderNames truncated by chunk ends + Case Is <> vbErrOutOfMemory: Err.Raise Err, methodName + End Select + If buffSize > &HFFFFF Then buffSize = buffSize / 2: Resume Try + Err.Raise Err, methodName 'Raise error if less than 1 MB RAM available +Continue: + On Error GoTo 0 + + 'Read cid.ini file + fileNum = FreeFile() + Open vDir & cid & ".ini" For Binary Access Read As #fileNum + ReDim b(0 To LOF(fileNum)): Get fileNum, , b + Close #fileNum: fileNum = 0 + #If Mac Then 'On Mac, the OneDrive settings files use UTF-8 encoding + sUtf8 = b: GoSub DecodeUTF8: + b = sUtf16 'b = StrConv(b, vbUnicode) <- UNRELIABLE + #End If + Select Case True + Case dirName Like "Business#" 'Settings files for a business OD account + 'Max 9 Business OneDrive accounts can be signed in at a time. + Dim libNrToWebColl As Collection: Set libNrToWebColl = New Collection + mainMount = vbNullString + For Each line In Split(b, vbNewLine) + webRoot = "": locRoot = "": parts = Split(line, """") + Select Case Left$(line, InStr(1, line, " = ", 0) - 1) + Case "libraryScope" 'One line per synchronized library + locRoot = parts(9) + syncFind = locRoot: syncID = Split(parts(10), " ")(2) + libNr = Split(line, " ")(2) + folderType = parts(3): parts = Split(parts(8), " ") + siteID = parts(1): webID = parts(2): libID = parts(3) + If mainMount = vbNullString And folderType = "ODB" Then + mainMount = locRoot: FileName = "ClientPolicy.ini" + mainSyncID = syncID: mainSyncFind = syncFind + Else: FileName = "ClientPolicy_" & libID & siteID & ".ini" + End If + On Error Resume Next 'On error try backup method... + webRoot = cliPolColl(FileName)("DavUrlNamespace") + On Error GoTo 0 + If webRoot = "" Then 'Backup method to find webRoot: + For Each vItem In cliPolColl + If vItem("SiteID") = siteID _ + And vItem("WebID") = webID _ + And vItem("IrmLibraryId") = libID Then + webRoot = vItem("DavUrlNamespace"): Exit For + End If + Next vItem + End If + If webRoot = vbNullString Then Err.Raise vbErrFileNotFound _ + , methodName + libNrToWebColl.Add VBA.Array(libNr, webRoot), libNr + If Not locRoot = vbNullString Then _ + locToWebColl.Add VBA.Array(locRoot, webRoot, email, _ + syncID, syncFind, dirName), key:=locRoot + Case "libraryFolder" 'One line per synchronized library folder + libNr = Split(line, " ")(3) + locRoot = parts(1): syncFind = locRoot + syncID = Split(parts(4), " ")(1) + s = vbNullString: parentID = Left$(Split(line, " ")(4), 32) + Do 'If not synced at the bottom dir of the library: + ' -> add folders below mount point to webRoot + On Error Resume Next: odFolders parentID + keyExists = (Err.Number = 0): On Error GoTo 0 + If Not keyExists Then Exit Do + s = odFolders(parentID)(1) & "/" & s + parentID = odFolders(parentID)(0) + Loop + webRoot = libNrToWebColl(libNr)(1) & s + locToWebColl.Add VBA.Array(locRoot, webRoot, email, _ + syncID, syncFind, dirName), locRoot + Case "AddedScope" 'One line per folder added as link to personal + relPath = parts(5): If relPath = " " Then relPath = "" 'lib + parts = Split(parts(4), " "): siteID = parts(1) + webID = parts(2): libID = parts(3): lnkID = parts(4) + FileName = "ClientPolicy_" & libID & siteID & lnkID & ".ini" + On Error Resume Next 'On error try backup method... + webRoot = cliPolColl(FileName)("DavUrlNamespace") & relPath + On Error GoTo 0 + If webRoot = "" Then 'Backup method to find webRoot: + For Each vItem In cliPolColl + If vItem("SiteID") = siteID _ + And vItem("WebID") = webID _ + And vItem("IrmLibraryId") = libID Then + webRoot = vItem("DavUrlNamespace") & relPath + Exit For + End If + Next vItem + End If + If webRoot = vbNullString Then Err.Raise vbErrFileNotFound _ + , methodName + s = vbNullString: parentID = Left$(Split(line, " ")(3), 32) + Do 'If link is not at the bottom of the personal library: + On Error Resume Next: odFolders parentID + keyExists = (Err.Number = 0): On Error GoTo 0 + If Not keyExists Then Exit Do 'add folders below + s = odFolders(parentID)(1) & ps & s 'mount point to + parentID = odFolders(parentID)(0) 'locRoot + Loop + locRoot = mainMount & ps & s + locToWebColl.Add VBA.Array(locRoot, webRoot, email, _ + mainSyncID, mainSyncFind, dirName), locRoot + Case Else: Exit For + End Select + Next line + Case dirName = "Personal" 'Settings files for a personal OD account + 'Only one Personal OneDrive account can be signed in at a time. + For Each line In Split(b, vbNewLine) 'Loop should exit at first line + If line Like "library = *" Then + parts = Split(line, """"): locRoot = parts(3) + syncFind = locRoot: syncID = Split(parts(4), " ")(2) + Exit For + End If + Next line + On Error Resume Next 'This file may be missing if the personal OD + webRoot = cliPolColl("ClientPolicy.ini")("DavUrlNamespace") 'account + On Error GoTo 0 'was logged out of the OneDrive app + If locRoot = "" Or webRoot = "" Or cid = "" Then GoTo NextFolder + locToWebColl.Add VBA.Array(locRoot, webRoot & "/" & cid, email, _ + syncID, syncFind, dirName), key:=locRoot + If Dir(vDir & "GroupFolders.ini") = "" Then GoTo NextFolder + 'Read GroupFolders.ini file + cid = vbNullString: fileNum = FreeFile() + Open vDir & "GroupFolders.ini" For Binary Access Read As #fileNum + ReDim b(0 To LOF(fileNum)): Get fileNum, , b + Close #fileNum: fileNum = 0 + #If Mac Then 'On Mac, the OneDrive settings files use UTF-8 encoding + sUtf8 = b: GoSub DecodeUTF8 + b = sUtf16 'StrConv(b, vbUnicode) is UNRELIABLE + #End If 'Two lines per synced folder from other peoples personal ODs + For Each line In Split(b, vbNewLine) + If line Like "*_BaseUri = *" And cid = vbNullString Then + cid = LCase$(Mid$(line, InStrRev(line, "/", , 0) + 1, _ + InStrRev(line, "!", , 0) - InStrRev(line, "/", , 0) - 1)) + folderID = Left$(line, InStr(line, "_") - 1) + ElseIf cid <> vbNullString Then + locToWebColl.Add VBA.Array(locRoot & ps & odFolders( _ + folderID)(1), webRoot & "/" & cid & "/" & _ + Mid$(line, Len(folderID) + 9), email, _ + syncID, syncFind, dirName), _ + key:=locRoot & ps & odFolders(folderID)(1) + cid = vbNullString: folderID = vbNullString + End If + Next line + End Select +NextFolder: + cid = vbNullString: s = vbNullString: email = vbNullString + Next vDir + + 'Clean the finished "dictionary" up, remove trailing "\" and "/" + Dim tmpColl As Collection: Set tmpColl = New Collection + For Each vItem In locToWebColl + locRoot = vItem(0): webRoot = vItem(1): syncFind = vItem(4) + If Right$(webRoot, 1) = "/" Then _ + webRoot = Left$(webRoot, Len(webRoot) - 1) + If Right$(locRoot, 1) = ps Then _ + locRoot = Left$(locRoot, Len(locRoot) - 1) + If Right$(syncFind, 1) = ps Then _ + syncFind = Left$(syncFind, Len(syncFind) - 1) + tmpColl.Add VBA.Array(locRoot, webRoot, vItem(2), _ + vItem(3), syncFind), locRoot + Next vItem + Set locToWebColl = tmpColl + + #If Mac Then 'deal with syncIDs + If cloudStoragePathExists Then + Set tmpColl = New Collection + For Each vItem In locToWebColl + locRoot = vItem(0): syncID = vItem(3): syncFind = vItem(4) + locRoot = Replace(locRoot, syncFind, _ + syncIDtoSyncDir(syncID)(1), , 1) + tmpColl.Add VBA.Array(locRoot, vItem(1), vItem(2)), locRoot + Next vItem + Set locToWebColl = tmpColl + End If + #End If + + GetLocalPath = GetLocalPath(path, returnAll, pmpo, False): Exit Function + Exit Function +DecodeUTF8: 'StrConv doesn't work reliably, therefore UTF-8 must + #If Mac Then 'be transcoded to UTF-16 manually (yes, this is insane) + utf8 = sUtf8 + ReDim utf16(0 To (UBound(utf8) - LBound(utf8) + 1) * 2) + i = LBound(utf8): k = 0 + Do While i <= UBound(utf8) 'Loop through the UTF-8 byte array + 'Determine the number of bytes in the current UTF-8 codepoint + numBytesOfCodePoint = 1 + If utf8(i) And &H80 Then + If utf8(i) And &H20 Then + If utf8(i) And &H10 Then + numBytesOfCodePoint = 4 + Else: numBytesOfCodePoint = 3: End If + Else: numBytesOfCodePoint = 2: End If + End If + If i + numBytesOfCodePoint - 1 > UBound(utf8) Then _ + Err.Raise vbErrInvalidFormatInResourceFile, methodName + 'Calculate the Unicode codepoint value from the UTF-8 bytes + If numBytesOfCodePoint = 1 Then + codepoint = utf8(i) + Else: codepoint = utf8(i) And (2 ^ (7 - numBytesOfCodePoint) - 1) + For j = 1 To numBytesOfCodePoint - 1 + codepoint = (codepoint * 64) + (utf8(i + j) And &H3F) + Next j + End If + 'Convert the Unicode codepoint to UTF-16LE bytes + If codepoint < &H10000 Then + utf16(k) = codepoint And &HFF& + utf16(k + 1) = codepoint \ &H100& + k = k + 2 + Else 'Codepoint must be encoded as surrogate pair + m = codepoint - &H10000 + highSurrogate = &HD800& Or (m \ &H400&) + lowSurrogate = &HDC00& Or (m And &H3FF) + utf16(k) = highSurrogate And &HFF& + utf16(k + 1) = highSurrogate \ &H100& + utf16(k + 2) = lowSurrogate And &HFF& + utf16(k + 3) = lowSurrogate \ &H100& + k = k + 4 + End If + i = i + numBytesOfCodePoint 'Move to the next UTF-8 codepoint + Loop + If k > 0 Then + ReDim Preserve utf16(0 To k - 1) + sUtf16 = utf16 + Else: sUtf16 = "" + End If + Return 'Jump back to the statement after last encountered GoSub + #End If +End Function + diff --git a/src/Main.bas b/src/Main.bas new file mode 100644 index 0000000..9a29fc9 --- /dev/null +++ b/src/Main.bas @@ -0,0 +1,600 @@ +' Author: Edward Middleton-Smith +' Precision And Research Technology Systems Limited + + +' MODULE INITIALISATION +' Set array start index to 1 to match spreadsheet indices +Option Base 1 +' Forced Variable Declaration +Option Explicit + +' Sleep function +Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) + +Sub Main() +' FUNCTION + ' Create database relationship bbetwee different tables of data in worksheets +' VARIABLE DECLARATION + ' Excel + Dim wb_me As Workbook + Dim wb_cash As Workbook + Dim wb_day As Workbook + Dim wb_customers As Workbook + Dim ws_out As Worksheet + Dim ws_anal As Worksheet + Dim ws_cash As Worksheet + Dim ws_day As Worksheet + Dim ws_customers As Worksheet + ' Worksheet Container Relation + Dim ws_rel As ws_relation + ' Worksheet Containers + Dim wsc_out As ws_access + Dim wsc_cash As ws_access + Dim wsc_day As ws_access + Dim wsc_customers As ws_access + Dim headings_S() As String + ' PowerPoint + Dim pptApp As PowerPoint.Application + Dim ppt As PowerPoint.Presentation + Dim pptSlide As PowerPoint.Slide + Dim paste_shape As PowerPoint.Shape + ' Temporary + Dim headings_V() As Variant + Dim i As Long + Dim j As Long + Dim tmp_total As Long + Dim tmp_heading As String + Dim i_col_out As Long + Dim i_col_cash As Long + Dim i_col_day As Long + Dim tmp_S As String + Dim t_sleep As Long +' VARIABLE INSTANTIATION + t_sleep = 5000 + ' PowerPoint + Set pptApp = New PowerPoint.Application + Set ppt = pptApp.Presentations.Open("C:\Users\edwar\OneDrive\Documents\4 Shires\Alex Automation\4 Shires Books Public\Pivot Linked.pptx") + Set pptSlide = ppt.Slides(2) + pptSlide.Select + Sleep t_sleep + ' Worksheet Relation + Set ws_rel = New ws_relation + Set wb_me = ActiveWorkbook + Set ws_out = create_sheet_out(wb_me) + Set ws_anal = wb_me.Sheets("Analysis") + get_downloaded_sheets wb_me, wb_cash, ws_cash, wb_day, ws_day, wb_customers, ws_customers, ws_rel, t_sleep + ' Worksheet Containers + ' Cashbook + headings_V = Array("BATCH NO", "BATCH DATE", "TRAN DATE", "CUST REF", "CUSTOMER NAME", "", "TRAN REF", "FURTHER REF", "", "", "", "", "", "", "CASH", "DISCOUNT", "", "", "", "", "", "", "") + convert_1D_Variant_2_String headings_V, headings_S + Set wsc_cash = New ws_access + wsc_cash.Init ws_cash, "Cashbook", headings_S, ColumnHeaders, 7, True, 3, False, 1, 0, 4 + ws_rel.AddWSC wsc_cash + ' Daybook + Erase headings_S + Erase headings_V + headings_V = Array("BATCH NO", "BATCH DATE", "TRAN DATE", "CUST REF", "CUST NAME", "CASH STATUS", "TRAN REF", "FURTHER REF", "", "GOODS", "VAT", "TOT INV", "TOT CRN", "ACC STAT", "", "", "", "", "", "", "", "", "") + convert_1D_Variant_2_String headings_V, headings_S + Set wsc_day = New ws_access + wsc_day.Init ws_day, "Daybook", headings_S, ColumnHeaders, 7, True, 3, False, 1, 0, 4 + If Not ws_rel.AddWSC(wsc_day) Then GoTo errhand + ' Customer List + Erase headings_S + Erase headings_V + headings_V = Array("", "", "", "A/C", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Name") + convert_1D_Variant_2_String headings_V, headings_S + Set wsc_customers = New ws_access + wsc_customers.Init ws_customers, "Customer List", headings_S, ColumnHeaders, 4, True, 6, False, 2, 0, 7, 0, 10 + If Not ws_rel.AddWSC(wsc_customers) Then GoTo errhand + ' Export + ' Set ws_out = create_sheet_out() + Erase headings_S + Erase headings_V + headings_V = Array("BATCH NO", "BATCH DATE", "Date", "Account Reference", "CUST NAME", "IMPORT ID NO.", "Reference", "Extra Reference", "User Name", "", "Tax Amount", "", "", "CASH STATUS", "", "", "Type", "Nominal A/C Ref", "Details", "Net Amount", "Tax Code", "TOT INV", "CUST NAME VLOOKUP") + convert_1D_Variant_2_String headings_V, headings_S + Set wsc_out = New ws_access + wsc_out.Init ws_out, "Export", headings_S, ColumnHeaders, 7, True, 1, False, 1, 0, 2 + If Not ws_rel.AddWSC(wsc_out) Then GoTo errhand + ' Worksheet size + tmp_total = wsc_cash.RowMax - wsc_cash.RowMin + 1 +' PROCESSING ACCELERATION + ' Disable automatic spreadsheet calculation - prevents refreshing of whole ws on each cell entry + Application.Calculation = xlCalculationManual + ' Disable screen updating + Application.ScreenUpdating = False +' METHODS + ' Populate export wsc with references from cashbook, daybook + wsc_out.ResizeLocal wsc_out.ColumnMin, wsc_out.ColumnMax, wsc_out.RowMin, wsc_cash.RowMax - wsc_cash.RowMin + wsc_day.RowMax - wsc_day.RowMin + wsc_out.RowMin + 1, True + ' Cashbook + For j = 1 To tmp_total + ' ' Set key value for record-wise population + ' wsc_out.SetCellDataLocal j, wsc_out.ColumnSearch, wsc_cash.GetCellDataLocal(j, wsc_cash.ColumnSearch) + + ' Set all available values + For i = 1 To ws_rel.nHeadings + i_col_out = wsc_out.ColumnID(CStr(i), True) + If i_col_out > 0 Then + wsc_out.Cell(j + wsc_out.RowMin - 1, i_col_out + wsc_out.ColumnMin - 1).Interior.ColorIndex = 17 ' 6 + i_col_cash = wsc_cash.ColumnID(CStr(i), True) + If i_col_cash > 0 Then + If tmp_heading = "Net Amount" Then + wsc_out.SetCellDataLocal j, i_col_out, CMoney_String(CDbl(wsc_cash.GetCellDataLocal(j, i_col_cash))) + Else + wsc_out.SetCellDataLocal j, i_col_out, wsc_cash.GetCellDataLocal(j, i_col_cash) + End If + Else + tmp_heading = wsc_out.HeadingName(i) + If tmp_heading = "Type" Or tmp_heading = "Details" Then + ' pass + ElseIf tmp_heading = "Nominal A/C Ref" Then + tmp_S = Left(wsc_cash.GetCellDataLocal(j, wsc_cash.ColumnID("FURTHER REF")), 2) + If tmp_S = "BA" Or tmp_S = "BX" Or tmp_S = "CQ" Then + wsc_out.SetCellDataLocal j, i_col_out, "1200" + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Tax Code"), "T9" + If wsc_cash.GetCellDataLocal(j, wsc_cash.ColumnID("FURTHER REF")) = "BX DUPLICATE ENTRY" Then + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Details"), "REFUND" + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Type"), "SP" + Else + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Details"), "PAYMENT RECEIVED" + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Type"), "SA" + End If + ElseIf tmp_S = "CA" Then + wsc_out.SetCellDataLocal j, i_col_out, "1230" + If CLng(wsc_cash.GetCellDataLocal(j, wsc_cash.ColumnID("CASH"))) < 0 Then + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Details"), "REFUND" + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Type"), "SP" + Else + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Details"), "PAYMENT RECEIVED" + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Type"), "SA" + End If + ElseIf tmp_S = "CC" Or tmp_S = "WE" Then + wsc_out.SetCellDataLocal j, i_col_out, "1250" + If CLng(wsc_cash.GetCellDataLocal(j, wsc_cash.ColumnID("CASH"))) < 0 Then + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Details"), "REFUND" + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Type"), "SP" + Else + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Details"), "PAYMENT RECEIVED" + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Type"), "SA" + End If + Else + wsc_out.SetCellDataLocal j, i_col_out, "1255" + If CLng(wsc_cash.GetCellDataLocal(j, wsc_cash.ColumnID("CASH"))) < 0 Then + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Details"), "REFUND" + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Type"), "SP" + Else + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Details"), "PAYMENT RECEIVED" + wsc_out.SetCellDataLocal j, wsc_out.ColumnID("Type"), "SA" + End If + End If + ElseIf tmp_heading = "Net Amount" Then + wsc_out.SetCellDataLocal j, i_col_out, CMoney_String(Abs(CDbl(wsc_cash.GetCellDataLocal(j, wsc_cash.ColumnID("CASH"))))) + ElseIf tmp_heading = "Tax Code" Then + wsc_out.SetCellDataLocal j, i_col_out, "T9" + ElseIf tmp_heading = "Tax Amount" Then + wsc_out.SetCellDataLocal j, i_col_out, "'0.00" + ElseIf tmp_heading = "User Name" Then + wsc_out.SetCellDataLocal j, i_col_out, "ALEX-IMPORTED" + End If + End If + End If + Next + Next + ' Daybook + For j = 1 To wsc_day.RowMax - wsc_day.RowMin + 1 + ' ' Set key value for record-wise population + ' wsc_out.SetCellDataLocal tmp_total + j, wsc_out.ColumnSearch, wsc_day.GetCellDataLocal(j, wsc_day.ColumnSearch) + + ' Set all available values + For i = 1 To ws_rel.nHeadings + i_col_out = wsc_out.ColumnID(CStr(i), True) + If i_col_out > 0 Then + wsc_out.Cell(tmp_total + j + wsc_out.RowMin - 1, i_col_out + wsc_out.ColumnMin - 1).Interior.ColorIndex = 42 ' 50 + i_col_day = wsc_day.ColumnID(CStr(i), True) + tmp_heading = wsc_out.HeadingName(i) + If i_col_day > 0 Then + ' If tmp_heading = "Tax Amount" Or tmp_heading = "TOT INV" Then + ' wsc_out.SetCellDataLocal tmp_total + j, i_col_out, CMoney_String(CDbl(wsc_day.GetCellDataLocal(j, i_col_day))) + ' Else + wsc_out.SetCellDataLocal tmp_total + j, i_col_out, wsc_day.GetCellDataLocal(j, i_col_day) + ' End If + Else + If tmp_heading = "Type" Then + ' pass + If CLng(wsc_day.GetCellDataLocal(j, wsc_day.ColumnID("GOODS"))) < 0 Then + wsc_out.SetCellDataLocal tmp_total + j, wsc_out.ColumnID("Type"), "SC" + wsc_out.SetCellDataLocal tmp_total + j, wsc_out.ColumnID("Details"), "CREDIT" + Else + wsc_out.SetCellDataLocal tmp_total + j, wsc_out.ColumnID("Type"), "SI" + wsc_out.SetCellDataLocal tmp_total + j, wsc_out.ColumnID("Details"), "INVOICE" + End If + ElseIf tmp_heading = "Nominal A/C Ref" Then + wsc_out.SetCellDataLocal tmp_total + j, wsc_out.ColumnSearch, "4000" + ElseIf tmp_heading = "Net Amount" Then + wsc_out.SetCellDataLocal tmp_total + j, i_col_out, CMoney_String(Abs(CDbl(wsc_day.GetCellDataLocal(j, wsc_day.ColumnID("GOODS"))))) + wsc_out.SetCellDataLocal tmp_total + j, wsc_out.ColumnID("Tax Amount"), CMoney_String(Abs(CDbl(wsc_day.GetCellDataLocal(j, wsc_day.ColumnID("VAT"))))) + wsc_out.SetCellDataLocal tmp_total + j, wsc_out.ColumnID("TOT INV"), CMoney_String(CDbl(wsc_day.GetCellDataLocal(j, wsc_day.ColumnID("GOODS"))) + CDbl(wsc_day.GetCellDataLocal(j, wsc_day.ColumnID("VAT")))) + ElseIf tmp_heading = "Tax Code" Then + wsc_out.SetCellDataLocal tmp_total + j, i_col_out, "T1" + ElseIf tmp_heading = "User Name" Then + wsc_out.SetCellDataLocal tmp_total + j, i_col_out, "ALEX-IMPORTED" + End If + End If + End If + Next + Next + ' Customer name 'vlookup' + wsc_out.ColumnSearch = wsc_out.ColumnID("Account Reference") + ws_rel.Populate "Export", False, "Customer List", False + wsc_out.ExportLocalData2WS + + ' ' Populate matching columns + ' ws_rel.Populate "Export", False, "Cashbook", False, True + ' ws_rel.Populate "Export", False, "Daybook", False, True + +' RETURNS + ' WS data + wsc_out.ExportLocalData2WS + ' table objects + ws_out.ListObjects.Add(xlSrcRange, ws_out.Range("$A$1:$W$" & CStr(wsc_out.RowMax)), , xlYes).name = "tbl_audit" ' 'Audit Trail'! + ws_anal.PivotTables("pivot_audit").ClearTable + With ws_anal.PivotTables("pivot_audit") + .ColumnGrand = True + .HasAutoFormat = True + .DisplayErrorString = False + .DisplayNullString = True + .EnableDrilldown = True + .ErrorString = "" + .MergeLabels = False + .NullString = "" + .PageFieldOrder = 2 + .PageFieldWrapCount = 0 + .PreserveFormatting = True + .RowGrand = True + .SaveData = True + .PrintTitles = False + .RepeatItemsOnEachPrintedPage = True + .TotalsAnnotation = False + .CompactRowIndent = 1 + .InGridDropZones = False + .DisplayFieldCaptions = True + .DisplayMemberPropertyTooltips = False + .DisplayContextTooltips = True + .ShowDrillIndicators = True + .PrintDrillIndicators = False + .AllowMultipleFilters = False + .SortUsingCustomLists = True + .FieldListSortAscending = False + .ShowValuesRow = False + .CalculatedMembersInFilters = False + .RowAxisLayout xlCompactRow + End With + With ws_anal.PivotTables("pivot_audit").PivotCache + .RefreshOnFileOpen = False + .MissingItemsLimit = xlMissingItemsDefault + End With + ws_anal.PivotTables("pivot_audit").RepeatAllLabels xlRepeatLabels + With ws_anal.PivotTables("pivot_audit").PivotFields("CUST NAME") + .orientation = xlRowField + .position = 1 + End With + ws_anal.PivotTables("pivot_audit").AddDataField ws_anal.PivotTables( _ + "pivot_audit").PivotFields("Net Amount"), "Sum of Net Amount", xlSum + ' Bring presentation to front + ' AppActivate ppt.path + ' Set ppt = pptApp.Presentations.Open("C:\Users\edwar\OneDrive\Documents\4 Shires\Alex Automation\4 Shires Books Public\Pivot Linked.pptx") + ' PowerPoint.Presentations(ppt.path).Windows(1).Activate + ws_anal.Range("A1:B27").Copy + ' ppt.Slides(2).Shapes(1).TextFrame.TextRange.PasteSpecial + pptSlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile + ' pptApp.ActiveWindow.View.PasteSpecial ppPasteEnhancedMetafile + Set paste_shape = pptSlide.Shapes(pptSlide.Shapes.count) + paste_shape.Left = paste_shape.Left - 150 + ws_anal.Range("A28:B54").Copy + ' ppt.Slides(2).Shapes(1).TextFrame.TextRange.PasteSpecial + pptSlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile + ' pptApp.ActiveWindow.View.PasteSpecial ppPasteEnhancedMetafile + Set paste_shape = pptSlide.Shapes(pptSlide.Shapes.count) + paste_shape.Left = paste_shape.Left + 150 + ' Add overdue statuses + ws_anal.Cells(1, 3).value = "Overdue Status" + ws_anal.Cells(1, 3).Interior.Color = RGB(209, 225, 239) ' "#D1E1EF" + ws_anal.Cells(1, 3).Font.Bold = True + ws_anal.Columns("C:C").ColumnWidth = 14.14 + For i = 2 To 53 Step 1 + If (i < 5) Then + ws_anal.Cells(i, 3).value = OverdueStatusName(i - 2) + Else + ws_anal.Cells(i, 3).value = OverdueStatusName(CLng(Rnd() * 2)) + End If + Next + ' Emails + ' MsgBox "Ready to send emails?" + ' Sleep 25000 + ' Scroll through processed data, ppt + mail_account_balances ws_anal, ws_customers +' ERROR HANDLING +errhand: + On Error Resume Next + wb_cash.Close + wb_day.Close + wb_customers.Close +' endgame: +' PROCESSING DECELARATION + ' Enable automatic spreadsheet calculation - prevents refreshing of whole ws on each cell entry + Application.Calculation = xlCalculationAutomatic + ' Enable screen updating + Application.ScreenUpdating = True +End Sub + +Sub mail_account_balances(ByRef ws_anal As Worksheet, ByRef ws_customer As Worksheet) +' FUNCTION + ' Contact all customers (as necessary) regarding their account balance +' CONSTANTS + Const nCustomer = 52 +' VARIABLE DECLARATION + Dim olApp As Outlook.Application + ' Dim olNS As Outlook.Namespace + Dim ws_rel As ws_relation + Dim wsc_anal As ws_access + Dim wsc_customer As ws_access + Dim headings_V() As Variant + Dim headings_S() As String + ' Iterables + Dim iRow As Long + Dim iRowCustomer As Long + Dim colIDAnal_Company As Long + Dim colIDAnal_Balance As Long + Dim colIDAnal_status As Long + Dim colIDCustomer_Contact As Long + Dim colIDCustomer_Address As Long +' VARIABLE INSTANTIATION + Set olApp = New Outlook.Application + ' Set olNS = olApp.GetNamespace("MAPI") + ' Worksheet Relation + Set ws_rel = New ws_relation + ' Worksheet Containers + ' Customers + headings_V = Array("A/C", "Name", "Contact Name", "Telephone", "Email", "", "") + convert_1D_Variant_2_String headings_V, headings_S + Set wsc_customer = New ws_access + ' Init(ByRef ws As Worksheet, ByVal name As String, ByRef headings() As String, Optional Orient As orientation = orientation.ColumnHeaders, Optional search_col As Long = 1, Optional search_col_is_heading_index As Boolean = False, Optional search_row As Long = 1, Optional search_row_is_heading_index As Boolean = False, Optional col_min As Long = 1, Optional col_max As Long = 0, Optional row_min As Long = 2, Optional row_max As Long = 0, Optional gap_max As Long = 1, Optional mutable_headings As Boolean = False) + ' wsc_customer.Init ws_customer, "Customers", headings_S, ColumnHeaders, 2, True, 6, False, 1, 0, 7 + wsc_customer.Init ws_customer, "Customer List", headings_S, ColumnHeaders, 2, True, 6, False, 2, 0, 7, 0, 10 + ws_rel.AddWSC wsc_customer + ' Analysis + Erase headings_S + Erase headings_V + headings_V = Array("", "Row Labels", "", "", "", "Sum of Net Amount", "Overdue Status") + convert_1D_Variant_2_String headings_V, headings_S + Set wsc_anal = New ws_access + wsc_anal.Init ws_anal, "Analysis", headings_S, ColumnHeaders, 1, False, 1, False, 1, 0, 2 + If Not ws_rel.AddWSC(wsc_anal) Then GoTo errhand + ' Indices + colIDAnal_Company = wsc_anal.ColumnID("Row Labels") + colIDAnal_Balance = wsc_anal.ColumnID("Sum of Net Amount") + colIDAnal_status = wsc_anal.ColumnID("Overdue Status") + colIDCustomer_Contact = wsc_customer.ColumnID("Contact Name") + colIDCustomer_Address = wsc_customer.ColumnID("Email") +' METHODS + For iRow = 2 To 5 Step 1 ' 1 + nCustomer + iRowCustomer = wsc_customer.Match(wsc_anal.GetCellDataLocal(iRow - 1, colIDAnal_Company)) + mail_account_balance olApp, _ + wsc_customer.GetCellDataLocal(iRow - 1, colIDCustomer_Contact), _ + wsc_customer.GetCellDataLocal(iRow - 1, colIDCustomer_Address), _ + GetOverdueStatus(wsc_anal.GetCellDataLocal(iRow - 1, colIDAnal_status)), _ + CLng(wsc_anal.GetCellDataLocal(iRow - 1, colIDAnal_Balance)) + Next +' RETURNS +' ERROR HANDLING +errhand: + Exit Sub +End Sub + +Sub mail_account_balance(ByRef olApp As Outlook.Application, ByVal name As String, ByVal address As String, ByVal overdue_status As OverdueStatus, ByVal balance As Long) +' FUNCTION + ' Send email to account holder regarding balance +' ARGUMENTS + ' Outlook.Application olApp + ' String name + ' String address + ' OverdueStatus overdue_status + ' String balance +' VARIABLE DECLARATION + Dim new_mail As Object + Dim mail_body As String + Dim subj As String + +' VARIABLE INSTANTIATION + Set new_mail = olApp.CreateItem(olMailItem) + + mail_body = "" + mail_body = mail_body & "
Dear " & name & ",
" + +' METHODS + Select Case overdue_status + Case OverdueStatus.OVERDUE: + mail_body = mail_body & "Your account is overdue with a balance of £" & CStr(balance) & ".
"
+ mail_body = mail_body & "Please resolve your balance as soon as possible.
Your account is overdue with a balance of £" & CStr(balance) & ".
"
+ mail_body = mail_body & "Please resolve your balance as soon as possible.
Kind regards,
"
+ mail_body = mail_body & "Lord Edward Middleton-Smith
"
+ mail_body = mail_body & "Director
"
+ mail_body = mail_body & "Precision And Research Technology Systems Limited"
+
+' RETURNS
+ With new_mail
+ .To = address
+ .Subject = subj
+ .HTMLBody = mail_body
+ .Display
+ End With
+End Sub
+
+Sub get_downloaded_sheets(ByRef wb As Workbook, ByRef wb_cash As Workbook, ByRef ws_cash As Worksheet, ByRef wb_day As Workbook, ByRef ws_day As Worksheet, ByRef wb_customers As Workbook, ByRef ws_customers As Worksheet, ByRef ws_rel As ws_relation, ByVal t_sleep As Long)
+' FUNCTION
+ ' Get downloaded cashbook, daybook, and customer list
+' ARGUMENTS
+ ' Worksheet ws_cash
+ ' Worksheet ws_day
+ ' Worksheet ws_customers
+ ' ws_relation ws_rel
+' PROCESSING ACCELERATION
+' CONSTANTS
+' VARIABLE DECLARATION
+ ' Dim wb_cash As Workbook
+ ' Dim wb_day As Workbook
+ ' Dim wb_customers As Workbook
+ Dim fold As Scripting.Folder
+ Dim f As Scripting.File
+ Dim objFSO As Scripting.FileSystemObject
+ Dim path_S As String
+ Dim suffix As String
+ Dim iTmp As Long
+ Dim sTmp As String
+' ARGUMENT VALIDATION
+' VARIABLE INSTANTIATION
+ Set objFSO = New Scripting.FileSystemObject
+ path_S = GetLocalPath(wb.FullName)
+ path_S = Left(path_S, Len(path_S) - Len(wb.name) - 1)
+ If (Left(path_S, 41) = "https://d.docs.live.net/5728c5526437cee2/") Then
+ path_S = "C:\Users\edwar\OneDrive\" & Mid(path_S, 42)
+ End If
+ Debug.Print path_S
+ Set fold = objFSO.GetFolder(path_S)
+' METHODS
+ For Each f In fold.Files
+ suffix = f.name
+ iTmp = InStr(1, suffix, ".xl")
+ If iTmp > 0 Then
+ suffix = Mid(suffix, iTmp)
+ suffix = Mid(suffix, 2, 2)
+ If suffix = "xl" Then
+ ' If f.Type = "Microsoft Macro-Enabled Workbook" Then
+ sTmp = f.name
+ If Not sTmp = wb.name Then
+ If Left(sTmp, 10) = "SL DAYBOOK" Then ' daybook
+ sTmp = f.path
+ Debug.Print sTmp
+ ' Set wb_day = Workbooks.Open(sTmp)
+ Set wb_day = ws_rel.SafeOpenWB(sTmp)
+ Set ws_day = wb_day.Sheets("Daybook")
+ ws_day.Activate
+ ws_day.Select
+ Sleep t_sleep
+ ElseIf Left(sTmp, 11) = "SL CASHBOOK" Then ' cashbook
+ sTmp = f.path
+ Debug.Print sTmp
+ ' Set wb_cash = Workbooks.Open(sTmp)
+ Set wb_cash = ws_rel.SafeOpenWB(sTmp)
+ Set ws_cash = wb_cash.Sheets("Cashbook")
+ ws_cash.Activate
+ ws_cash.Select
+ Sleep t_sleep
+ ElseIf sTmp = "Customers.xlsx" Then ' customer list
+ sTmp = f.path
+ Debug.Print sTmp
+ ' Set wb_customers = Workbooks.Open(sTmp)
+ Set wb_customers = ws_rel.SafeOpenWB(sTmp)
+ Set ws_customers = wb_customers.Sheets("Customer List")
+ ws_customers.Activate
+ ws_customers.Select
+ Sleep t_sleep
+ End If
+ End If
+ End If
+ End If
+ Next
+' RETURNS
+' ERROR HANDLING
+' PROCESSING DECELARATION
+End Sub
+
+Function create_sheet_out(ByRef wb As Workbook) As Worksheet
+' FUNCTION
+ ' Create new output sheet
+' ARGUMENTS
+ ' Workbook wb
+' PROCESSING ACCELERATION
+' CONSTANTS
+' VARIABLE DECLARATION
+ Dim ws As Worksheet
+ Dim headings_V() As Variant
+ Dim colours_V() As Variant
+ Dim i As Long
+ Dim N As Long
+ Dim found As Boolean
+ Dim w As Long
+' ARGUMENT VALIDATION
+' VARIABLE INSTANTIATION
+ headings_V = Array("Type", "Account Reference", "Nominal A/C Ref", "Department Code", "Date", "Reference", "Details", "Net Amount", "Tax Code", "Tax Amount", "Exchange Rate", "Extra Reference", "User Name", "Project Refn", "Cost Code Refn", "TOT INV", "IMPORT ID NO.", "BATCH NO", "BATCH DATE", "CASH STATUS", "NO", "CUST NAME", "CUST NAME VLOOKUP")
+ ' colours_V = Array("50", "50", "50", "27", "50", "27", "27", "50", "50", "50", "27", "27", "27", "27", "27", "15", "15", "15", "15", "15", "15", "15", "15")
+ colours_V = Array("42", "42", "42", "17", "42", "17", "17", "42", "42", "42", "17", "17", "17", "17", "17", "15", "15", "15", "15", "15", "15", "15", "15")
+ N = SizeArrayDim_Variant(headings_V)
+ Set ws = wb.Sheets.Add()
+ ' Name ws
+ found = True
+ i = 0
+ Do While found
+ found = False
+ For w = 1 To wb.Sheets.count
+ If i = 0 Then
+ If wb.Sheets.Item(w).name = "Audit Trail" Then
+ found = True
+ Exit For
+ End If
+ Else
+ If wb.Sheets.Item(w).name = "Audit Trail " & CStr(i) Then
+ found = True
+ Exit For
+ End If
+ End If
+ Next
+ If found Then i = i + 1
+ Loop
+ If i = 0 Then
+ ws.name = "Audit Trail"
+ Else
+ ws.name = "Audit Trail " & CStr(i)
+ End If
+' METHODS
+ For i = 1 To N
+ ws.Cells(1, i).value = headings_V(i)
+ ws.Cells(1, i).Interior.ColorIndex = CLng(colours_V(i))
+ Next
+' RETURNS
+ Set create_sheet_out = ws
+' ERROR HANDLING
+' PROCESSING DECELARATION
+End Function
+
+Function OverdueStatusName(ByVal overdue_status As OverdueStatus) As String
+ Select Case overdue_status
+ Case UNDUE:
+ OverdueStatusName = "Not due"
+ Case OVERDUE:
+ OverdueStatusName = "Overdue"
+ Case SUPEROVERDUE:
+ OverdueStatusName = "Super Overdue"
+ End Select
+End Function
+
+Function GetOverdueStatus(ByVal overdue_status As String) As OverdueStatus
+ Select Case overdue_status
+ Case "Not due":
+ GetOverdueStatus = OverdueStatus.UNDUE
+ Case "Overdue":
+ GetOverdueStatus = OverdueStatus.OVERDUE
+ Case "Super Overdue":
+ GetOverdueStatus = OverdueStatus.SUPEROVERDUE
+ End Select
+End Function
diff --git a/src/Matrix_Operations.bas b/src/Matrix_Operations.bas
new file mode 100644
index 0000000..2977351
--- /dev/null
+++ b/src/Matrix_Operations.bas
@@ -0,0 +1,1474 @@
+' Author: Edward Middleton-Smith
+' Precision And Research Technology Systems Limited
+
+
+' MODULE INITIALISATION
+' Set array start index to 1 to match spreadsheet indices
+Option Base 1
+' Forced Variable Declaration
+Option Explicit
+
+
+Sub ReDimPreserve_String(ByRef arr() As String, ByVal dimension As Long, ByVal newbound As Long, ByVal NDim As Long)
+' FUNCTION
+ ' Redimensionalise array without losing data (unless shrinking)
+' ARGUMENTS
+ ' String Array arr
+ ' Long dimension - dimension of arr to change
+ ' Long newbound - new size of dimension
+ ' Long ndim - number of dimensions of arr
+' VARIABLE DECLARATION
+ Dim x() As Long ' Iterables for each dimension
+ Dim N() As Long ' Size of each dimension
+ Dim i As Long
+ Dim j As Long
+ Dim iterate As Boolean
+ Dim Outs() As String
+ Dim Nold() As Long
+ Dim minbound As Long
+' VARIABLE INSTANTIATION
+ ReDim x(NDim)
+ ReDim N(NDim)
+ iterate = True
+' METHODS
+ ' Populate N
+ For i = 1 To NDim
+ x(i) = 1
+ N(i) = SizeArrayDim_String(arr, i)
+ Next
+ Nold = N
+ N(dimension) = newbound
+ minbound = min_Long(newbound, Nold(dimension))
+ ' Redimensionalise outputs
+ Select Case NDim
+ Case 1
+ ReDim Outs(N(1))
+ Case 2
+ ReDim Outs(N(1), N(2))
+ Case 3
+ ReDim Outs(N(1), N(2), N(3))
+ Case 4
+ ReDim Outs(N(1), N(2), N(3), N(4))
+ Case 5
+ ReDim Outs(N(1), N(2), N(3), N(4), N(5))
+ Case Else
+ MsgBox "Too many dimensions"
+ Exit Sub
+ End Select
+ ' Fill values
+ Do While iterate
+ ' Fill value
+ Select Case NDim
+ Case 1
+ Outs(x(1)) = arr(x(1))
+ Case 2
+ Outs(x(1), x(2)) = arr(x(1), x(2))
+ Case 3
+ Outs(x(1), x(2), x(3)) = arr(x(1), x(2), x(3))
+ Case 4
+ Outs(x(1), x(2), x(3), x(4)) = arr(x(1), x(2), x(3), x(4))
+ Case 5
+ Outs(x(1), x(2), x(3), x(4), x(5)) = arr(x(1), x(2), x(3), x(4), x(5))
+ Case Else
+ MsgBox "Too many dimensions"
+ Exit Sub
+ End Select
+ ' Iterate position
+ For i = NDim To 1 Step -1
+ If (i = dimension) Then
+ If (x(dimension) < minbound) Then
+ x(dimension) = x(dimension) + 1
+ If (dimension < NDim) Then
+ For j = dimension + 1 To NDim
+ x(j) = 1
+ Next
+ End If
+ Exit For
+ End If
+ ElseIf (x(i) < N(i)) Then
+ x(i) = x(i) + 1
+ If (i < NDim) Then
+ For j = i + 1 To NDim
+ x(j) = 1
+ Next
+ End If
+ Exit For
+ End If
+ If (i = 1) Then
+ iterate = False
+ End If
+ Next
+ Loop
+' RETURNS
+ arr = Outs
+End Sub
+
+
+Sub ReDimPreserve_Long(ByRef arr() As Long, ByVal dimension As Long, ByVal newbound As Long, ByVal NDim As Long)
+' FUNCTION
+ ' Redimensionalise array without losing data (unless shrinking)
+' ARGUMENTS
+ ' Long Array arr
+ ' Long dimension - dimension of arr to change
+ ' Long newbound - new size of dimension
+ ' Long ndim - number of dimensions of arr
+' VARIABLE DECLARATION
+ Dim x() As Long ' Iterables for each dimension
+ Dim N() As Long ' Size of each dimension
+ Dim i As Long
+ Dim j As Long
+ Dim iterate As Boolean
+ Dim Outs() As Long
+ Dim Nold() As Long
+ Dim minbound As Long
+' VARIABLE INSTANTIATION
+ ReDim x(NDim)
+ ReDim N(NDim)
+ iterate = True
+' METHODS
+ ' Populate N
+ For i = 1 To NDim
+ x(i) = 1
+ N(i) = SizeArrayDim_Long(arr, i)
+ Next
+ Nold = N
+ N(dimension) = newbound
+ minbound = min_Long(newbound, Nold(dimension))
+ ' Redimensionalise outputs
+ Select Case NDim
+ Case 1
+ ReDim Outs(N(1))
+ Case 2
+ ReDim Outs(N(1), N(2))
+ Case 3
+ ReDim Outs(N(1), N(2), N(3))
+ Case 4
+ ReDim Outs(N(1), N(2), N(3), N(4))
+ Case 5
+ ReDim Outs(N(1), N(2), N(3), N(4), N(5))
+ Case Else
+ MsgBox "Too many dimensions"
+ Exit Sub
+ End Select
+ ' Fill values
+ Do While iterate
+ ' Fill value
+ Select Case NDim
+ Case 1
+ Outs(x(1)) = arr(x(1))
+ Case 2
+ Outs(x(1), x(2)) = arr(x(1), x(2))
+ Case 3
+ Outs(x(1), x(2), x(3)) = arr(x(1), x(2), x(3))
+ Case 4
+ Outs(x(1), x(2), x(3), x(4)) = arr(x(1), x(2), x(3), x(4))
+ Case 5
+ Outs(x(1), x(2), x(3), x(4), x(5)) = arr(x(1), x(2), x(3), x(4), x(5))
+ Case Else
+ MsgBox "Too many dimensions"
+ Exit Sub
+ End Select
+ ' Iterate position
+ For i = NDim To 1 Step -1
+ If (i = dimension) Then
+ If (x(dimension) < minbound) Then
+ x(dimension) = x(dimension) + 1
+ If (dimension < NDim) Then
+ For j = dimension + 1 To NDim
+ x(j) = 1
+ Next
+ End If
+ Exit For
+ End If
+ ElseIf (x(i) < N(i)) Then
+ x(i) = x(i) + 1
+ If (i < NDim) Then
+ For j = i + 1 To NDim
+ x(j) = 1
+ Next
+ End If
+ Exit For
+ End If
+ If (i = 1) Then
+ iterate = False
+ End If
+ Next
+ Loop
+' RETURNS
+ arr = Outs
+End Sub
+
+
+Sub ReDimPreserve_Variant(ByRef arr() As Variant, ByVal dimension As Long, ByVal newbound As Long, ByVal NDim As Long)
+' FUNCTION
+ ' Redimensionalise array without losing data (unless shrinking)
+' ARGUMENTS
+ ' Variant Array arr
+ ' Long dimension - dimension of arr to change
+ ' Long newbound - new size of dimension
+ ' Long ndim - number of dimensions of arr
+' VARIABLE DECLARATION
+ Dim x() As Long ' Iterables for each dimension
+ Dim N() As Long ' Size of each dimension
+ Dim i As Long
+ Dim j As Long
+ Dim iterate As Boolean
+ Dim Outs() As Variant
+ Dim Nold() As Long
+ Dim minbound As Long
+' VARIABLE INSTANTIATION
+ ReDim x(NDim)
+ ReDim N(NDim)
+ iterate = True
+' METHODS
+ ' Populate N
+ For i = 1 To NDim
+ x(i) = 1
+ N(i) = SizeArrayDim_Variant(arr, i)
+ Next
+ Nold = N
+ N(dimension) = newbound
+ minbound = min_Long(newbound, Nold(dimension))
+ ' Redimensionalise outputs
+ Select Case NDim
+ Case 1
+ ReDim Outs(N(1))
+ Case 2
+ ReDim Outs(N(1), N(2))
+ Case 3
+ ReDim Outs(N(1), N(2), N(3))
+ Case 4
+ ReDim Outs(N(1), N(2), N(3), N(4))
+ Case 5
+ ReDim Outs(N(1), N(2), N(3), N(4), N(5))
+ Case Else
+ MsgBox "Too many dimensions"
+ Exit Sub
+ End Select
+ ' Fill values
+ Do While iterate
+ ' Fill value
+ Select Case NDim
+ Case 1
+ Outs(x(1)) = arr(x(1))
+ Case 2
+ Outs(x(1), x(2)) = arr(x(1), x(2))
+ Case 3
+ Outs(x(1), x(2), x(3)) = arr(x(1), x(2), x(3))
+ Case 4
+ Outs(x(1), x(2), x(3), x(4)) = arr(x(1), x(2), x(3), x(4))
+ Case 5
+ Outs(x(1), x(2), x(3), x(4), x(5)) = arr(x(1), x(2), x(3), x(4), x(5))
+ Case Else
+ MsgBox "Too many dimensions"
+ Exit Sub
+ End Select
+ ' Iterate position
+ For i = NDim To 1 Step -1
+ If (i = dimension) Then
+ If (x(dimension) < minbound) Then
+ x(dimension) = x(dimension) + 1
+ If (dimension < NDim) Then
+ For j = dimension + 1 To NDim
+ x(j) = 1
+ Next
+ End If
+ Exit For
+ End If
+ ElseIf (x(i) < N(i)) Then
+ x(i) = x(i) + 1
+ If (i < NDim) Then
+ For j = i + 1 To NDim
+ x(j) = 1
+ Next
+ End If
+ Exit For
+ End If
+ If (i = 1) Then
+ iterate = False
+ End If
+ Next
+ Loop
+' RETURNS
+ arr = Outs
+End Sub
+
+
+Function SizeArrayDim_String(ByRef arr() As String, Optional dimension As Long = 1) As Long
+' FUNCTION
+ ' Find size of dimension of arr
+' VARIABLE INSTANTIATION
+ dimension = max_Long(1, dimension)
+' METHODS
+ On Error GoTo errhand
+ If Not ((Not arr) = -1) Then
+ SizeArrayDim_String = UBound(arr, dimension) - LBound(arr, dimension) + 1
+ Else
+ SizeArrayDim_String = 0
+ End If
+ Exit Function
+' ERROR HANDLING
+errhand:
+ SizeArrayDim_String = 0
+End Function
+
+
+Function SizeArrayDim_Long(ByRef arr() As Long, Optional dimension As Long = 1) As Long
+' FUNCTION
+ ' Find size of dimension of arr
+' VARIABLE INSTANTIATION
+ dimension = max_Long(1, dimension)
+' METHODS
+ On Error GoTo errhand
+ If Not ((Not arr) = -1) Then
+ SizeArrayDim_Long = UBound(arr, dimension) - LBound(arr, dimension) + 1
+ Else
+ SizeArrayDim_Long = 0
+ End If
+ Exit Function
+' ERROR HANDLING
+errhand:
+ SizeArrayDim_Long = 0
+End Function
+
+
+Function SizeArrayDim_Variant(ByRef arr() As Variant, Optional dimension As Long = 1) As Long
+' FUNCTION
+ ' Find size of dimension of arr
+' VARIABLE INSTANTIATION
+ dimension = max_Long(1, dimension)
+' METHODS
+ On Error GoTo errhand
+ If Not ((Not arr) = -1) Then
+ SizeArrayDim_Variant = UBound(arr, dimension) - LBound(arr, dimension) + 1
+ Else
+ SizeArrayDim_Variant = 0
+ End If
+ Exit Function
+' ERROR HANDLING
+errhand:
+ SizeArrayDim_Variant = 0
+End Function
+
+
+Function SizeArrayDim_Variant_0(ByVal arr As Variant) As Long
+' FUNCTION
+ ' Find size of dimension of arr
+' METHODS
+ On Error GoTo errhand
+ If Not IsEmpty(arr) Then ' ((Not arr) = -1) Then
+ SizeArrayDim_Variant_0 = UBound(arr) - LBound(arr) + 1
+ Else
+ SizeArrayDim_Variant_0 = 0
+ End If
+ Exit Function
+' ERROR HANDLING
+errhand:
+ SizeArrayDim_Variant_0 = 0
+End Function
+
+
+Function create_1D_mat_Boolean(Optional value As Boolean = False, Optional N As Long = 1) As Boolean()
+' FUNCTION
+ ' Create 1D matrix (array) of size N, type Boolean and value
+' ARGUMENTS
+ ' Boolean value - for each element of array
+ ' Long N - number of elements in array
+' PROCESSING ACCELERATION
+' CONSTANTS
+' VARIABLE DECLARATION
+ Dim Outs() As Boolean
+ Dim i As Long
+' ARGUMENT VALIDATION
+ N = max_Long(1, N)
+' VARIABLE INSTANTIATION
+ ReDim Outs(N)
+' METHODS
+ For i = 1 To N
+ Outs(i) = value
+ Next
+' RETURNS
+ create_1D_mat_Boolean = Outs
+End Function
+
+
+Function create_1D_mat_Long(Optional value As Long = 0, Optional N As Long = 1) As Long()
+' FUNCTION
+ ' Create 1D matrix (array) of size N, type Long and value
+' ARGUMENTS
+ ' Long value - for each element of array
+ ' Long N - number of elements in array
+' PROCESSING ACCELERATION
+' CONSTANTS
+' VARIABLE DECLARATION
+ Dim Outs() As Long
+ Dim i As Long
+' ARGUMENT VALIDATION
+ N = max_Long(1, N)
+' VARIABLE INSTANTIATION
+ ReDim Outs(N)
+' METHODS
+ For i = 1 To N
+ Outs(i) = value
+ Next
+' RETURNS
+ create_1D_mat_Long = Outs
+End Function
+
+
+Function create_1D_mat_String(Optional value As String = False, Optional N As Long = 1) As String()
+' FUNCTION
+ ' Create 1D matrix (array) of size N, type String and value
+' ARGUMENTS
+ ' String value - for each element of array
+ ' Long N - number of elements in array
+' PROCESSING ACCELERATION
+' CONSTANTS
+' VARIABLE DECLARATION
+ Dim Outs() As String
+ Dim i As Long
+' ARGUMENT VALIDATION
+ N = max_Long(1, N)
+' VARIABLE INSTANTIATION
+ ReDim Outs(N)
+' METHODS
+ For i = 1 To N
+ Outs(i) = value
+ Next
+' RETURNS
+ create_1D_mat_String = Outs
+End Function
+
+
+Sub copy_N_mat_String(ByRef data_in() As String, ByRef data_out() As String, ByVal N_in As Long, ByVal N_out As Long)
+' FUNCTION
+ ' Copy from one N-dimensional matrix into another all overlapping elements
+' ARGUMENTS
+ ' String Matrix data_in - matrix to copy from
+ ' String Matrix data_out - matrix receiving data
+ ' Long N_in - number of dimensions in data_in
+ ' Long N_out - number of dimensions in N_out
+' VARIABLE DECLARATION
+ Dim dims_in() As Long
+ Dim dims_out() As Long
+ Dim i As Long
+ Dim dim_min As Long
+ Dim x() As Long
+ Dim N() As Long
+' ARGUMENT VALIDATION
+ If (N_in < 1 Or N_out <> N_in) Then Exit Sub
+' VARIABLE INSTANTIATION
+ dim_min = min_Long(N_in, N_out)
+ ReDim dims_in(dim_min)
+ ReDim dims_out(dim_min)
+ ReDim x(dim_min)
+ ReDim N(dim_min)
+ For i = 1 To dim_min
+ dims_in(i) = SizeArrayDim_String(data_in, i)
+ dims_out(i) = SizeArrayDim_String(data_out, i)
+ x(i) = 1
+ N(i) = min_Long(dims_in(i), dims_out(i))
+ Next
+' METHODS
+ Do While compare_all_iterators(x, N, dim_min)
+ set_index_N_mat_String data_out, x, dim_min, get_index_N_mat_String(data_in, x, N_out)
+ iterate_iterator x, N, dim_min
+ Loop
+End Sub
+
+
+Sub copy_N_mat_Long(ByRef data_in() As Long, ByRef data_out() As Long, ByVal N_in As Long, ByVal N_out As Long)
+' FUNCTION
+ ' Copy from one N-dimensional matrix into another all overlapping elements
+' ARGUMENTS
+ ' Long Matrix data_in - matrix to copy from
+ ' Long Matrix data_out - matrix receiving data
+ ' Long N_in - number of dimensions in data_in
+ ' Long N_out - number of dimensions in N_out
+' VARIABLE DECLARATION
+ Dim dims_in() As Long
+ Dim dims_out() As Long
+ Dim i As Long
+ Dim dim_min As Long
+ Dim x() As Long
+ Dim N() As Long
+' ARGUMENT VALIDATION
+ If (N_in < 1 Or N_out <> N_in) Then Exit Sub
+' VARIABLE INSTANTIATION
+ dim_min = min_Long(N_in, N_out)
+ ReDim dims_in(dim_min)
+ ReDim dims_out(dim_min)
+ ReDim x(dim_min)
+ ReDim N(dim_min)
+ For i = 1 To dim_min
+ dims_in(i) = SizeArrayDim_Long(data_in, i)
+ dims_out(i) = SizeArrayDim_Long(data_out, i)
+ x(i) = 1
+ N(i) = min_Long(dims_in(i), dims_out(i))
+ Next
+' METHODS
+ Do While compare_all_iterators(x, N, dim_min)
+ set_index_N_mat_Long data_out, x, dim_min, get_index_N_mat_Long(data_in, x, N_out)
+ iterate_iterator x, N, dim_min
+ Loop
+End Sub
+
+
+Sub copy_N_mat_Variant(ByRef data_in() As Variant, ByRef data_out() As Variant, ByVal N_in As Long, ByVal N_out As Long)
+' FUNCTION
+ ' Copy from one N-dimensional matrix into another all overlapping elements
+' ARGUMENTS
+ ' Variant Matrix data_in - matrix to copy from
+ ' Variant Matrix data_out - matrix receiving data
+ ' Long N_in - number of dimensions in data_in
+ ' Long N_out - number of dimensions in N_out
+' VARIABLE DECLARATION
+ Dim dims_in() As Long
+ Dim dims_out() As Long
+ Dim i As Long
+ Dim dim_min As Long
+ Dim x() As Long
+ Dim N() As Long
+' ARGUMENT VALIDATION
+ If (N_in < 1 Or N_out <> N_in) Then Exit Sub
+' VARIABLE INSTANTIATION
+ dim_min = min_Long(N_in, N_out)
+ ReDim dims_in(dim_min)
+ ReDim dims_out(dim_min)
+ ReDim x(dim_min)
+ ReDim N(dim_min)
+ For i = 1 To dim_min
+ dims_in(i) = SizeArrayDim_Variant(data_in, i)
+ dims_out(i) = SizeArrayDim_Variant(data_out, i)
+ x(i) = 1
+ N(i) = min_Long(dims_in(i), dims_out(i))
+ Next
+' METHODS
+ Do While compare_all_iterators(x, N, dim_min)
+ set_index_N_mat_Variant data_out, x, dim_min, get_index_N_mat_Variant(data_in, x, N_out)
+ iterate_iterator x, N, dim_min
+ Loop
+End Sub
+
+
+Sub iterate_iterator(ByRef x() As Long, ByRef N() As Long, ByVal NDim As Long)
+' FUNCTION
+ ' Increment iterator x under limits N
+' ARGUMENTS
+ ' Long Array x - iterator
+ ' Long Array SzDim1 - iterator limits
+ ' Long NDim
+' VARIABLE DECLARATION
+ Dim i As Long
+ Dim j As Long
+' METHODS
+ For i = NDim To 1 Step -1
+ If (x(i) < N(i)) Then
+ x(i) = x(i) + 1
+ If (i < NDim) Then
+ For j = i + 1 To NDim
+ x(j) = 1
+ Next
+ End If
+ Exit Sub
+ End If
+ Next
+' RETURNS
+ x(1) = x(1) + 1 ' what is this
+End Sub
+
+
+Function compare_all_iterators(ByRef x() As Long, ByRef N() As Long, ByVal NDim As Long) As Boolean
+' FUNCTION
+ ' Are all x(i) <= N(i)
+' ARGUMENTS
+ ' Long Array x
+ ' Long Array N
+ ' Long ndim
+' VARIABLE DECLARATION
+ Dim i As Long
+' ARGUMENT VALIDATION
+ compare_all_iterators = False
+ If (NDim < 1) Then Exit Function
+' VARIABLE INSTANTIATION
+ compare_all_iterators = True
+' METHODS
+ For i = 1 To NDim
+ If Not (x(i) <= N(i)) Then
+ compare_all_iterators = False
+ Exit Function
+ End If
+ Next
+End Function
+
+
+Function create_N_mat_String(ByVal N As Long, ByRef dims() As Long) As String()
+' FUNCTION
+ ' Create N-dimensional String-type matrix
+' ARGUMENTS
+ ' Long N - number of dimensions
+ ' Long Array dims - size of each dimension
+' ARGUMENT VALIDATION
+ If Not N <= SizeArrayDim_Long(dims) Then Exit Function
+' VARIABLE INSTANTIATION
+ Select Case N
+ Case 1
+ ReDim create_N_mat_String(max_Long(1, dims(1)))
+ Case 2
+ ReDim create_N_mat_String(max_Long(1, dims(1)), max_Long(1, dims(2)))
+ Case 3
+ ReDim create_N_mat_String(max_Long(1, dims(1)), max_Long(1, dims(2)), max_Long(1, dims(3)))
+ Case 4
+ ReDim create_N_mat_String(max_Long(1, dims(1)), max_Long(1, dims(2)), max_Long(1, dims(3)), max_Long(1, dims(4)))
+ Case 5
+ ReDim create_N_mat_String(max_Long(1, dims(1)), max_Long(1, dims(2)), max_Long(1, dims(3)), max_Long(1, dims(4)), max_Long(1, dims(5)))
+ Case Else:
+ Exit Function
+ End Select
+End Function
+
+
+Function create_N_mat_Long(ByVal N As Long, ByRef dims() As Long) As Long()
+' FUNCTION
+ ' Create N-dimensional Long-type matrix
+' ARGUMENTS
+ ' Long N - number of dimensions
+ ' Long Array dims - size of each dimension
+' ARGUMENT VALIDATION
+ If Not N <= SizeArrayDim_Long(dims) Then Exit Function
+' VARIABLE INSTANTIATION
+ Select Case N
+ Case 1
+ ReDim create_N_mat_Long(max_Long(1, dims(1)))
+ Case 2
+ ReDim create_N_mat_Long(max_Long(1, dims(1)), max_Long(1, dims(2)))
+ Case 3
+ ReDim create_N_mat_Long(max_Long(1, dims(1)), max_Long(1, dims(2)), max_Long(1, dims(3)))
+ Case 4
+ ReDim create_N_mat_Long(max_Long(1, dims(1)), max_Long(1, dims(2)), max_Long(1, dims(3)), max_Long(1, dims(4)))
+ Case 5
+ ReDim create_N_mat_Long(max_Long(1, dims(1)), max_Long(1, dims(2)), max_Long(1, dims(3)), max_Long(1, dims(4)), max_Long(1, dims(5)))
+ Case Else:
+ Exit Function
+ End Select
+End Function
+
+
+Function create_N_mat_Variant(ByVal N As Long, ByRef dims() As Long) As Variant()
+' FUNCTION
+ ' Create N-dimensional Variant-type matrix
+' ARGUMENTS
+ ' Long N - number of dimensions
+ ' Long Array dims - size of each dimension
+' ARGUMENT VALIDATION
+ If Not N <= SizeArrayDim_Long(dims) Then Exit Function
+' VARIABLE INSTANTIATION
+ Select Case N
+ Case 1
+ ReDim create_N_mat_Variant(max_Long(1, dims(1)))
+ Case 2
+ ReDim create_N_mat_Variant(max_Long(1, dims(1)), max_Long(1, dims(2)))
+ Case 3
+ ReDim create_N_mat_Variant(max_Long(1, dims(1)), max_Long(1, dims(2)), max_Long(1, dims(3)))
+ Case 4
+ ReDim create_N_mat_Variant(max_Long(1, dims(1)), max_Long(1, dims(2)), max_Long(1, dims(3)), max_Long(1, dims(4)))
+ Case 5
+ ReDim create_N_mat_Variant(max_Long(1, dims(1)), max_Long(1, dims(2)), max_Long(1, dims(3)), max_Long(1, dims(4)), max_Long(1, dims(5)))
+ Case Else:
+ Exit Function
+ End Select
+End Function
+
+
+Function get_index_N_mat_String(ByRef nd_matrix() As String, ByRef position() As Long, ByVal NDim As Long) As String
+' FUNCTION
+ ' Get value from indexed cell of N-dimensional String-type matrix
+' ARGUMENTS
+ ' String Matrix nd_matrix
+ ' Long Array position
+ ' Long ndim - number of dimensions in matrix
+' VARIABLE DECLARATION
+ Dim x As Long
+' ARGUMENT VALIDATION
+ get_index_N_mat_String = "Error"
+ If NDim < 1 Then Exit Function
+ If Not SizeArrayDim_Long(position) = NDim Then Exit Function
+ If Not SizeArrayDim_String(nd_matrix, NDim) >= 1 Then Exit Function
+ For x = 1 To NDim
+ If Not SizeArrayDim_String(nd_matrix, x) >= position(x) Then Exit Function
+ If Not position(x) >= 1 Then Exit Function
+ Next
+' METHODS
+ Select Case NDim
+ Case 1:
+ get_index_N_mat_String = nd_matrix(position(1))
+ Case 2:
+ get_index_N_mat_String = nd_matrix(position(1), position(2))
+ Case 3:
+ get_index_N_mat_String = nd_matrix(position(1), position(2), position(3))
+ Case 4:
+ get_index_N_mat_String = nd_matrix(position(1), position(2), position(3), position(4))
+ Case 5:
+ get_index_N_mat_String = nd_matrix(position(1), position(2), position(3), position(4), position(5))
+ Case Else:
+ Exit Function
+ End Select
+End Function
+
+
+Function get_index_N_mat_Long(ByRef nd_matrix() As Long, ByRef position() As Long, ByVal NDim As Long) As Long
+' FUNCTION
+ ' Get value from indexed cell of N-dimensional Long-type matrix
+' ARGUMENTS
+ ' Long Matrix nd_matrix
+ ' Long Array position
+ ' Long ndim - number of dimensions in matrix
+' VARIABLE DECLARATION
+ Dim x As Long
+' ARGUMENT VALIDATION
+ get_index_N_mat_Long = "Error"
+ If NDim < 1 Then Exit Function
+ If Not SizeArrayDim_Long(position) = NDim Then Exit Function
+ If Not SizeArrayDim_Long(nd_matrix, NDim) >= 1 Then Exit Function
+ For x = 1 To NDim
+ If Not SizeArrayDim_Long(nd_matrix, x) >= position(x) Then Exit Function
+ If Not position(x) >= 1 Then Exit Function
+ Next
+' METHODS
+ Select Case NDim
+ Case 1:
+ get_index_N_mat_Long = nd_matrix(position(1))
+ Case 2:
+ get_index_N_mat_Long = nd_matrix(position(1), position(2))
+ Case 3:
+ get_index_N_mat_Long = nd_matrix(position(1), position(2), position(3))
+ Case 4:
+ get_index_N_mat_Long = nd_matrix(position(1), position(2), position(3), position(4))
+ Case 5:
+ get_index_N_mat_Long = nd_matrix(position(1), position(2), position(3), position(4), position(5))
+ Case Else:
+ Exit Function
+ End Select
+End Function
+
+
+Function get_index_N_mat_Variant(ByRef nd_matrix() As Variant, ByRef position() As Long, ByVal NDim As Long) As Variant
+' FUNCTION
+ ' Get value from indexed cell of N-dimensional Variant-type matrix
+' ARGUMENTS
+ ' Variant Matrix nd_matrix
+ ' Long Array position
+ ' Long ndim - number of dimensions in matrix
+' VARIABLE DECLARATION
+ Dim x As Long
+' ARGUMENT VALIDATION
+ get_index_N_mat_Variant = "Error"
+ If NDim < 1 Then Exit Function
+ If Not SizeArrayDim_Long(position) = NDim Then Exit Function
+ If Not SizeArrayDim_Variant(nd_matrix, NDim) >= 1 Then Exit Function
+ For x = 1 To NDim
+ If Not SizeArrayDim_Variant(nd_matrix, x) >= position(x) Then Exit Function
+ If Not position(x) >= 1 Then Exit Function
+ Next
+' METHODS
+ Select Case NDim
+ Case 1:
+ get_index_N_mat_Variant = nd_matrix(position(1))
+ Case 2:
+ get_index_N_mat_Variant = nd_matrix(position(1), position(2))
+ Case 3:
+ get_index_N_mat_Variant = nd_matrix(position(1), position(2), position(3))
+ Case 4:
+ get_index_N_mat_Variant = nd_matrix(position(1), position(2), position(3), position(4))
+ Case 5:
+ get_index_N_mat_Variant = nd_matrix(position(1), position(2), position(3), position(4), position(5))
+ Case Else:
+ Exit Function
+ End Select
+End Function
+
+
+Sub set_index_N_mat_String(ByRef nd_matrix() As String, ByRef position() As Long, ByVal NDim As Long, ByVal vNew As String)
+' FUNCTION
+ ' Get value from indexed cell of N-dimensional String-type matrix
+' ARGUMENTS
+ ' String Matrix nd_matrix
+ ' Long Array position
+ ' Long ndim - number of dimensions in matrix
+' VARIABLE DECLARATION
+ Dim x As Long
+' ARGUMENT VALIDATION
+ ' get_index_N_mat_String = "Error"
+ If NDim < 1 Then Exit Sub
+ If Not SizeArrayDim_Long(position) = NDim Then GoTo exitsub
+ If Not SizeArrayDim_String(nd_matrix, NDim) >= 1 Then GoTo exitsub
+ For x = 1 To NDim
+ If Not SizeArrayDim_String(nd_matrix, x) >= position(x) Then GoTo exitsub
+ If Not position(x) >= 1 Then GoTo exitsub
+ Next
+' METHODS
+ Select Case NDim
+ Case 1:
+ nd_matrix(position(1)) = vNew
+ Case 2:
+ nd_matrix(position(1), position(2)) = vNew
+ Case 3:
+ nd_matrix(position(1), position(2), position(3)) = vNew
+ Case 4:
+ nd_matrix(position(1), position(2), position(3), position(4)) = vNew
+ Case 5:
+ nd_matrix(position(1), position(2), position(3), position(4), position(5)) = vNew
+ Case Else:
+ Exit Sub
+ End Select
+ Exit Sub
+exitsub:
+ MsgBox "Error"
+End Sub
+
+
+Sub set_index_N_mat_Long(ByRef nd_matrix() As Long, ByRef position() As Long, ByVal NDim As Long, ByVal vNew As Long)
+' FUNCTION
+ ' Get value from indexed cell of N-dimensional String-type matrix
+' ARGUMENTS
+ ' Long Matrix nd_matrix
+ ' Long Array position
+ ' Long ndim - number of dimensions in matrix
+' VARIABLE DECLARATION
+ Dim x As Long
+' ARGUMENT VALIDATION
+ ' get_index_N_mat_String = "Error"
+ If NDim < 1 Then Exit Sub
+ If Not SizeArrayDim_Long(position) = NDim Then GoTo exitsub
+ If Not SizeArrayDim_Long(nd_matrix, NDim) >= 1 Then GoTo exitsub
+ For x = 1 To NDim
+ If Not SizeArrayDim_Long(nd_matrix, x) >= position(x) Then GoTo exitsub
+ If Not position(x) >= 1 Then GoTo exitsub
+ Next
+' METHODS
+ Select Case NDim
+ Case 1:
+ nd_matrix(position(1)) = vNew
+ Case 2:
+ nd_matrix(position(1), position(2)) = vNew
+ Case 3:
+ nd_matrix(position(1), position(2), position(3)) = vNew
+ Case 4:
+ nd_matrix(position(1), position(2), position(3), position(4)) = vNew
+ Case 5:
+ nd_matrix(position(1), position(2), position(3), position(4), position(5)) = vNew
+ Case Else:
+ Exit Sub
+ End Select
+ Exit Sub
+exitsub:
+ MsgBox "Error"
+End Sub
+
+
+Sub set_index_N_mat_Variant(ByRef nd_matrix() As Variant, ByRef position() As Long, ByVal NDim As Long, ByVal vNew As Variant)
+' FUNCTION
+ ' Get value from indexed cell of N-dimensional String-type matrix
+' ARGUMENTS
+ ' Variant Matrix nd_matrix
+ ' Long Array position
+ ' Long ndim - number of dimensions in matrix
+' VARIABLE DECLARATION
+ Dim x As Long
+' ARGUMENT VALIDATION
+ ' get_index_N_mat_String = "Error"
+ If NDim < 1 Then GoTo exitsub
+ If Not SizeArrayDim_Long(position) = NDim Then GoTo exitsub
+ If Not SizeArrayDim_Variant(nd_matrix, NDim) >= 1 Then GoTo exitsub
+ For x = 1 To NDim
+ If Not SizeArrayDim_Variant(nd_matrix, x) >= position(x) Then GoTo exitsub
+ If Not position(x) >= 1 Then GoTo exitsub
+ Next
+' METHODS
+ Select Case NDim
+ Case 1:
+ nd_matrix(position(1)) = vNew
+ Case 2:
+ nd_matrix(position(1), position(2)) = vNew
+ Case 3:
+ nd_matrix(position(1), position(2), position(3)) = vNew
+ Case 4:
+ nd_matrix(position(1), position(2), position(3), position(4)) = vNew
+ Case 5:
+ nd_matrix(position(1), position(2), position(3), position(4), position(5)) = vNew
+ Case Else:
+ Exit Sub
+ End Select
+ Exit Sub
+exitsub:
+ MsgBox "Error"
+End Sub
+
+
+Sub convert_1D_Variant_2_String(ByRef input_array() As Variant, ByRef return_array() As String)
+' FUNCTION
+ ' Convert 1D Variant array to 1D String array
+' ARGUMENTS
+ ' Variant Array input_array
+ ' String Array return_array
+' VARIABLE DECLARATION
+ Dim N As Long
+ Dim i As Long
+' ARGUMENT VALIDATION
+ If ((Not input_array) = -1) Then
+ ReDim return_array(1)
+ return_array(1) = "Error"
+ Exit Sub
+ End If
+' VARIABLE INSTANTIATION
+ N = SizeArrayDim_Variant(input_array)
+ ReDim return_array(N)
+' METHODS
+ For i = 1 To N
+ return_array(i) = CStr(input_array(i))
+ Next
+End Sub
+
+
+Sub convert_1D_Variant_2_Long(ByRef input_array() As Variant, ByRef return_array() As Long)
+' FUNCTION
+ ' Convert 1D Variant array to 1D Long array
+' ARGUMENTS
+ ' Variant Array input_array
+ ' Long Array return_array
+' VARIABLE DECLARATION
+ Dim N As Long
+ Dim i As Long
+' ARGUMENT VALIDATION
+ If ((Not input_array) = -1) Then
+ ReDim return_array(1)
+ return_array(1) = "Error"
+ Exit Sub
+ End If
+' VARIABLE INSTANTIATION
+ N = SizeArrayDim_Variant(input_array)
+ ReDim return_array(N)
+' METHODS
+ For i = 1 To N
+ return_array(i) = CStr(input_array(i))
+ Next
+End Sub
+
+
+Sub convert_2D_Variant_2_String(ByRef input_array() As Variant, ByRef return_array() As String)
+' FUNCTION
+ ' Convert 2D matrix from Variant to String
+' ARGUMENTS
+ ' Variant Array input_array
+ ' String Array return_array
+' VARIABLE DECLARATION
+ Dim N(2) As Long
+ Dim i As Long
+ Dim j As Long
+' ARGUMENT VALIDATION
+ If ((Not input_array) = -1) Then
+ ReDim return_array(1)
+ return_array(1) = "Error"
+ Exit Sub
+ End If
+' VARIABLE INSTANTIATION
+ ' ReDim N(2)
+ N(1) = SizeArrayDim_Variant(input_array, 1)
+ N(2) = SizeArrayDim_Variant(input_array, 2)
+ ReDim return_array(N(1), N(2))
+' METHODS
+ For i = 1 To N(1)
+ For j = 1 To N(2)
+ return_array(i, j) = CStr(input_array(i, j))
+ Next
+ Next
+End Sub
+
+
+Sub convert_2D_Variant_2_Long(ByRef input_array() As Variant, ByRef return_array() As Long)
+' FUNCTION
+ ' Convert 2D matrix from Variant to Long
+' ARGUMENTS
+ ' Variant Array input_array
+ ' Long Array return_array
+' VARIABLE DECLARATION
+ Dim N() As Long
+ Dim i As Long
+ Dim j As Long
+' ARGUMENT VALIDATION
+ If ((Not input_array) = -1) Then
+ ReDim return_array(1)
+ return_array(1) = "Error"
+ Exit Sub
+ End If
+' VARIABLE INSTANTIATION
+ ReDim N(2)
+ N(1) = SizeArrayDim_Variant(input_array, 1)
+ N(2) = SizeArrayDim_Variant(input_array, 2)
+ ReDim return_array(N(1), N(2))
+' METHODS
+ For i = 1 To N(1)
+ For j = 1 To N(2)
+ return_array(i, j) = CLng(input_array(i, j))
+ Next
+ Next
+End Sub
+
+
+Function last_filled_cell(ByVal array1D As Variant, Optional start As Long = 1, Optional max_i As Long = -1, Optional gap_max As Long = 1, Optional dir_move As dir_traverse = dir_traverse.FORWARDS) As Long
+' FUNCTION
+ ' Find last populated cell in 1D array from start to max_i with maximum number of consecutive empty cells gap_max
+' ARGUMENTS
+ ' Variant Array array1D
+ ' Long start
+ ' Long max_i
+ ' Long gap_max
+ ' dir_traverse dir_move
+' VARIABLE DECLARATION
+ Dim found_last As Boolean
+ Dim temp As Variant
+ Dim gap_count As Long
+ Dim i As Long
+' ARGUMENT VALIDATION
+ If max_i < 1 Then
+ max_i = SizeArrayDim_Variant_0(array1D)
+ End If
+ If (start < 0 Or start > max_i) Then Exit Function
+' VARIABLE INSTANTIATION
+ If dir_move = FORWARDS Then
+ i = start - 1
+ Else
+ i = max_i
+ max_i = start
+ start = i
+ i = max_i + 1
+ End If
+' METHODS
+ Do While Not found_last
+ i = i + 1
+ If i = max_i Then
+ found_last = True
+ ElseIf IsEmpty(array1D(i)) Or array1D(i) = "" Then
+ gap_count = gap_count + 1
+ Else
+ gap_count = 0
+ End If
+ If gap_count > gap_max Then
+ found_last = True
+ End If
+ Loop
+' RETURNS
+ last_filled_cell = i - gap_count
+End Function
+
+
+Function max_n_Long(ByRef array1D() As Long, Optional N As Long = 0, Optional start As Long = 1, Optional dir_move As dir_traverse = dir_traverse.FORWARDS) As Long
+' FUNCTION
+ ' Maximum Long value in array1D from start up to start + N - 1
+' ARGUMENTS
+ ' Long Array array1D
+ ' Long N - number of elements to check
+ ' Long start - first element to check
+' VARIABLE DECLARATION
+ Dim i As Long
+ Dim max_i As Long
+ Dim grand_max As Long
+' ARGUMENT VALIDATION
+ start = max_Long(1, start)
+ max_i = SizeArrayDim_Long(array1D)
+ If N > 0 Then
+ If start + N - 1 > max_i Then
+ MsgBox "Invalid search indices for array of size " & CStr(max_i)
+ End If
+ End If
+' METHODS
+ If dir_move = FORWARDS Then
+ For i = start To max_i
+ grand_max = max_Long(grand_max, array1D(i))
+ Next
+ Else
+ For i = start To 1 Step -1
+ grand_max = max_Long(grand_max, array1D(i))
+ Next
+ End If
+' RETURNS
+ max_n_Long = grand_max
+End Function
+
+
+Function min_n_Long(ByRef array1D() As Long, Optional N As Long = 0, Optional start As Long = 1, Optional dir_move As dir_traverse = dir_traverse.FORWARDS) As Long
+' FUNCTION
+ ' Minimum Long value in array1D from start up to start + N - 1
+' ARGUMENTS
+ ' Long Array array1D
+ ' Long N - number of elements to check
+ ' Long start - first element to check
+' VARIABLE DECLARATION
+ Dim i As Long
+ Dim max_i As Long
+ Dim grand_min As Long
+' ARGUMENT VALIDATION
+ start = max_Long(1, start)
+ max_i = SizeArrayDim_Long(array1D)
+ If N > 0 Then
+ If start + N - 1 > max_i Then
+ MsgBox "Invalid search indices for array of size " & CStr(max_i)
+ End If
+ End If
+' METHODS
+ If dir_move = FORWARDS Then
+ For i = start To max_i
+ grand_min = min_Long(grand_min, array1D(i))
+ Next
+ Else
+ For i = start To 1 Step -1
+ grand_min = min_Long(grand_min, array1D(i))
+ Next
+ End If
+' RETURNS
+ min_n_Long = grand_min
+End Function
+
+
+Function match_Long(ByVal key As Long, ByRef array1D() As Long, Optional start As Long = 1, Optional max_i As Long = 0, Optional dir_move As dir_traverse = dir_traverse.FORWARDS) As Long
+' FUNCTION
+ ' Find first or last instance of key in array1D
+' ARGUMENTS
+ ' Long key
+ ' Long Array array1D
+ ' Long start
+ ' Long max_i
+ ' dir_traverse dir_move
+' VARIABLE DECLARATION
+ Dim i As Long
+' ARGUMENT VALIDATION
+ If max_i < 1 Then
+ max_i = SizeArrayDim_Long(array1D)
+ End If
+ If (start < 0 Or start > max_i) Then Exit Function
+' METHODS
+ If dir_move = FORWARDS Then
+ For i = start To max_i
+ If array1D(i) = key Then
+ match_Long = i
+ Exit Function
+ End If
+ Next
+ Else
+ For i = start To 1 Step -1
+ If array1D(i) = key Then
+ match_Long = i
+ Exit Function
+ End If
+ Next
+ End If
+End Function
+
+
+Function match_String(ByVal key As String, ByRef array1D() As String, Optional start As Long = 1, Optional max_i As Long = 0, Optional dir_move As dir_traverse = dir_traverse.FORWARDS) As Long
+' FUNCTION
+ ' Find first or last instance of key in array1D
+' ARGUMENTS
+ ' String key
+ ' String Array array1D
+ ' Long start
+ ' Long max_i
+ ' dir_traverse dir_move
+' VARIABLE DECLARATION
+ Dim i As Long
+' ARGUMENT VALIDATION
+ If max_i < 1 Then
+ max_i = SizeArrayDim_String(array1D)
+ End If
+ If (start < 0 Or start > max_i) Then Exit Function
+' METHODS
+ If dir_move = FORWARDS Then
+ For i = start To max_i
+ If array1D(i) = key Then
+ match_String = i
+ Exit Function
+ End If
+ Next
+ Else
+ For i = start To 1 Step -1
+ If array1D(i) = key Then
+ match_String = i
+ Exit Function
+ End If
+ Next
+ End If
+End Function
+
+
+Function match_Variant(ByVal key As Variant, ByRef array1D() As Variant, Optional start As Long = 1, Optional max_i As Long = 0, Optional dir_move As dir_traverse = dir_traverse.FORWARDS) As Long
+' FUNCTION
+ ' Find first or last instance of key in array1D
+' ARGUMENTS
+ ' Variant key
+ ' Variant Array array1D
+ ' Long start
+ ' Long max_i
+ ' dir_traverse dir_move
+' VARIABLE DECLARATION
+ Dim i As Long
+' ARGUMENT VALIDATION
+ If max_i < 1 Then
+ max_i = SizeArrayDim_Variant(array1D, 2)
+ End If
+ If (start < 0 Or start > max_i) Then Exit Function
+' METHODS
+ If dir_move = FORWARDS Then
+ For i = start To max_i
+ If array1D(i) = key Then
+ match_Variant = i
+ Exit Function
+ End If
+ Next
+ Else
+ For i = start To 1 Step -1
+ If array1D(i) = key Then
+ match_Variant = i
+ Exit Function
+ End If
+ Next
+ End If
+End Function
+
+
+Function change_array_base_String(ByRef array1D() As String, Optional base_new As Long = 1) As String()
+' FUNCTION
+ ' Change base of array1D to new_base, if necessary
+' ARGUMENTS
+ ' String Array array1D
+ ' Long base_new
+' VARIABLE DECLARATION
+ Dim base_old As Long
+ Dim i As Long
+ Dim N As Long
+ Dim array_out() As String
+' ARGUMENT VALIDATION
+ N = SizeArrayDim_String(array1D)
+ If N < 1 Then Exit Function
+' VARIABLE INSTANTIATION
+ base_old = LBound(array1D)
+ ReDim array_out(N)
+' METHODS
+ If base_old = base_new Then
+ change_array_base_String = array1D
+ Exit Function
+ End If
+ For i = 1 To N
+ array_out(base_new + i - 1) = array1D(base_old + i - 1)
+ Next
+' RETURNS
+ change_array_base_String = array_out
+End Function
+
+
+Function get_1D_strip_N_mat_String(ByRef N_mat() As String, ByVal dimension As Long, ByRef position() As Long) As String()
+' FUNCTION
+ ' Get 1D array of String-type N-dimensional matrix with all but one dimension fixed
+' ARGUMENTS
+ ' String Array N_mat
+ ' Long dimension - dimension of N-mat to vary
+ ' Long Array position - fixed coordinates
+' VARIABLE DECLARATION
+ Dim Outs() As String
+ Dim x() As Long
+ Dim i As Long
+ Dim N As Long
+ Dim x_max As Long
+ Dim p_max As Long
+' ARGUMENT VALIDATION
+ If dimension < 1 Then
+ MsgBox "Error: Dimension out of range."
+ Exit Function
+ End If
+ p_max = SizeArrayDim_Long(position)
+ If p_max = 0 Then
+ MsgBox "Error: Position is nothing."
+ Exit Function
+ End If
+ If (p_max < dimension - 1) Then
+ MsgBox "Error: Insufficient quantity of dimensions in position for dimension " & CStr(dimension) & "."
+ Exit Function
+ End If
+ x_max = SizeArrayDim_String(N_mat, N)
+ If x_max = 0 Then
+ MsgBox "Error: N-dimensional Matrix of insufficient quantity of dimensions."
+ Exit Function
+ End If
+ x_max = SizeArrayDim_String(N_mat, N + 1)
+ If x_max > 0 Then
+ MsgBox "Error: N-dimensional Matrix of too many dimensions."
+ Exit Function
+ End If
+' VARIABLE INSTANTIATION
+ x_max = SizeArrayDim_String(N_mat, dimension)
+ ReDim Outs(x_max)
+ ReDim x(N)
+ For i = 1 To p_max
+ x(i) = position(i)
+ Next
+' METHODS
+ For i = 1 To x_max
+ x(dimension) = i
+ Outs(i) = get_index_N_mat_String(N_mat, x, N)
+ Next
+' RETURNS
+ get_1D_strip_N_mat_String = Outs
+End Function
+
+
+Function get_1D_strip_N_mat_Long(ByRef N_mat() As Long, ByVal dimension As Long, ByRef position() As Long) As Long()
+' FUNCTION
+ ' Get 1D array of Long-type N-dimensional matrix with all but one dimension fixed
+' ARGUMENTS
+ ' String Array N_mat
+ ' Long dimension - dimension of N-mat to vary
+ ' Long Array position - fixed coordinates
+' VARIABLE DECLARATION
+ Dim Outs() As Long
+ Dim x() As Long
+ Dim i As Long
+ Dim N As Long
+ Dim x_max As Long
+ Dim p_max As Long
+' ARGUMENT VALIDATION
+ If dimension < 1 Then
+ MsgBox "Error: Dimension out of range."
+ Exit Function
+ End If
+ p_max = SizeArrayDim_Long(position)
+ If p_max = 0 Then
+ MsgBox "Error: Position is nothing."
+ Exit Function
+ End If
+ If (p_max < dimension - 1) Then
+ MsgBox "Error: Insufficient quantity of dimensions in position for dimension " & CStr(dimension) & "."
+ Exit Function
+ End If
+ N = max_Long(dimension, p_max)
+ x_max = SizeArrayDim_Long(N_mat, N)
+ If x_max = 0 Then
+ MsgBox "Error: N-dimensional Matrix of insufficient quantity of dimensions."
+ Exit Function
+ End If
+ x_max = SizeArrayDim_Long(N_mat, N + 1)
+ If x_max > 0 Then
+ MsgBox "Error: N-dimensional Matrix of too many dimensions."
+ Exit Function
+ End If
+' VARIABLE INSTANTIATION
+ x_max = SizeArrayDim_Long(N_mat, dimension)
+ ReDim Outs(x_max)
+ ReDim x(N)
+ For i = 1 To p_max
+ x(i) = position(i)
+ Next
+' METHODS
+ For i = 1 To x_max
+ x(dimension) = i
+ Outs(i) = get_index_N_mat_Long(N_mat, x, N)
+ Next
+' RETURNS
+ get_1D_strip_N_mat_Long = Outs
+End Function
+
+
+Function get_1D_strip_N_mat_Variant(ByRef N_mat() As Variant, ByVal dimension As Long, ByRef position() As Long) As Variant()
+' FUNCTION
+ ' Get 1D array of Variant-type N-dimensional matrix with all but one dimension fixed
+' ARGUMENTS
+ ' String Array N_mat
+ ' Long dimension - dimension of N-mat to vary
+ ' Long Array position - fixed coordinates
+' VARIABLE DECLARATION
+ Dim Outs() As Variant
+ Dim x() As Long
+ Dim i As Long
+ Dim N As Long
+ Dim x_max As Long
+ Dim p_max As Long
+' ARGUMENT VALIDATION
+ If dimension < 1 Then
+ MsgBox "Error: Dimension out of range."
+ Exit Function
+ End If
+ p_max = SizeArrayDim_Long(position)
+ If p_max = 0 Then
+ MsgBox "Error: Position is nothing."
+ Exit Function
+ End If
+ If (p_max < dimension - 1) Then
+ MsgBox "Error: Insufficient quantity of dimensions in position for dimension " & CStr(dimension) & "."
+ Exit Function
+ End If
+ x_max = SizeArrayDim_Variant(N_mat, N)
+ If x_max = 0 Then
+ MsgBox "Error: N-dimensional Matrix of insufficient quantity of dimensions."
+ Exit Function
+ End If
+ x_max = SizeArrayDim_Variant(N_mat, N + 1)
+ If x_max > 0 Then
+ MsgBox "Error: N-dimensional Matrix of too many dimensions."
+ Exit Function
+ End If
+' VARIABLE INSTANTIATION
+ x_max = SizeArrayDim_Variant(N_mat, dimension)
+ ReDim Outs(x_max)
+ ReDim x(N)
+ For i = 1 To p_max
+ x(i) = position(i)
+ Next
+' METHODS
+ For i = 1 To x_max
+ x(dimension) = i
+ Outs(i) = get_index_N_mat_Variant(N_mat, x, N)
+ Next
+' RETURNS
+ get_1D_strip_N_mat_Variant = Outs
+End Function
+
+
+Function Mod_Double(ByVal numerator As Double, ByVal denominator As Double) As Double
+' FUNCTION
+ ' Modulo method for doubles
+' ARGUMENTS
+ ' Double numerator
+ ' Double denominator
+' METHODS
+ Do While numerator > denominator
+ numerator = numerator - denominator
+ Loop
+ Do While numerator < 0
+ numerator = numerator + denominator
+ Loop
+' RETURNS
+ Mod_Double = numerator
+End Function
+
+
+Function Div_Double(ByVal numerator As Double, ByVal denominator As Double) As Long
+' FUNCTION
+ ' Modular division method for doubles
+' ARGUMENTS
+ ' Double numerator
+ ' Double denominator
+' VARIABLE DECLARATION
+ Dim count As Long
+' VARIABLE DECLARATION
+ count = 0
+' METHODS
+ Do While numerator > denominator
+ count = count + 1
+ numerator = numerator - denominator
+ Loop
+ Do While numerator < 0
+ count = count - 1
+ numerator = numerator + denominator
+ Loop
+' RETURNS
+ Div_Double = count
+End Function
+
diff --git a/src/Range_Strings.bas b/src/Range_Strings.bas
new file mode 100644
index 0000000..7e8c2e4
--- /dev/null
+++ b/src/Range_Strings.bas
@@ -0,0 +1,70 @@
+' Author: Edward Middleton-Smith
+' Precision And Research Technology Systems Limited
+
+
+' MODULE INITIALISATION
+' Set array start index to 1 to match spreadsheet indices
+Option Base 1
+' Forced Variable Declaration
+Option Explicit
+
+
+Function Range_1D_String(ByRef ws As Worksheet, ByVal range_str As String) As String()
+' FUNCTION
+ ' Get range of worksheet as 1D String array
+' ARGUMENTS
+ ' Worksheet ws
+ ' String range_str
+' VARIABLE DECLARATION
+ Dim my_range() As Variant
+ Dim sz_x As Long
+ Dim sz_y As Long
+ Dim i As Long
+ Dim N As Long
+ Dim strs() As String
+ Dim out_err() As String
+' ARGUMENT VALIDATION
+ ReDim out_err(1)
+ out_err(1) = "Error"
+ If Not valid_range_String(range_str) Or ws Is Nothing Then
+ Range_1D_String = out_err
+ Exit Function
+ End If
+' VARIABLE INSTANTIATION
+ my_range = ws.Range(range_str).value
+ sz_x = SizeArrayDim_Variant(my_range, 2)
+ sz_y = SizeArrayDim_Variant(my_range, 1)
+ If (sz_x = 0 Or sz_y = 0 Or (sz_x > 1 And sz_y > 1)) Then
+ Range_1D_String = out_err
+ Exit Function
+ End If
+ N = max_Long(sz_x, sz_y)
+ ReDim strs(N)
+' METHODS
+ For i = 1 To N
+ If sz_x > sz_y Then
+ strs(i) = my_range(1, i)
+ Else
+ strs(i) = my_range(i, 1)
+ End If
+ Next
+' RETURNS
+ Range_1D_String = strs
+End Function
+
+Function Range_String(ByVal col_min As Long, ByVal col_max As Long, ByVal row_min As Long, ByVal row_max As Long) As String
+' FUNCTION
+ ' Create range string from minimum and maximum positions
+' ARGUMENTS
+ ' Long col_min
+ ' Long col_max
+ ' Long row_min
+ ' Long row_max
+' ARGUMENT VALIDATION
+ Range_String = "Error: Invalid coordinates"
+ If row_max < 1 Then row_max = 1048576
+ If col_max < 1 Then col_max = 16384
+ If Not (valid_coordinate(row_min, col_min) And valid_coordinate(row_max, col_max)) Then Exit Function
+' RETURNS
+ Range_String = get_col_str(col_min) & CStr(row_min) & ":" & get_col_str(col_max) & CStr(row_max)
+End Function
diff --git a/src/String_Operations.bas b/src/String_Operations.bas
new file mode 100644
index 0000000..68e38fc
--- /dev/null
+++ b/src/String_Operations.bas
@@ -0,0 +1,175 @@
+' Author: Edward Middleton-Smith
+' Precision And Research Technology Systems Limited
+
+
+' MODULE INITIALISATION
+' Set array start index to 1 to match spreadsheet indices
+Option Base 1
+' Forced Variable Declaration
+Option Explicit
+
+
+Function is_numeric(ByVal numeric_chars As String) As Boolean
+' FUNCTION
+ ' Evaluate if all characters in numeric_chars are numeric
+' ARGUMENTS
+ ' String numeric_chars
+' VARIABLE DECLARATION
+ Dim i As Long
+ Dim asci_i As Long
+ Dim valid As Boolean
+' ARGUMENT VALIDATION
+ If numeric_chars = "" Then Exit Function
+' VARIABLE INSTANTIATION
+ valid = True
+' METHODS
+ For i = 1 To Len(numeric_chars)
+ asci_i = Asc(Mid(numeric_chars, i, 1))
+ valid = valid And (asci_i >= 48 And asci_i <= 57)
+ Next
+' RETURNS
+ is_numeric = valid
+End Function
+
+Function is_alphabetic(ByVal alphabetic_chars As String) As Boolean
+' FUNCTION
+ ' Evaluate if all characters in alphabetic_chars are alpabetic
+' ARGUMENTS
+ ' String numeric_chars
+' VARIABLE DECLARATION
+ Dim i As Long
+ Dim asci_i As Long
+ Dim valid As Boolean
+' ARGUMENT VALIDATION
+ If alphabetic_chars = "" Then Exit Function
+' VARIABLE INSTANTIATION
+ valid = True
+' METHODS
+ For i = 1 To Len(alphabetic_chars)
+ asci_i = Asc(Mid(alphabetic_chars, i, 1))
+ valid = valid And ((asci_i >= 65 And asci_i <= 90) Or (asci_i >= 97 And asci_i <= 122))
+ Next
+' RETURNS
+ is_alphabetic = valid
+End Function
+
+Function get_col_str(ByVal col_ID As Long) As String
+' FUNCTION
+ ' Return column ID as String from Long
+' ARGUMENTS
+ ' Long col_ID
+' VARIABLE DECLARATION
+ Dim strID As String
+ Dim i As Double
+ Dim N As Double
+ Dim temp As Long
+ Dim remainder As Double
+' ARGUMENT VALIDATION
+ If col_ID < 1 Then Exit Function
+' VARIABLE INSTANTIATION
+ N = max_Double(0, Div_Double(Log(col_ID), Log(26)))
+ If Not Mod_Double(Log(col_ID), Log(26)) = 0# Or col_ID = 1 Then N = N + 1
+' METHODS
+ For i = 1 To N
+ temp = ((col_ID - 1) Mod (26 ^ i)) \ (26 ^ (i - 1)) + 1
+ strID = Chr(64 + temp) & strID
+ col_ID = col_ID - temp * (26 ^ (i - 1))
+ Next
+' RETURNS
+ get_col_str = strID
+End Function
+
+Function get_col_ID(ByVal col_str As String) As Long
+' FUNCTION
+ ' Return column ID as Long from String
+' ARGUMENTS
+ ' String col_ID
+' VARIABLE DECLARATION
+ Dim col_ID As Long
+ Dim i As Long
+ Dim N As Long
+ Dim temp As Long
+ Dim remainder As Double
+' ARGUMENT VALIDATION
+ If Not is_alphabetic(col_str) Then Exit Function
+' VARIABLE INSTANTIATION
+ N = Len(col_str)
+' METHODS
+ For i = 1 To N
+ col_ID = col_ID + (Asc(Mid(col_str, i, 1)) - 64) * 26 ^ (N - i)
+ Next
+' RETURNS
+ get_col_ID = col_ID
+End Function
+
+Function Range_String_Coords(ByVal range_str As String) As Long()
+' FUNCTION
+ ' Array of coordinates in range_str (row_1, column_1, [row_2, column_2])
+' ARGUMENTS
+ ' String range_str
+' VARIABLE DECLARATION
+ Dim coords() As Long
+ Dim Phrases() As String
+ Dim Tmps() As String
+ Dim i As Long
+ Dim N As Long
+' ARGUMENT VALIDATION
+ If Not valid_range_String(range_str) Then Exit Function
+' VARIABLE INSTANTIATION
+ Tmps = Split(range_str, ":")
+ Phrases = change_array_base_String(Tmps)
+ N = SizeArrayDim_String(Phrases)
+ ReDim coords(N)
+' METHODS
+ For i = 1 To N
+ If i Mod 2 = 0 Then
+ coords(i) = get_col_ID(Phrases(i))
+ Else
+ coords(i) = CLng(Phrases(i))
+ End If
+ Next
+End Function
+
+Function CMoney_String(ByVal money_D As Double) As String
+' FUNCTION
+ ' Get string equivalent of double to 2 decimal places
+' ARGUMENTS
+ ' Double money_D
+' VARIABLE DECLARATION
+ ' Dim money_L As Long
+ Dim money_S As String
+ Dim iDot As Long
+' VARIABLE INSTANTIATION
+ ' money_D = 100 * money_D
+ ' money_L = money_D
+ ' money_D = money_L / 100
+ money_S = CStr(Round(money_D, 2))
+ iDot = InStr(1, money_S, ".")
+' RETURNS
+ If iDot < 1 Then
+ CMoney_String = "'" & money_S & ".00"
+ ElseIf iDot = Len(money_S) - 1 Then
+ CMoney_String = "'" & money_S & "0"
+ Else
+ CMoney_String = "'" & money_S
+ End If
+End Function
+
+Function Path2Name(ByVal FilePath As String) As String
+' FUNCTION
+ ' Get file name from path
+' ARGUMENTS
+ ' String FilePath
+' VARIABLE DECLARATION
+ Dim iSlash As Long
+' VARIABLE INSTANTIATION
+ Path2Name = FilePath
+ iSlash = InStr(1, Path2Name, "/")
+ If iSlash < 1 Then iSlash = InStr(1, Path2Name, "/")
+' METHODS
+ Do While iSlash > 0
+ Path2Name = Mid(Path2Name, iSlash + 1)
+ iSlash = InStr(1, Path2Name, "/")
+ If iSlash < 1 Then iSlash = InStr(1, Path2Name, "/")
+ Loop
+End Function
diff --git a/src/Validation_Comparison.bas b/src/Validation_Comparison.bas
new file mode 100644
index 0000000..fcf0f0a
--- /dev/null
+++ b/src/Validation_Comparison.bas
@@ -0,0 +1,204 @@
+' Author: Edward Middleton-Smith
+' Precision And Research Technology Systems Limited
+
+
+' MODULE INITIALISATION
+' Set array start index to 1 to match spreadsheet indices
+Option Base 1
+' Forced Variable Declaration
+Option Explicit
+
+
+Function input_Bool(ByVal bool_str As String) As Boolean
+' FUNCTION
+ ' Evaluate if String is equivalent to Boolean True
+' ARGUMENTS
+ ' String bool_str
+' VARIABLE DECLARATION
+ Dim L As Long
+' VARIABLE INSTANTIATION
+ bool_str = UCase(bool_str)
+ L = Len(bool_str)
+ input_Bool = False
+' METHODS
+ Select Case bool_str
+ Case "YES", "Y", "YEAH", "YESH", "YEESH", "YEES", "TRUE", "T":
+ input_Bool = True
+ Case Else:
+ If is_numeric(bool_str) Then
+ input_Bool = True
+ ElseIf L > 1 Then
+ If Left(bool_str, 1) = "_" And is_numeric(Mid(bool_str, 2)) Then
+ input_Bool = True
+ End If
+ End If
+ End Select
+End Function
+
+
+Function input_Long(ByVal long_str As String, Optional Default As Long = 0) As Long
+' FUNCTION
+ ' Evaluate if String is equivalent to Long
+' ARGUMENTS
+ ' String long_str
+ ' Long default
+' METHODS
+ If is_numeric(long_str) Then
+ input_Long = CLng(long_str)
+ Else
+ input_Long = Default
+ End If
+End Function
+
+
+Function max_Long(ByRef a As Long, ByVal b As Long) As Long
+' FUNCTION
+ ' Maximum of two Long values
+' ARGUMENTS
+ ' Long a
+ ' Long b
+' METHODS
+ If a < b Then
+ max_Long = b
+ Else
+ max_Long = a
+ End If
+End Function
+
+
+Function min_Long(ByRef a As Long, ByVal b As Long) As Long
+' FUNCTION
+ ' Maximum of two Long values
+' ARGUMENTS
+ ' Long a
+ ' Long b
+' METHODS
+ min_Long = -max_Long(-a, -b)
+End Function
+
+
+Function max_Double(ByRef a As Double, ByVal b As Double) As Double
+' FUNCTION
+ ' Maximum of two Double values
+' ARGUMENTS
+ ' Double a
+ ' Double b
+' METHODS
+ If a < b Then
+ max_Double = b
+ Else
+ max_Double = a
+ End If
+End Function
+
+
+Function min_Double(ByRef a As Double, ByVal b As Double) As Double
+' FUNCTION
+ ' Maximum of two Double values
+' ARGUMENTS
+ ' Double a
+ ' Double b
+' METHODS
+ min_Double = -max_Double(-a, -b)
+End Function
+
+
+Function valid_coordinate(ByRef row As Long, ByVal col As Long) As Boolean
+' FUNCTION
+ ' Is coordinate on Worksheet?
+' ARGUMENTS
+ ' Long a
+ ' Long b
+' RETURNS
+ valid_coordinate = Not ((row < 1) Or (row > 1048576) Or (col < 1) Or (col > 16384))
+End Function
+
+
+Function valid_range_String(ByVal range_str As String) As Boolean
+' FUNCTION
+ ' Validate format of range string, e.g. "ABC123:BUD420"
+' ARGUMENTS
+ ' String range_str
+' VARIABLE DECLARATION
+ Dim Temps() As String
+ Dim Phrases() As String
+ Dim i As Long
+ Dim N As Long
+' VARIABLE INSTANTIATION
+ valid_range_String = False
+ Temps = Split(range_str, ":")
+ Phrases = change_array_base_String(Temps)
+ N = SizeArrayDim_String(Phrases)
+' METHODS
+ If N <> 2 Then
+ If (N = 1) Then
+ valid_range_String = valid_range_String_segment(range_str)
+ End If
+ Exit Function
+ End If
+ valid_range_String = True
+ For i = 1 To 2
+ valid_range_String = valid_range_String And valid_range_String_segment(Phrases(i))
+ If Not valid_range_String Then Exit Function
+ Next
+End Function
+
+
+Function valid_range_String_segment(ByVal range_str_segment As String) As Boolean
+' FUNCTION
+ ' Validate format of segment of range string, e.g. "ABC123"
+' ARGUMENTS
+ ' String range_str_segment
+' VARIABLE DECLARATION
+ Dim i As Long
+ Dim N As Long
+ Dim end_of_lets As Boolean
+ Dim temp As String
+' VARIABLE INSTANTIATION
+ N = Len(range_str_segment)
+ end_of_lets = False
+ valid_range_String_segment = False
+' METHODS
+ If N < 2 Then Exit Function
+ For i = 1 To N
+ temp = Mid(range_str_segment, i, 1)
+ If end_of_lets Then
+ If Not is_numeric(temp) Then Exit Function
+ ElseIf Not is_alphabetic(temp) Then
+ If is_numeric(temp) Then
+ end_of_lets = True
+ Else
+ Exit Function
+ End If
+ End If
+ Next
+' RETURNS
+ valid_range_String_segment = end_of_lets
+End Function
+
+
+Function error_msg(ByVal v As Variant, ByVal name As String, ByVal v_type As String, Optional v_expected As Variant = Nothing) As String
+' FUNCTION
+ ' Error message string for invalid argument
+' ARGUMENTS
+ ' Variant v - erroneous argument
+ ' String name - name of argument
+ ' String v_type - argument data type
+ ' Variant v_expected - expected value
+' VARIABLE DECLARATION
+ Dim str_v As String
+ Dim str_exp As String
+' VARIABLE INSTANTIATION
+ If v Is Nothing Then
+ str_v = "Nothing"
+ Else
+ str_v = CStr(v)
+ End If
+ If v_expected Is Nothing Then
+ str_exp = "Nothing"
+ Else
+ str_exp = CStr(v_expected)
+ End If
+' RETURNS
+ error_msg = "Invalid " & v_type & " " & name & "." & vbCrLf & "Value = " & str_v & vbCrLf & "Expected value = " & str_exp
+End Function
diff --git a/src/WS_Access.cls b/src/WS_Access.cls
new file mode 100644
index 0000000..26d8867
--- /dev/null
+++ b/src/WS_Access.cls
@@ -0,0 +1,853 @@
+' Author: Edward Middleton-Smith
+' Precision And Research Technology Systems Limited
+
+
+' MODULE INITIALISATION
+' Set array start index to 1 to match spreadsheet indices
+Option Base 1
+' Forced Variable Declaration
+Option Explicit
+
+
+' PROPERTY DECLARATION
+Private v_ws As Worksheet
+Private v_name As String
+Private v_dir As orientation
+Private v_col_min As Long
+Private v_col_max As Long
+Private v_col_search As Long
+Private v_row_min As Long
+Private v_row_max As Long
+Private v_row_search As Long
+Private v_data() As String
+Private v_headings() As String
+Private v_col_IDs() As Long
+Private v_mutable_headings As Boolean
+Private v_gap_max As Long
+
+
+' PROPERTY METHODS
+Public Property Get Worksheet() As Worksheet
+ Set Worksheet = v_ws
+End Property
+
+Public Property Get name() As String
+ name = v_name
+End Property
+Public Property Let name(ByVal new_name As String)
+ v_name = new_name
+End Property
+
+Public Property Get Cell(ByVal row As Long, ByVal col As Long) As Range
+ Set Cell = v_ws.Cells(row, col)
+End Property
+
+Public Property Get Range(ByVal range_str As String) As Range
+ Set Range = v_ws.Range(range_str)
+End Property
+Public Sub SetRange_Variant(ByVal range_str As String, ByRef new_data() As Variant)
+' FUNCTION
+ ' Set Range of v_ws
+' ARGUMENTS
+ ' String range_str
+ ' Variant Matrix new_data
+' VARIABLE DECLARATION
+ Dim sz_x As Long
+ Dim sz_y As Long
+ Dim sz_z As Long
+ Dim out_data() As String
+ Dim range_coords() As Long
+' VARIABLE INSTANTIATION
+ sz_x = SizeArrayDim_Variant(new_data, 2)
+ sz_y = SizeArrayDim_Variant(new_data, 1)
+ sz_z = SizeArrayDim_Variant(new_data, 3)
+' ARGUMENT VALIDATION
+ ' string-literal decomposition of range string
+ If Not valid_range_String(range_str) Then
+ MsgBox "Invalid range string"
+ Exit Sub
+ End If
+ range_coords = Range_String_Coords(range_str)
+ ' Validate new coordinates
+ If Not (valid_coordinate(range_coords(1), range_coords(2)) And valid_coordinate(range_coords(3), range_coords(4))) Then
+ MsgBox "Invalid range string coordinates"
+ Exit Sub
+ End If
+ ' Validate new_data dimensions against range_str dimensions
+ If Not ((sz_x = range_coords(4) - range_coords(2) + 1) And (sz_x = range_coords(4) - range_coords(2) + 1) And (sz_z = 0)) Then
+ MsgBox "Inconsistent range and matrix dimensions"
+ Exit Sub
+ End If
+' METHODS
+ ' Compare new dimensions against current dimensions
+ If Not (range_coords(1) = v_row_min And range_coords(2) = v_col_min And range_coords(3) = v_row_max And range_coords(4) = v_col_max) Then
+ ResizeLocal range_coords(2), range_coords(4), range_coords(1), range_coords(3), True
+ End If
+' RETURNS
+ v_ws.Range(range_str).value = new_data
+ convert_2D_Variant_2_String new_data, out_data
+ Me.SetData out_data
+End Sub
+Public Sub SetRange_String(ByVal range_str As String, ByRef new_data() As String)
+' FUNCTION
+ ' Set Range of v_ws
+' ARGUMENTS
+ ' String range_str
+ ' String Matrix new_data
+' VARIABLE DECLARATION
+ Dim sz_x As Long
+ Dim sz_y As Long
+ Dim sz_z As Long
+ Dim range_coords() As Long
+ Dim i As Long
+ Dim j As Long
+' VARIABLE INSTANTIATION
+ sz_x = SizeArrayDim_String(new_data, 2)
+ sz_y = SizeArrayDim_String(new_data, 1)
+ sz_z = SizeArrayDim_String(new_data, 3)
+' ARGUMENT VALIDATION
+ ' string-literal decomposition of range string
+ If Not valid_range_String(range_str) Then
+ MsgBox "Invalid range string"
+ Exit Sub
+ End If
+ range_coords = Range_String_Coords(range_str)
+ ' Validate new coordinates
+ If Not (valid_coordinate(range_coords(1), range_coords(2)) And valid_coordinate(range_coords(3), range_coords(4))) Then
+ MsgBox "Invalid range string coordinates"
+ Exit Sub
+ End If
+ ' Validate new_data dimensions against range_str dimensions
+ If Not ((sz_x = range_coords(4) - range_coords(2) + 1) And (sz_x = range_coords(4) - range_coords(2) + 1) And (sz_z = 0)) Then
+ MsgBox "Inconsistent range and matrix dimensions"
+ Exit Sub
+ End If
+' METHODS
+ ' Compare new dimensions against current dimensions
+ If Not (range_coords(1) = v_row_min And range_coords(2) = v_col_min And range_coords(3) = v_row_max And range_coords(4) = v_col_max) Then
+ ResizeLocal range_coords(2), range_coords(4), range_coords(1), range_coords(3), True
+ End If
+' RETURNS
+ For i = v_col_min To v_col_max
+ For j = v_row_min To v_row_max
+ v_ws.Cells(j, i).value = new_data(j, i)
+ Next
+ Next
+ Me.SetData new_data
+End Sub
+
+Public Sub GetData(ByRef data_out() As String)
+ data_out = v_data
+End Sub
+Public Sub SetData(ByRef new_data() As String, Optional update_ws As Boolean = False)
+' FUNCTION
+ ' Set local data to new matrix, and optionally, update Worksheet
+' ARGUMENTS
+ ' String Matrix new_data
+ ' Boolean update_ws
+' VARIABLE DECLARATION
+ Dim sz_x As Long
+ Dim sz_y As Long
+ Dim sz_z As Long
+' VARIABLE INSTANTIATION
+ sz_x = SizeArrayDim_String(new_data, 2)
+ sz_y = SizeArrayDim_String(new_data, 1)
+ sz_z = SizeArrayDim_String(new_data, 3)
+' ARGUMENT VALIDATION
+ If Not ((sz_x = v_col_max - v_col_min + 1) And (sz_y = v_row_max - v_row_min + 1) And (sz_z = 0)) Then
+ MsgBox "Invalid dimensions to replace worksheet data"
+ Exit Sub
+ End If
+' METHODS
+ Erase v_data
+' RETURNS
+ v_data = new_data
+ If update_ws Then ExportLocalData2WS
+End Sub
+Public Sub SetEmptyData(ByRef new_data() As String, Optional update_ws As Boolean = False)
+' FUNCTION
+ ' Set local data to new matrix, and optionally, update Worksheet. Only populate empty cells
+' ARGUMENTS
+ ' String Matrix new_data
+ ' Boolean update_ws
+' VARIABLE DECLARATION
+ Dim sz_x As Long
+ Dim sz_y As Long
+ Dim sz_z As Long
+ Dim i As Long
+ Dim j As Long
+' VARIABLE INSTANTIATION
+ sz_x = SizeArrayDim_String(new_data, 2)
+ sz_y = SizeArrayDim_String(new_data, 1)
+ sz_z = SizeArrayDim_String(new_data, 3)
+' ARGUMENT VALIDATION
+ If Not ((sz_x = v_col_max - v_col_min + 1) And (sz_y = v_row_max - v_row_min + 1) And (sz_z = 0)) Then
+ MsgBox "Invalid dimensions to replace worksheet data"
+ Exit Sub
+ End If
+' METHODS
+ For j = 1 To sz_y
+ For i = 1 To sz_x
+ If v_data(j, i) = "" Then v_data(j, i) = new_data(j, i)
+ Next
+ Next
+' RETURNS
+ If update_ws Then ExportLocalData2WS
+End Sub
+
+Public Function GetCellDataLocal(ByVal row As Long, ByVal col As Long) As String
+' FUNCTION
+ ' Get value from cell of local copy of Worksheet data
+' ARGUMENTS
+ ' Long row - relative row index in data table
+ ' Long col - relative column index in data table
+' VARIABLE INSTANTIATION
+ If ((row > v_row_max - v_row_min + 1) Or (row < 1) Or (col > v_col_max - v_col_min + 1) Or (col < 1)) Then
+ Debug.Print "Invalid cell index. Row = " & CStr(row) & ", column = " & CStr(col)
+ Exit Function
+ End If
+' RETURNS
+ GetCellDataLocal = v_data(row, col)
+End Function
+Public Sub SetCellDataLocal(ByVal row As Long, ByVal col As Long, ByVal new_value As String)
+' FUNCTION
+ ' Set value of cell of local copy of Worksheet data
+' ARGUMENTS
+ ' Long row - relative row index in data table
+ ' Long col - relative column index in data table
+' VARIABLE INSTANTIATION
+ If ((row > v_row_max - v_row_min + 1) Or (row < 1) Or (col > v_col_max - v_col_min + 1) Or (col < 1)) Then
+ Debug.Print "Invalid cell index. Row = " & CStr(row) & ", column = " & CStr(col)
+ Exit Sub
+ End If
+' METHODS
+ v_data(row, col) = new_value
+End Sub
+
+Public Property Get nHeadings() As Long
+ nHeadings = SizeArrayDim_String(v_headings)
+End Property
+
+Public Property Get HeadingName(ByVal h_index As Long) As String
+' FUNCTION
+ ' Get heading text from index in v_headings
+' ARGUMENTS
+ ' Long h_index - index of desired heading in v_headings
+' VARIABLE DECLARATION
+ Dim n_h As Long
+' VARIABLE INSTANTIATION
+ n_h = nHeadings
+ HeadingName = "Error: Invalid index"
+' ARGUMENT VALIDATION
+ If (h_index < 1 Or h_index > n_h) Then Exit Property
+' RETURNS
+ HeadingName = v_headings(h_index)
+End Property
+
+Public Property Get HeadingIndex(ByVal h_search As String, Optional ID_in_searchstr As Boolean = False) As Long
+' FUNCTION
+ ' Get index of h_search within v_headings, if it exists
+' ARGUMENTS
+ ' String h_search - heading to search for
+ ' Boolean ID_in_searchstr - is index in h_search instead of heading name?
+' VARIABLE DECLARATION
+ Dim n_h As Long
+ Dim h_i As Long
+' ARGUMENT VALIDATION
+ If h_search = "" Then Exit Property
+ If Not ExistsHeading(h_search, ID_in_searchstr) Then Exit Property
+' VARIABLE INSTANTIATION
+ n_h = nHeadings
+' METHODS
+ If ID_in_searchstr Then
+ h_i = CLng(h_search)
+ If (h_i < 1 Or h_i > n_h) Then Exit Property
+ HeadingIndex = h_i
+ Else
+ For h_i = 1 To n_h
+ If (v_headings(h_i) = h_search) Then
+ HeadingIndex = h_i
+ Exit Property
+ End If
+ Next
+ End If
+End Property
+
+Public Property Get ColumnID(ByVal h_search As String, Optional ID_in_searchstr As Boolean = False) As Long
+' FUNCTION
+ ' Get index of column containing h_search, if it exists within v_headings
+' ARGUMENTS
+ ' String h_search - heading to search for
+ ' Boolean ID_in_searchstr - is index in h_search instead of heading name?
+' VARIABLE DECLARATION
+ Dim n_h As Long
+ Dim h_i As Long
+' ARGUMENT VALIDATION
+ If h_search = "" Then Exit Property
+ If Not ExistsHeading(h_search, ID_in_searchstr) Then Exit Property
+' VARIABLE INSTANTIATION
+ n_h = nHeadings
+' METHODS
+ If ID_in_searchstr Then
+ h_i = CLng(h_search)
+ If (h_i < 1 Or h_i > n_h) Then Exit Property
+ ColumnID = v_col_IDs(CLng(h_search))
+ Else
+ For h_i = 1 To n_h
+ If (v_headings(h_i) = h_search) Then
+ ColumnID = v_col_IDs(h_i)
+ Exit Property
+ End If
+ Next
+ End If
+End Property
+
+Public Property Get SearchID() As Long
+' FUNCTION
+ ' Get index of search row / column
+' METHODS
+ If v_dir = ColumnHeaders Then
+ SearchID = v_col_search
+ Else
+ SearchID = v_row_search
+ End If
+End Property
+
+Public Property Get ColumnMin() As Long
+ ColumnMin = v_col_min
+End Property
+Public Property Get ColumnMax() As Long
+ ColumnMax = v_col_max
+End Property
+Public Property Get ColumnSearch() As Long
+ ColumnSearch = v_col_search
+End Property
+Public Property Let ColumnSearch(ByVal vNew As Long)
+ If Not valid_coordinate(1, vNew) Then
+ MsgBox "Error: Invalid new search column index"
+ Exit Property
+ End If
+ If vNew < v_col_min And v_dir = ColumnHeaders Then v_col_min = vNew
+ If vNew > v_col_max And v_dir = ColumnHeaders Then v_col_max = vNew
+ If vNew > v_col_min And v_dir = RowHeaders Then v_col_min = vNew + 1
+ v_col_search = vNew
+End Property
+
+Public Property Get RowMin() As Long
+ RowMin = v_row_min
+End Property
+Public Property Get RowMax() As Long
+ RowMax = v_row_max
+End Property
+Public Property Get RowSearch() As Long
+ RowSearch = v_row_search
+End Property
+Public Property Let RowSearch(ByVal vNew As Long)
+ If Not valid_coordinate(vNew, 1) Then
+ MsgBox "Error: Invalid new search column index"
+ Exit Property
+ End If
+ If vNew < v_row_min And v_dir = RowHeaders Then v_row_min = vNew
+ If vNew > v_row_max And v_dir = RowHeaders Then v_row_max = vNew
+ If vNew > v_row_min And v_dir = ColumnHeaders Then v_row_min = vNew + 1
+ v_row_search = vNew
+End Property
+
+Public Property Get Orient() As orientation
+ Orient = v_dir
+End Property
+
+Public Property Get GapMax() As Long
+ GapMax = v_gap_max
+End Property
+
+
+' METHODS
+Public Sub Init(ByRef ws As Worksheet, ByVal name As String, ByRef headings() As String, Optional Orient As orientation = orientation.ColumnHeaders, Optional search_col As Long = 1, Optional search_col_is_heading_index As Boolean = False, Optional search_row As Long = 1, Optional search_row_is_heading_index As Boolean = False, Optional col_min As Long = 1, Optional col_max As Long = 0, Optional row_min As Long = 2, Optional row_max As Long = 0, Optional gap_max As Long = 1, Optional mutable_headings As Boolean = False)
+' FUNCTION
+ ' Instantiate worksheet container
+' ARGUMENTS
+ ' Worksheet ws
+ ' String name
+ ' String Array headings
+ ' orientation Orient
+ ' Long search_col
+ ' Boolean search_col_is_heading_index
+ ' Long search_row
+ ' Boolean search_row_is_heading_index
+ ' Long col_min
+ ' Long col_max
+ ' Long row_min
+ ' Long row_max
+ ' Long gap_max
+ ' Boolean mutable_headings - can headings be over-written?
+' PROCESSING ACCELERATION
+' CONSTANTS
+ Const uni_col_max As Long = 16384
+ Const uni_row_max As Long = 1048576
+' VARIABLE DECLARATION
+ If ws Is Nothing Then Exit Sub
+ Erase v_data
+ Dim my_data() As Variant
+ Dim col_min_str As String
+ Dim col_max_str As String
+ Dim col_search_str As String
+ Dim n_h As Long
+ Dim ws_data_in() As Variant
+ Dim temp_col_min As String
+ Dim temp_col_max As String
+' ARGUMENT VALIDATION
+' VARIABLE INSTANTIATION
+ If (Orient = ColumnHeaders) Then
+ v_col_search = min_Long(max_Long(1, search_col), uni_col_max)
+ v_row_search = min_Long(max_Long(1, search_row), uni_row_max - 1)
+ v_col_min = min_Long(max_Long(1, col_min), uni_col_max - 1)
+ v_row_min = min_Long(max_Long(1 + v_row_search, row_min), uni_row_max)
+ Else
+ v_col_search = min_Long(max_Long(1, search_col), uni_col_max - 1)
+ v_row_search = min_Long(max_Long(1, search_row), uni_row_max)
+ v_col_min = min_Long(max_Long(1 + v_col_search, col_min), uni_col_max)
+ v_row_min = min_Long(max_Long(1, row_min), uni_row_max - 1)
+ End If
+ col_min_str = get_col_str(v_col_min)
+ n_h = SizeArrayDim_String(headings)
+' METHODS
+ Set v_ws = ws
+ v_name = name
+ ' Dimensions
+ If (Orient = ColumnHeaders) Then
+ If (col_max < v_col_min) Then
+ v_col_max = last_filled_cell(Range_1D_String(ws, Range_String(1, 0, v_row_search, v_row_search)), v_col_min, -1, gap_max) ' ws.Range("A" & CStr(v_row_search) & ":XFD" & CStr(v_row_search)).Value,
+ If v_col_max = v_col_min And IsEmpty(ws.Cells(v_row_search, v_col_max)) Then v_col_max = v_col_search
+ Else
+ v_col_max = min_Long(col_max, uni_col_max)
+ End If
+ ' Headings + Column IDs
+ If n_h > 0 Then
+ v_headings = headings
+ GetColIDs
+ If (search_col_is_heading_index And v_col_search <= n_h) Then
+ If (v_col_IDs(v_col_search) > 0) Then v_col_search = v_col_IDs(v_col_search)
+ End If
+ End If
+ If (row_max < v_row_min) Then
+ v_row_max = last_filled_cell(Range_1D_String(ws, Range_String(v_col_search + v_col_min - 1, v_col_search + v_col_min - 1, 1, 0)), v_row_min, -1, gap_max)
+ If v_row_max = v_row_min And IsEmpty(ws.Cells(v_row_max, v_col_search + v_col_min - 1)) Then v_row_max = v_row_search
+ Else
+ v_row_max = min_Long(v_row_max, uni_row_max)
+ End If
+ ' Get data
+ my_data = ws.Range(Range_String(v_col_min, v_col_max, v_row_min, max_Long(v_row_max, v_row_min))).value
+ Else
+ If (row_max < v_row_min) Then
+ v_row_max = last_filled_cell(Range_1D_String(ws, Range_String(1, 0, v_row_search + v_row_min - 1, v_row_search + v_row_min - 1)), v_row_min, -1, gap_max)
+ If v_row_max = v_row_min And IsEmpty(ws.Cells(v_row_max, v_col_search)) Then v_row_max = v_row_search
+ Else
+ v_row_max = min_Long(v_row_max, uni_row_max)
+ End If
+ ' Headings + Column IDs
+ If n_h > 0 Then
+ v_headings = headings
+ GetColIDs
+ If (search_row_is_heading_index And v_row_search <= n_h) Then
+ If (v_col_IDs(v_row_search) > 0) Then v_row_search = v_col_IDs(v_row_search)
+ End If
+ End If
+ If (col_max < v_col_min) Then
+ v_col_max = last_filled_cell(Range_1D_String(ws, Range_String(1, 0, v_row_search, v_row_search)), v_col_min, -1, gap_max) ' ws.Range("A" & CStr(v_row_search) & ":XFD" & CStr(v_row_search)).Value,
+ If v_col_max = v_col_min And IsEmpty(ws.Cells(v_row_search, v_col_max)) Then v_col_max = v_col_search
+ Else
+ v_col_max = min_Long(col_max, uni_col_max)
+ End If
+ ' Get data
+ my_data = ws.Range(Range_String(v_col_min, max_Long(v_col_max, v_col_min), v_row_min, v_row_max)).value
+ End If
+' RETURNS
+ convert_2D_Variant_2_String my_data, v_data
+ v_mutable_headings = mutable_headings
+ v_gap_max = gap_max
+End Sub
+
+Private Sub GetColIDs()
+' FUNCTION
+ ' Find first instance of each of colnames within row RowSearch of ws
+' VARIABLE DECLARATION
+ Dim NHeading As Long
+ Dim Search_S As String
+ Dim iCol As Long
+ Dim ColName As String
+ Dim Outs() As Long
+ Dim Temps() As String
+ Dim iFail As Long
+ Dim fail_S As String
+' VARIABLE INSTANTIATION
+ NHeading = nHeadings
+ ReDim v_col_IDs(NHeading)
+ If (v_dir = ColumnHeaders) Then
+ ' Temps = v_ws.Range(Range_String(v_col_min, v_col_max, v_row_search, v_row_search)).value
+ Temps = Range_1D_String(v_ws, Range_String(v_col_min, v_col_max, v_row_search, v_row_search))
+ Else
+ ' Temps = v_ws.Range(Range_String(v_col_search, v_col_search, v_row_min, v_row_max)).value
+ Temps = Range_1D_String(v_ws, Range_String(v_col_min, v_col_max, v_row_search, v_row_search))
+ End If
+ iFail = 0
+' METHODS
+ For iCol = 1 To NHeading
+ ColName = v_headings(iCol)
+ If Not ColName = "" Then
+ v_col_IDs(iCol) = match_String(ColName, Temps)
+ If v_col_IDs(iCol) < 1 Then
+ iFail = iFail + 1
+ If iFail > 1 Then
+ fail_S = fail_S & ", " & ColName
+ Else
+ fail_S = ColName
+ End If
+ End If
+ Else
+ v_col_IDs(iCol) = 0
+ End If
+ Next
+' RETURNS
+ If iFail > 0 Then
+ MsgBox "Error: The below headings were not found on Worksheet " & v_name & vbCrLf & fail_S
+ End If
+End Sub
+
+Public Sub ExportLocalData2WS()
+' FUNCTION
+ ' Export v_data to v_ws
+' METHODS
+ If v_dir = ColumnHeaders Then
+ v_ws.Range(Range_String(v_col_min, v_col_max, v_row_min, max_Long(v_row_max, v_row_min))).value = v_data
+ Else
+ v_ws.Range(Range_String(v_col_min, max_Long(v_col_max, v_col_min), v_row_min, v_row_max)).value = v_data
+ End If
+End Sub
+
+Public Sub AddCellComment(ByVal row As Long, ByVal col As Long, ByVal mycomment As String, Optional row_abs_not_rel As Boolean = False, Optional col_abs_not_rel As Boolean = False)
+' FUNCTION
+ ' Add comment to cell
+' ARGUMENTS
+ ' Long row
+ ' Long col - cell index
+ ' String mycomment
+ ' Optional Boolean row_abs_not_rel
+ ' Optional Boolean col_abs_not_rel - positioning on worksheet relative to v_data table
+' METHODS
+ v_ws.Range(Range_String(col, col, row, row)).AddComment mycomment
+End Sub
+
+Public Sub ResizeLocalAuto(Optional replace_local_data As Boolean = True)
+' FUNCTION
+ ' Find table size from Worksheet
+' PROCESSING ACCELERATION
+ ' Disable automatic spreadsheet calculation - prevents refreshing of whole ws on each cell entry
+ Application.Calculation = xlCalculationManual
+ ' Disable screen updating
+ Application.ScreenUpdating = False
+' CONSTANTS
+ Const uni_col_max As Long = 16384
+ Const uni_row_max As Long = 1048576
+' VARIABLE DECLARATION
+ Dim my_data() As Variant
+' METHODS
+ If (Orient = ColumnHeaders) Then
+ v_row_max = last_filled_cell(Range_1D_String(v_ws, Range_String(v_col_search, v_col_search, 1, 0)), v_row_min, -1, v_gap_max)
+ If v_row_max = v_row_min And IsEmpty(v_ws.Cells(v_row_max, v_col_search)) Then v_row_max = v_row_search
+ ' Get data
+ my_data = v_ws.Range(Range_String(v_col_min, v_col_max, v_row_min, max_Long(v_row_max, v_row_min))).value
+ Else
+ v_col_max = last_filled_cell(Range_1D_String(v_ws, Range_String(1, 0, v_row_search, v_row_search)), v_col_min, -1, v_gap_max) ' ws.Range("A" & CStr(v_row_search) & ":XFD" & CStr(v_row_search)).Value,
+ If v_col_max = v_col_min And IsEmpty(v_ws.Cells(v_row_search, v_col_max)) Then v_col_max = v_col_search
+ ' Get data
+ my_data = v_ws.Range(Range_String(v_col_min, max_Long(v_col_max, v_col_min), v_row_min, v_row_max)).value
+ End If
+' RETURNS
+ If replace_local_data Then
+ Erase v_data
+ convert_2D_Variant_2_String my_data, v_data
+ End If
+' PROCESSING DECELARATION
+ ' Enable automatic spreadsheet calculation - prevents refreshing of whole ws on each cell entry
+ Application.Calculation = xlCalculationAutomatic
+ ' Enable screen updating
+ Application.ScreenUpdating = True
+End Sub
+
+
+Public Sub ResizeLocal(ByVal col_min As Long, ByVal col_max As Long, ByVal row_min As Long, ByVal row_max As Long, Optional clear_data As Boolean = False)
+' FUNCTION
+ ' Resize local copy of ws table data
+' ARGUMENTS
+ ' Long col_min
+ ' Long col_max
+ ' Long row_min
+ ' Long row_max
+ ' Optional Boolean clear_data
+' ARGUMENT VALIDATION
+ If col_max < col_min Or row_max < row_min Or (Not valid_coordinate(row_min, col_min)) Or (Not valid_coordinate(row_max, col_max)) Then
+ MsgBox "Error: Invalid new local data dimensions." & vbCrLf & error_msg(col_min, "col_min", "Long", "?") & vbCrLf & error_msg(col_max, "col_max", "Long", "?") & vbCrLf & error_msg(row_min, "row_min", "Long", "?") & vbCrLf & error_msg(row_max, "row_max", "Long", "?")
+ Exit Sub
+ End If
+ If col_min = v_col_min And col_max = v_col_max And row_min = v_row_min And row_max = v_row_max Then
+ Debug.Print "No change required to local data dimensions."
+ Exit Sub
+ End If
+' METHODS
+ If clear_data Then
+ v_col_min = col_min
+ v_col_max = col_max
+ v_row_min = row_min
+ v_row_max = row_max
+ ClearData
+ Else
+ v_col_min = col_min
+ v_col_max = col_max
+ ReDimPreserve_String v_data, 2, v_col_max - v_col_min + 1, 2
+ v_row_min = row_min
+ v_row_max = row_max
+ ReDimPreserve_String v_data, 1, v_row_max - v_row_min + 1, 2
+ End If
+End Sub
+
+
+Public Function Match(ByVal v_search As String, Optional array_ID As Long = -1, Optional minimum As Long = 1, Optional maximum As Long = -1, Optional dir_move As dir_traverse = dir_traverse.FORWARDS) As Long
+' FUNCTION
+ ' Find match of v_search in local data and return index
+' ARGUMENTS
+ ' String v_search
+ ' Long array_ID
+ ' Long minimum
+ ' Long maximum
+ ' dir_traverse dir_move
+' CONSTANTS
+ ' Const RowMaxUniversal As Long = 1048576
+ ' Const ColMaxUniversal As Long = 16384
+' VARIABLE DECLARATION
+ Dim i As Long
+ Dim di As Long
+ Dim RowMaxUniversal As Long
+ Dim ColMaxUniversal As Long
+' ARGUMENT VALIDATION
+ Match = -1
+' VARIABLE INSTANTIATION
+ RowMaxUniversal = SizeArrayDim_String(v_data, 1) + v_row_min - 1
+ ColMaxUniversal = SizeArrayDim_String(v_data, 2) + v_col_min - 1
+ If v_dir = ColumnHeaders Then
+ maximum = min_Long(RowMaxUniversal - v_row_min + 1, maximum)
+ If maximum < 1 Then maximum = RowMaxUniversal - v_row_min + 1
+ minimum = max_Long(1, min_Long(minimum, maximum))
+ If Not valid_coordinate(minimum, 1) Then minimum = 1
+ If Not valid_coordinate(maximum, 1) Then maximum = v_row_max - v_row_min + 1
+ If Not valid_coordinate(1, array_ID) Then array_ID = v_col_search
+ Else
+ maximum = min_Long(ColMaxUniversal - v_col_min + 1, maximum)
+ If maximum < 1 Then maximum = ColMaxUniversal - v_col_min + 1
+ minimum = max_Long(1, min_Long(minimum, maximum))
+ If Not valid_coordinate(1, minimum) Then minimum = 1
+ If Not valid_coordinate(1, maximum) Then maximum = v_col_max - v_col_min + 1
+ If Not valid_coordinate(array_ID, 1) Then array_ID = v_row_search
+ End If
+ If maximum < minimum Then ' not possible
+ MsgBox "Error: Invalid minimum (" & CStr(minimum) & ") and maximum (" & CStr(maximum) & ") positions."
+ Exit Function
+ End If
+ If dir_move = BACKWARDS Then
+ i = minimum
+ minimum = maximum
+ maximum = i
+ di = -1
+ Else
+ di = 1
+ End If
+' METHODS
+ For i = minimum To maximum Step di
+ If v_dir = ColumnHeaders Then
+ If v_data(i, array_ID) = v_search Then
+ Match = i
+ Exit For
+ End If
+ Else
+ If v_data(array_ID, i) = v_search Then
+ Match = i
+ Exit For
+ End If
+ End If
+ Next
+End Function
+
+
+Public Function Match_F(ByVal v_search As String, ByVal function_name As String, Optional array_ID As Long = -1, Optional minimum As Long = 1, Optional maximum As Long = -1, Optional dir_move As dir_traverse = dir_traverse.FORWARDS) As Long
+' FUNCTION
+ ' Find match of v_search in local data and return index
+' ARGUMENTS
+ ' String v_search
+ ' String function_name - name of function which takes single argument of cell data
+ ' Long array_ID
+ ' Long minimum
+ ' Long maximum
+ ' dir_traverse dir_move
+' VARIABLE DECLARATION
+ Dim i As Long
+ Dim di As Long
+' ARGUMENT VALIDATION
+ Match_F = -1
+' VARIABLE INSTANTIATION
+ i = max_Long(minimum, maximum)
+ minimum = max_Long(1, min_Long(minimum, maximum))
+ If v_dir = ColumnHeaders Then
+ maximum = min_Long(1048576, i)
+ If Not valid_coordinate(1, minimum) Then minimum = v_row_min
+ If Not valid_coordinate(1, maximum) Then maximum = v_row_max
+ If Not valid_coordinate(1, array_ID) Then array_ID = v_row_search
+ Else
+ maximum = min_Long(16384, i)
+ If Not valid_coordinate(minimum, 1) Then minimum = v_col_min
+ If Not valid_coordinate(maximum, 1) Then maximum = v_col_max
+ If Not valid_coordinate(array_ID, 1) Then array_ID = v_col_search
+ End If
+ If maximum < minimum Then
+ MsgBox "Error: Invalid minimum (" & CStr(minimum) & ") and maximum (" & CStr(maximum) & ") positions."
+ Exit Function
+ End If
+ If dir_move = BACKWARDS Then
+ i = minimum
+ minimum = maximum
+ maximum = i
+ di = -1
+ Else
+ di = 1
+ End If
+' METHODS
+ For i = minimum To maximum Step di
+ If v_dir = ColumnHeaders Then
+ If CStr(Application.Run(function_name, v_data(i, array_ID))) = v_search Then
+ Match_F = i
+ Exit For
+ End If
+ Else
+ If CStr(Application.Run(function_name, v_data(array_ID, i))) = v_search Then
+ Match_F = i
+ Exit For
+ End If
+ End If
+ Next
+End Function
+
+
+Public Function ExistsHeading(ByVal search_heading As String, Optional ID_in_str As Boolean = False) As Boolean
+' FUNCTION
+ ' Does search_heading exist in v_headings?
+' ARGUMENTS
+ ' String search_heading
+ ' Boolean ID_in_str - is search_heading a heading index instead of name?
+' VARIABLE DECLARATION
+ Dim h_n As Long
+ Dim h_i As Long
+ Dim ID As String
+' ARGUMENT VALIDATION
+ ExistsHeading = False
+ If search_heading = "" Then Exit Function
+' VARIABLE INSTANTIATION
+ h_n = nHeadings
+' METHODS
+ If ID_in_str Then
+ If Not is_numeric(search_heading) Then Exit Function
+ ID = CLng(search_heading)
+ If ID < 1 Or ID > h_n Then Exit Function
+ ExistsHeading = True
+ Exit Function
+ End If
+ For h_i = 1 To h_n
+ If v_headings(h_i) = search_heading Then
+ ExistsHeading = True
+ Exit Function
+ End If
+ Next
+End Function
+
+
+Public Sub ClearData()
+' FUNCTION
+ ' Clear contents of local copy of ws data but keep dimensions
+' METHODS
+ ReDim v_data(v_row_max - v_row_min + 1, v_col_max - v_col_min + 1)
+End Sub
+
+
+Public Function LastFilledCell(Optional search_ID As Long = -1, Optional start As Long = -1, Optional gap_max As Long = -1, Optional Orient As orientation, Optional i_max As Long = -1, Optional dir_move As dir_traverse) As Long
+' FUNCTION
+ ' Identify index of last filled cell in column or row from start in v_ws
+' ARGUMENTS
+ ' Optional Long search_ID
+ ' Optional Long start
+ ' Optional Long gap_max
+ ' Optional orientation orient
+ ' Optional Long i_max
+ ' Optional dir_traverse dir_move
+' CONSTANTS
+ Const uni_row_max As Long = 1048576
+ Const uni_col_max As Long = 16384
+' VARIABLE DECLARATION
+ Dim found_last As Boolean
+ Dim temp As Variant
+ Dim gap_n As Long
+ Dim i As Long
+' ARGUMENT VALIDATION
+ If Orient = ColumnHeaders Then
+ If i_max < 1 Then i_max = uni_row_max
+ i_max = min_Long(max_Long(1, i_max), uni_row_max)
+ If search_ID < 1 Then
+ search_ID = v_col_search
+ Else
+ search_ID = min_Long(max_Long(1, search_ID), uni_col_max)
+ End If
+ gap_max = min_Long(max_Long(1, gap_max), i_max - 1)
+ If start = -1 Then
+ start = v_row_min
+ Else
+ start = min_Long(max_Long(1, v_row_min), max_Long(i_max - gap_max, 1))
+ End If
+ Else
+ If i_max < 1 Then i_max = uni_col_max
+ i_max = min_Long(max_Long(1, i_max), uni_col_max)
+ If search_ID < 1 Then
+ search_ID = v_row_search
+ Else
+ search_ID = min_Long(max_Long(1, search_ID), uni_row_max)
+ End If
+ gap_max = min_Long(max_Long(1, gap_max), i_max - 1)
+ If start = -1 Then
+ start = v_row_min
+ Else
+ start = min_Long(max_Long(1, v_row_min), max_Long(i_max - gap_max, 1))
+ End If
+ End If
+' VARIABLE INSTANTIATION
+ found_last = False
+ If dir_move = FORWARDS Then
+ i = start - 1
+ Else
+ i = i_max + 1
+ End If
+' METHODS
+ Do While Not found_last
+ i = i + dir_move
+ If Orient = ColumnHeaders Then
+ temp = Me.Cell(i, search_ID).value
+ Else
+ temp = Me.Cell(search_ID, i).value
+ End If
+ If (dir_move = FORWARDS And i = start) Or (dir_move = FORWARDS And i = start) Then
+ found_last = True
+ ElseIf IsEmpty(temp) Then
+ gap_n = gap_n + 1
+ Else
+ gap_n = 0
+ End If
+ If gap_n > gap_max Then found_last = True
+ Loop
+' RETURNS
+ LastFilledCell = i - gap_n * dir_move
+End Function
diff --git a/src/WS_Relation.cls b/src/WS_Relation.cls
new file mode 100644
index 0000000..60e346e
--- /dev/null
+++ b/src/WS_Relation.cls
@@ -0,0 +1,276 @@
+' Author: Edward Middleton-Smith
+' Precision And Research Technology Systems Limited
+
+
+' MODULE INITIALISATION
+' Set array start index to 1 to match spreadsheet indices
+Option Base 1
+' Forced Variable Declaration
+Option Explicit
+
+
+
+
+' PROPERTY DECLARATION
+Private v_wss As Collection
+Private v_N_h As Long
+Private v_name As String
+
+
+' PROPERTY METHODS
+Public Property Get nSheets() As Long
+ nSheets = CountWorksheets
+End Property
+
+Public Property Get nHeadings() As Long
+ nHeadings = v_N_h
+End Property
+
+Public Property Get wsc(ByVal sheet_name As String, Optional ID_in_name As Boolean = False) As ws_access
+' FUNCTION
+ ' Get Worksheet Container object by name or ID
+' ARGUMENTS
+ ' String sheet_name
+ ' Boolean ID_in_name
+' VARIABLE DECLARATION
+ Dim sheet_ID As Long
+ Dim n_ws As Long
+ Dim w_i As Long
+ Dim wsc_temp As ws_access
+' VARIABLE INSTANTIATION
+ n_ws = CountWorksheets
+' METHODS
+ If sheet_name = "" Then Exit Property
+ If ID_in_name Then
+ If Not is_numeric(sheet_name) Then Exit Property
+ sheet_ID = CLng(sheet_name)
+ If (sheet_ID < 1 Or sheet_ID > n_ws) Then
+ Set wsc = v_wss.Item(sheet_ID)
+ Exit Property
+ End If
+ End If
+ For w_i = 1 To n_ws
+ Set wsc_temp = v_wss.Item(w_i)
+ If wsc_temp.name = sheet_name Then
+ Set wsc = wsc_temp
+ Exit Property
+ End If
+ Next
+End Property
+
+Public Property Get name() As String
+ name = v_name
+End Property
+Public Property Let name(ByVal new_name As String)
+ v_name = new_name
+End Property
+
+
+' METHODS
+Public Function AddWSC(ByRef wsc As ws_access, Optional suppress_console_outputs As Boolean = False) As Boolean
+' FUNCTION
+ ' Get Workheet Container object by name or ID
+' ARGUMENTS
+ ' ws_access wsc
+' VARIABLE DECLARATION
+ Dim n_h As Long
+ Dim n_ws As Long
+' ARGUMENT VALIDATION
+ AddWSC = False
+ If wsc Is Nothing Then Exit Function
+' VARIABLE INSTANTIATION
+ n_h = wsc.nHeadings
+ n_ws = CountWorksheets
+' METHODS
+ If n_ws = 0 Then
+ v_N_h = n_h
+ Set v_wss = New Collection
+ v_wss.Add wsc
+ If Not suppress_console_outputs Then Debug.Print "Worksheet Container " & wsc.name & " added to Worksheet Relation " & v_name & "."
+ Else
+ If v_N_h = n_h Then
+ v_wss.Add wsc
+ If Not suppress_console_outputs Then Debug.Print "Worksheet Container " & wsc.name & " added to Worksheet Relation " & v_name & "."
+ Else
+ MsgBox "Error: Unable to add Worksheet Container " & wsc.name & " to Worksheet Relation " & v_name & "." & vbCrLf & _
+ CStr(v_N_h) & " headings in relation." & vbCrLf & _
+ CStr(n_h) & " headings in container."
+ Exit Function
+ End If
+ End If
+' RETURNS
+ AddWSC = True
+End Function
+
+Private Function CountWorksheets() As Long
+ If v_wss Is Nothing Then
+ CountWorksheets = 0
+ Else
+ CountWorksheets = v_wss.count
+ End If
+End Function
+
+Public Sub Populate(Optional recipient_wsc As String = "1", Optional recipient_ID_in_str As Boolean = True, Optional provider_wsc As String = "2", Optional provider_ID_in_str As Boolean = True, Optional write_empties_only As Boolean = False, Optional export_2_ws As Boolean = True)
+' FUNCTION
+ ' Populate recipient wsc with data for each record by look up in provider wsc
+' ARGUMENTS
+ ' Optional String recipient_wsc
+ ' Optional Boolean recipient_ID_in_str
+ ' Optional String provider_wsc
+ ' Optional Boolean provider_ID_in_str - ID instead of wsc name?
+' VARIABLE DECLARATION
+ Dim n_ws As Long
+ Dim wsc_recipient As ws_access
+ Dim wsc_provider As ws_access
+ Dim h_i As Long
+ Dim col_recipient As Long
+ Dim col_provider As Long
+ Dim row_recipient As Long
+ Dim row_provider As Long
+' ARGUMENT VALIDATION
+ n_ws = CountWorksheets
+ If (n_ws < 2 Or (recipient_wsc = provider_wsc And recipient_ID_in_str = provider_ID_in_str) Or Not ExistsWSC(recipient_wsc, recipient_ID_in_str) Or Not ExistsWSC(recipient_wsc, recipient_ID_in_str)) Then
+ MsgBox "Error: Unable to populate Worksheet Container " & recipient_wsc & " with " & provider_wsc
+ Exit Sub
+ End If
+' VARIABLE INSTANTIATION
+ Set wsc_recipient = v_wss.Item(IndexWSC(recipient_wsc, recipient_ID_in_str))
+ Set wsc_provider = v_wss.Item(IndexWSC(provider_wsc, provider_ID_in_str))
+' METHODS
+ If wsc_recipient.Orient = ColumnHeaders Then
+ For row_recipient = 1 To wsc_recipient.RowMax - wsc_recipient.RowMin + 1
+ row_provider = wsc_provider.Match(wsc_recipient.GetCellDataLocal(row_recipient, wsc_recipient.ColumnSearch))
+ If row_provider > 0 Then
+ For h_i = 1 To v_N_h
+ col_recipient = wsc_recipient.ColumnID(CStr(h_i), True)
+ col_provider = wsc_provider.ColumnID(CStr(h_i), True)
+ If col_provider > 0 And col_recipient > 0 Then
+ If col_recipient <> wsc_recipient.ColumnSearch Then
+ If wsc_provider.Orient = ColumnHeaders Then
+ wsc_recipient.SetCellDataLocal row_recipient, col_recipient, wsc_provider.GetCellDataLocal(row_provider, col_provider)
+ Else
+ wsc_recipient.SetCellDataLocal row_recipient, col_recipient, wsc_provider.GetCellDataLocal(col_provider, row_provider)
+ End If
+ End If
+ End If
+ Next
+ Else
+ Debug.Print "Customer not found:" & vbCrLf & "Customer reference = " & wsc_recipient.GetCellDataLocal(row_recipient, wsc_recipient.ColumnSearch) & vbCrLf & "Row = " & CStr(row_recipient + wsc_recipient.RowMin - 1)
+ End If
+ Next
+ Else
+ For row_recipient = 1 To wsc_recipient.ColumnMax - wsc_recipient.ColumnMin + 1
+ row_provider = wsc_provider.Match(wsc_recipient.GetCellDataLocal(wsc_recipient.RowSearch, row_recipient))
+ If row_provider > 0 Then
+ For h_i = 1 To v_N_h
+ col_recipient = wsc_recipient.ColumnID(CStr(h_i), True)
+ col_provider = wsc_provider.ColumnID(CStr(h_i), True)
+ If col_provider > 0 And col_recipient > 0 Then
+ If (wsc_recipient.Orient = ColumnHeaders And col_recipient <> wsc_recipient.ColumnSearch) Then
+ If wsc_provider.Orient = ColumnHeaders Then
+ wsc_recipient.SetCellDataLocal col_recipient, row_recipient, wsc_provider.GetCellDataLocal(row_provider, col_provider)
+ Else
+ wsc_recipient.SetCellDataLocal col_recipient, row_recipient, wsc_provider.GetCellDataLocal(col_provider, row_provider)
+ End If
+ End If
+ End If
+ Next
+ Else
+ Debug.Print "Customer not found:" & vbCrLf & "Customer reference = " & wsc_recipient.GetCellDataLocal(wsc_recipient.RowSearch, row_recipient) & vbCrLf & "Column = " & CStr(row_recipient + wsc_recipient.ColumnMin - 1)
+ End If
+ Next
+ End If
+' RETURNS
+ If export_2_ws Then wsc_recipient.ExportLocalData2WS
+End Sub
+
+Public Function ExistsWSC(ByVal wsc_name As String, Optional ID_in_str As Boolean = True) As Boolean
+' FUNCTION
+ ' Identify is Worksheet Container exists in relationship
+' ARGUMENTS
+ ' String wsc_name
+ ' Optional Boolean ID_in_str - ID as String instead of name?
+' VARIABLE DECLARATION
+ Dim n_ws As Long
+ Dim w_i As Long
+ Dim ID As Long
+' VARIABLE INSTANTIATION
+ n_ws = CountWorksheets
+ ExistsWSC = False
+' ARGUMENT VALIDATION
+ If wsc_name = "" Then Exit Function
+' METHODS
+ If ID_in_str Then
+ If Not is_numeric(wsc_name) Then Exit Function
+ ID = CLng(wsc_name)
+ If Not (ID > 0 And ID <= n_ws) Then Exit Function
+ ExistsWSC = True
+ Exit Function
+ End If
+ For w_i = 1 To n_ws
+ If v_wss.Item(w_i).name = wsc_name Then
+ ExistsWSC = True
+ Exit Function
+ End If
+ Next
+' RETURNS
+ MsgBox "Error: Worksheet Container " & wsc_name & " not found in relation."
+End Function
+
+Public Function IndexWSC(ByVal wsc_name As String, Optional ID_in_str As Boolean = True) As Long
+' FUNCTION
+ ' Get index for Worksheet Container within relationship
+' ARGUMENTS
+ ' String wsc_name
+ ' Optional Boolean ID_in_str - ID as String instead of name?
+' VARIABLE DECLARATION
+ Dim n_ws As Long
+ Dim w_i As Long
+' VARIABLE INSTANTIATION
+ n_ws = CountWorksheets
+ IndexWSC = False
+' ARGUMENT VALIDATION
+ If Not ExistsWSC(wsc_name, ID_in_str) Then Exit Function
+' METHODS
+ If ID_in_str Then
+ IndexWSC = CLng(wsc_name)
+ Else
+ For w_i = 1 To n_ws
+ If v_wss.Item(w_i).name = wsc_name Then
+ IndexWSC = w_i
+ Exit For
+ End If
+ Next
+ End If
+End Function
+
+Public Function SafeOpenWB(ByVal pathWB As String) As Workbook
+' FUNCTION
+ ' Open Workbook if not already open
+' ARGUMENTS
+ ' String pathWB
+' VARIABLE DECLARATION
+ Dim wb As Workbook
+ Dim nameWB As String
+ Dim nWB As Long
+ Dim iWB As Long
+' VARIABLE INSTANTIATION
+ nameWB = Path2Name(pathWB)
+ nWB = Workbooks.count
+' ARGUMENT VALIDATION
+ If nameWB = "" Then
+ MsgBox "Error: Invalid workbook path"
+ Exit Function
+ End If
+' METHODS
+ For iWB = 1 To nWB
+ Set wb = Workbooks.Item(iWB)
+ If wb.name = nameWB Then
+ Set SafeOpenWB = wb
+ Exit Function
+ End If
+ Next
+' RETURNS
+ Set SafeOpenWB = Workbooks.Open(pathWB)
+End Function
+