Skip to content

Instantly share code, notes, and snippets.

View thoriqmacto's full-sized avatar
🎯
Focusing

Thariq thoriqmacto

🎯
Focusing
View GitHub Profile
@thoriqmacto
thoriqmacto / IsArrayUnique.bas
Created January 13, 2021 07:43
[VBA] Boolean function to return whether a 1D-array members is unique or not.
Public Function IsArrayUnique(tArr As Variant) As Boolean
Dim out As Boolean, tOut As Double
Dim i As Long
For Each t In tArr
If Len(t) > 0 Then
tOut = WorksheetFunction.Match(CStr(t), tArr, 0)
If WorksheetFunction.IsError(tOut) Then
out = False
Else
@thoriqmacto
thoriqmacto / ColumnNumberToLetter.bas
Created January 11, 2021 16:28
[VBA] Convert column number to letter
Public Function ColumnNumberToLetter(colNum As Long) As String
ColumnNumberToLetter = Split(Cells(1, colNum).Address, "$")(1)
End Function
@thoriqmacto
thoriqmacto / ArrayElements_1D.bas
Created January 11, 2021 15:41
[VBA] Return the number of elements inside 1D array
Public Function ArrayElements_1D(arrInput As Variant) As Integer
ArrayElements_1D = UBound(arrInput, 1) - LBound(arrInput) + 1
End Function
@thoriqmacto
thoriqmacto / WorkbookOperation.bas
Last active January 11, 2021 15:12
[VBA] Workbook operation including Create, Open and Close
'Create .xlsb file
Public Function RunWorkbookCreate(urlHistData As String, Optional CloseDirectly As Boolean = True) As Workbook
Dim newWbook As Workbook
Set newWbook = Workbooks.Add
newWbook.SaveAs Filename:=urlHistData, FileFormat:=50
Debug.Print "WbCREATE: " & newWbook.FullName
Set RunWorkbookCreate = newWbook
@thoriqmacto
thoriqmacto / FileSize.bas
Created December 19, 2020 00:48
[VBA] Get FileSize of file based on supplied path. Output in "bytes".
Function FileSize(path As String) As Variant
FileSize = FileLen(path)
End Function
@thoriqmacto
thoriqmacto / BulkDownloads.bas
Last active December 19, 2020 00:43
[VBA] Download bulk static files based on URL using WinHTTP
Sub BulkDownloads()
Dim i As Long, FileNum As Long, lrow As Long, headRow As Long
Dim FileData() As Byte
Dim MyFile As String, DownloadedDir As String
Dim WHTTP As Object
Dim ws As Excel.Worksheet
On Error Resume Next
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
@thoriqmacto
thoriqmacto / FileExist.bas
Created December 19, 2020 00:06
[VBA] Check file exist based on supplied path.
Function FileExist(path As String) As Boolean
If Dir(path) <> vbNullString Then FileExist = True
End Function
@thoriqmacto
thoriqmacto / ExtractExcelContents.bas
Last active December 19, 2020 00:44
[VBA] Loop through files in folder which contain only excel files with same format, then copy its content to master list.
Public Sub main()
Dim xDir As String
Workbooks("combined_list.xlsm").Activate
xDir = Range("root_url").Value
Call ExtractExcelContents(xDir, True)
End Sub
Private Sub ExtractExcelContents(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Dim xFileSystemObject As Object, xFolder As Object, xSubFolder As Object
@thoriqmacto
thoriqmacto / ListFilesInFolderWithHyperlink.bas
Last active December 19, 2020 00:49
[VBA] To list all files in folder along with hyperlinks.
@thoriqmacto
thoriqmacto / LoopShading.bas
Last active December 19, 2020 00:50
[VBA] Draw different color row shading based on LOOP tag.
Public Sub LoopShading()
Dim c As Range, bottomA As Long, curr As String, prev As String
Dim currColor As String, prevColor As String, tempcolor As String
bottomA = Range("A" & Rows.Count).End(xlUp).Row
currColor = "blue"
prevColor = "orange"
prev = "010-F-31001"
For Each c In Range("A2:A" & bottomA)
curr = c