Code Monkey home page Code Monkey logo

vba-for-everyone's Introduction

VBA for Everyone

Author: Santosh Rai January, 2018

Introduction

Requirments

Setup

CodingStyle

'/**
' * @Purpose:  Get Corresponding sheet
' * @Param  :  {Workbook} Book
'            {String}   sheetname 
' * @Return : {Worksheet} sheet
' */

CommonVBA-Utility

This is utility module with lots of reuseable functions for VBA

  • Function to get worksheet object
'/**
' * @Purpose:  Get Corresponding sheet
' * @Param  :  {Workbook} Book
'            {String}   sheetname 
' * @Return : Worksheet
' */
Public Function GetSheet(ByVal Book As Workbook, ByVal SheetName As String) As Worksheet
    Dim sheet As Object
    For Each sheet In Book.Worksheets
        If sheet.Name = SheetName Then
            Set GetSheet = sheet
            Exit Function
        End If
    Next
    Set GetSheet = Nothing
    
End Function
  • Function to get last row or column count number
'/**
' * @Purpose:  Return the Last Row Or Column Number
' * @Param  :  {Workbook} Workbook
'            {String}   RowColumn
' * @Return : {Long} RowColumn
' */
Public Function GetLastRowColumn(ws As Worksheet, RowColumn As String) As Long
    Dim LastRowColumn As Long
    Select Case LCase(Left(RowColumn, 1)) 'If they put in 'row' or column instead of 'r' or 'c'.
        Case "c"
            LastRowColumn = ws.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious).Column
        Case "r"
            LastRowColumn = ws.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious).Row
        Case Else
            LastRowColumn = 1
        End Select
    'Return
    GetLastRowColumn = LastRowColumn
End Function
  • Function to get workbook
'/**
' * @Purpose:  Get Corresponding workbook
' * @Param  :  {String}    Name of workbook
' * @Return : {Workbook}  Corresponding workbook if it find the workbook otherwise Nothing
' */
Public Function GetWorkbook(ByVal WorkBookName As String) As Workbook
    Dim EachWorkbook As Object
    
    If Not Trim(WorkBookName) = vbNullString Then
        For Each EachWorkbook In Excel.Workbooks
            If EachWorkbook.Name = WorkBookName Then
                  Set GetWorkbook = EachWorkbook
                  Exit Function
            End If
        Next EachWorkbook
    End If
    
    Set GetWorkbook = Nothing
    
End Function 
'/**
' * @Purpose:  Find out dynamci array allocated or not
' * @Param  :  {Varaint}  Arr
' * @Return : {Boolean}  Return True if Arr is a valid and allocted array
' */
Function IsArrayAllocated(Arr As Variant) As Boolean
        On Error Resume Next
        IsArrayAllocated = IsArray(Arr) And _
                           Not IsError(LBound(Arr, 1)) And _
                           LBound(Arr, 1) <= UBound(Arr, 1)
End Function                           
  • Function to check whether given file exist or not
'/**
' * @Purpose:  Check whether given file exist or not
' * @Param  :  {String} FilePath
' * @Return : {Boolean} True if successful
' */
Public Function FileExist(FilePath As String) As Boolean
Dim GetFile As String
Dim FileExistResult As Boolean
    
     GetFile = Dir(FilePath)
    
     If GetFile <> "" Then
            FileExistResult = True
     End If
    
    'return
    FileExist = FileExistResult
    
End Function
  • Function to check whether input is valid or not
'/**
' * @Purpose:  Check whether TextBox is valid or not
' * @Param  :  {String} ctrlName *Optional
' * @Return : Nothing
' */
Public Sub ValidateForm(Optional ctrlName As String)
Dim ctrl As Object
Dim ControlName As String

    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Then
            ControlName = IIf(ctrlName <> "", ctrlName, ctrl.ControlName)
            If IsNull(ctrl.Value) Then
                ctrl.SetFocus
                MsgBox ControlName & "に入力してから実行してください。"
                End
            End If
        End If
    Next ctrl
End Sub
  • Function to find whether Userform is existed or not
'/**
' * @Purpose:  Find whether Userform is existed or not
' * @Param  :  {String} userform name
' * @Return :  {Boolean} True if userform is exist
' * @References: 'http://custom-designed-databases.com/wordpress/2011/ms-access-vba-does-form-exist-function/
' */

Function isFormLoaded(formName As String) As Boolean

Dim retVal As Boolean

On Error GoTo ErrHandler

retVal = CurrentProject.AllForms.Item(formName).IsLoaded
retVal = (CurrentProject.AllForms.Item(formName).CurrentView = acCurViewDatasheet Or CurrentProject.AllForms.Item(formName).CurrentView = acCurViewFormBrowse)

isFormLoaded = retVal

GoTo ExitRoutine

ErrHandler:

If Err.Number = 2467 Then
  retVal = False
'  Debug.Print “Form is not loaded or does not exist”
End If

ExitRoutine:

isFormLoaded = retVal

End Function

vba-for-everyone's People

Contributors

santosrai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.