Skip to content

Instantly share code, notes, and snippets.

@capm
Last active September 28, 2022 15:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capm/19edce12a3943d00492030b3cbf32a8d to your computer and use it in GitHub Desktop.
Save capm/19edce12a3943d00492030b3cbf32a8d to your computer and use it in GitHub Desktop.
Common functions I use in VBA.
Private Sub Auto_Open()
Call FXSBS
End Sub
Attribute VB_Name = "capmFunctions"
'XXXXX
Public Function RightToLeftChar(InputData As String, InputChar As String) As String
'
RightToLeftChar = Right(InputData, Len(InputData) - WorksheetFunction.Find("|", WorksheetFunction.Substitute(InputData, InputChar, "|", Len(InputData) - Len(WorksheetFunction.Substitute(InputData, InputChar, "")))))
End Function
'XXXXX
Public Function FindLastRow(flrSheet As Worksheet, flrCol As Integer) As Range
Set FindLastRow = flrSheet.Cells(flrSheet.Rows.Count, flrCol).End(xlUp)
End Function
'XXXXX
Public Function GetRawHTML(urlWebSite As String)
'
Set GetRawHTML = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", urlWebSite, False
.send
GetRawHTML = .responseText
End With
' First select "Microsoft HTML Object Library" from VBA References
' Use it this way:
' Set oHtml = New HTMLDocument
' oHtml.body.innerHTML = GetRawHTML(urlWebSite)
End Function
'XXXXX
Public Function GetRawHTMLIE(urlWebSite As String) As MSHTML.HTMLDocument
' Function to scrape websites navigating webs
' Need Microsoft HTML Object Library, Microsoft Internet Controls and Microsoft Forms 2.0 Object Library
' Get Usage
'Dim htmlSMV As MSHTML.HTMLDocument
'Set htmlSMV = GetRawHTMLIE(urlScrap)
'Dim tableFM As MSHTML.HTMLTable
'Set tableFM = htmlSMV.getElementById("grdValorCuota")
' Declaring an IE object
Dim ie As SHDocVw.InternetExplorer
' Setting the IE Object
Set ie = New SHDocVw.InternetExplorer
' Surfing and scraping web
With ie
'
.Visible = False
.navigate urlWebSite
' Wait until web finish loading
While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Wend
' Assign document to variable and close IE
Set GetRawHTMLIE = .document
.Quit
End With
End Function
'XXXXX
Public Function FindLastCell(wsEval As Worksheet, wsCol As Integer, fType As Integer) As Long
' wsCol: Row or column number.
' fType can be 0 or 1.
' 0: Find last row in column
' 1: Find last column in row
'
If fType = 0 Then
FindLastCell = wsEval.Cells(wsEval.Rows.Count, wsCol).End(xlUp).Row
End If
If fType = 1 Then
FindLastCell = wsEval.Cells(wsCol, wsEval.Columns.Count).End(xlToLeft).Column
End If
If fType <> 0 And fType <> 1 Then
MsgBox "Must choose find last row in column or find last column in row."
End If
End Function
'XXXXX
Public Function BestPractices(Indicator As Integer)
' Indicator must be 0 or 1, 0 to turn off and 1 to turn back to normal.
If Indicator = 0 Then
' Turn off some Excel functionality so your code runs faster
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False 'note this is a sheet-level setting
End If
If Indicator = 1 Then
' Restore state
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculation = xlNormal
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True ' Note this is a sheet-level setting
End If
If Indicator <> 0 And Indicator <> 1 Then
MsgBox "Must choose between 0 and 1."
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment