{"id":13942697,"url":"https://github.com/ctechhindi/Excel-VBA-Examples","last_synced_at":"2025-07-20T06:31:45.903Z","repository":{"id":118143442,"uuid":"200884194","full_name":"ctechhindi/Excel-VBA-Examples","owner":"ctechhindi","description":"Excel VBA Script Documentation and Examples","archived":false,"fork":false,"pushed_at":"2019-08-06T16:06:48.000Z","size":2,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-24T16:54:34.150Z","etag":null,"topics":["excel","ms-excel","vba-excel","vba-forms","vba-macros","vba-modules"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ctechhindi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-08-06T16:05:30.000Z","updated_at":"2022-04-04T21:31:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"2f7dbbd3-9fd7-499b-933e-6330eac02207","html_url":"https://github.com/ctechhindi/Excel-VBA-Examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ctechhindi/Excel-VBA-Examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctechhindi%2FExcel-VBA-Examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctechhindi%2FExcel-VBA-Examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctechhindi%2FExcel-VBA-Examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctechhindi%2FExcel-VBA-Examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ctechhindi","download_url":"https://codeload.github.com/ctechhindi/Excel-VBA-Examples/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ctechhindi%2FExcel-VBA-Examples/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266076350,"owners_count":23872741,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["excel","ms-excel","vba-excel","vba-forms","vba-macros","vba-modules"],"created_at":"2024-08-08T02:01:59.562Z","updated_at":"2025-07-20T06:31:45.648Z","avatar_url":"https://github.com/ctechhindi.png","language":null,"funding_links":[],"categories":["Others"],"sub_categories":[],"readme":"# VBA ─ Overview\n\nVBA stands for `Visual Basic for Applications` an event-driven programming language from\nMicrosoft that is now predominantly used with Microsoft office applications such as MS-Excel,\nMS-Word, and MS-Access.\n\nIn Excel window, press `ALT+F11`. \n\n```java\nPrivate Sub say_helloWorld()\n    MsgBox \"Hi\"\nEnd Sub\n```\n\n## Comments\n\n* Keyword `REM` and `'`.\n\n```java\n' Written by : Jeevan Lal\n\nREM Modified by : Jeevan Lal\n```\n\n## Variables/Constants\n\n**Syntax**\n\n\u003e Dim `variable_name` As `variable_type`\n\n\u003e Const `constant_name` As `constant_type` = `constant_value`\n\n### Data Types\n\n* Byte\n* Integer\n* Long\n* Single\n* Double\n* Currency\n* Decimal\n* String\n* Date\n* Boolean\n* Object\n* Variant\n\nExample : \n\n```java\nPrivate Sub Variables()\n\n    Dim name As String\n    name = \"jeevan\"\n\n    Const password As Integer = 1234\n\n    Dim birthDay As Date\n    birthDay = 30 / 10 / 2020\n\n    MsgBox \"Password is \" \u0026 password \u0026 Chr(10) \u0026 \"Name \" \u0026 name \u0026 Chr(10) \u0026 \"Birthday is \" \u0026 birthDay\n\nEnd Sub\n```\n\n```java\nPrivate Sub AnotherMethod()\n\n    Dim a as String : a = \"jeevan lal\"\n    Dim score As Integer, result As String\n    \nEnd Sub\n```\n\n## If Then/Else Statement\n\n```java\nDim score As Integer, result As String\nscore = Range(\"A1\").Value\n\nIf score \u003e= 60 Then result = \"pass\"\n\nRange(\"B1\").Value = result\n```\n\n```java\nDim score As Integer, result As String\nscore = Range(\"A1\").Value\n\nIf score \u003e= 60 Then\n    result = \"pass\"\nElse\n    result = \"fail\"\nEnd If\n\nRange(\"B1\").Value = result\n```\n\n## Workbook and Worksheet Object\n\n```java\nRange(\"A1\").Value = \"Hello\"\n```\n\nbut what we really meant was:\n\n```java\nApplication.Workbooks(\"create-a-macro\").Worksheets(1).Range(\"A1\").Value = \"Hello\"\n```\n\n### Collections\n\n* Using the worksheet name\n\n```java\nWorksheets(\"Sales\").Range(\"A1\").Value = \"Hello\"\n```\n\n*  Using the index number (1 is the first worksheet starting from the left).\n\n```java\nWorksheets(1).Range(\"A1\").Value = \"Hello\"\n```\n\n* Using the CodeName.\n\n```java\nSheet1.Range(\"A1\").Value = \"Hello\"\n```\n\n### Properties and Methods\n\n1. The Add method of the Workbooks collection creates a new workbook.\n\n```\nWorkbooks.Add\n```\n\n2. The Count property of the Worksheets collection counts the number of worksheets in a workbook.\n\n```\nMsgBox Worksheets.Count\n```\n\n## Range Object\n\n```java\nRange(\"B3\").Value = 2\n\nRange(\"A1:A4\").Value = 5\n\nRange(\"A1:A2,B3:C4\").Value = 10\n```\n\n### Named Range\n\n[Web URL](https://www.excel-easy.com/examples/names-in-formulas.html)\n\nExample :\n\n```java\nRange(\"Prices\").Value = 15\n```\n\n### Cells\n\n```java\nCells(3, 2).Value = 2\n```\n\nExcel VBA enters the value 2 into the cell at the intersection of row 3 and column 2.\n\n```java\nRange(Cells(1, 1), Cells(4, 1)).Value = 5\n```\n\n### Declare a Range Object\n\nYou can declare a Range object by using the keywords `Dim` and `Set`.\n\n```java\nDim example As Range\nSet example = Range(\"A1:C4\")\n\nexample.Value = 8\n```\n\n### Select\n\nAn important method of the Range object is the Select method. The Select method simply selects a range.\n\n```java\nDim example As Range\nSet example = Range(\"A1:C4\")\n\nexample.Select\n```\n\n`Note`: To select cells on a different worksheet.\n\n```java\nWorksheets(3).Activate\nWorksheets(3).Range(\"B7\").Select\n```\n\n### Rows/Columns\n\nThe Rows property gives access to a specific row of a range. The Columns property gives access to a specific column of a range.\n\n```java\nDim example As Range\nSet example = Range(\"A1:C4\")\n\nexample.Rows(3).Select\n```\n\n```java\nDim example As Range\nSet example = Range(\"A1:C4\")\n\nexample.Columns(2).Select\n```\n\n### Copy/Paste\n\nThe Copy and Paste method are used to copy a range and to paste it somewhere else on the worksheet.\n\n```java\nRange(\"A1:A2\").Select\nSelection.Copy\n\nRange(\"C3\").Select\nActiveSheet.Paste\n```\n\n```java\nRange(\"C3:C4\").Value = Range(\"A1:A2\").Value\n```\n\n### Clear\n\nTo clear the content of an Excel range, you can use the ClearContents method.\n\n```java\nRange(\"A1\").ClearContents\n\nRange(\"A1\").Value = \"\"\n```\n\n### Count\n\nWith the Count property, you can count the number of cells, rows and columns of a range.\n\n```java\nDim example As Range\nSet example = Range(\"A1:C4\")\n\nMsgBox example.Count\nMsgBox example.Rows.Count\n```\n\n## Loop\n\n### Single Loop\n\n```java\nDim i As Integer\n\nFor i = 1 To 6\n    Cells(i, 1).Value = 100\nNext i\n```\n\n### Double Loop\n\n```java\nDim i As Integer, j As Integer\n\nFor i = 1 To 6\n    For j = 1 To 2\n        Cells(i, j).Value = 100\n    Next j\nNext i\n```\n\n### Triple Loop\n\n```java\nDim c As Integer, i As Integer, j As Integer\n\nFor c = 1 To 3\n    For i = 1 To 6\n        For j = 1 To 2\n            Worksheets(c).Cells(i, j).Value = 100\n        Next j\n    Next i\nNext c\n```\n\n## Do While Loop\n\n```java\nDim i As Integer\ni = 1\n\nDo While i \u003c 6\n    Cells(i, 1).Value = 20\n    i = i + 1\nLoop\n```\n\n```java\nDim i As Integer\ni = 1\n\nDo While Cells(i, 1).Value \u003c\u003e \"\"\n    Cells(i, 2).Value = Cells(i, 1).Value + 10\n    i = i + 1\nLoop\n```\n\n**Explanation**: as long as `Cells(i, 1)`.Value is not empty `(\u003c\u003e means not equal to)`, Excel VBA enters the value into the cell at the intersection of row i and column 2, that is 10 higher than the value in the cell at the intersection of row i and column 1. Excel VBA stops when i equals 7 because Cells(7, 1).Value is empty. This is a great way to loop through any number of rows on a worksheet.\n\n\n## String Manipulation\n\n### Join Strings\n\n```java\nDim text1 As String, text2 As String\ntext1 = \"Hi\"\ntext2 = \"Tim\"\n\nMsgBox text1 \u0026 \" \" \u0026 text2\n```\n\n`Note`: to insert a space, use \" \"\n\n### Left/Right/Mid/Len/Instr\n\n`Note`: To find the position of a substring in a string, use `Instr`.\n\n```java\nMsgBox Left(\"example text\", 4)\nMsgBox Right(\"example text\", 2)\nMsgBox Mid(\"example text\", 9, 2)\nMsgBox Len(\"example text\")\n\n' Note: string \"am\" found at position 3.\nMsgBox Instr(\"example text\", \"am\") \n```\n\n## Date and Time\n\nTo get the current date and time, use the Now function.\n\n```java\nMsgBox Now\n```\n\n### Year, Month, Day of a Date, Hour, Minute, Second\n\n```java\nDim exampleDate As Date\n\nexampleDate = DateValue(\"Jun 19, 2010\")\n\nMsgBox Year(exampleDate)\nMsgBox Hour(Now)\n```\n\n### DateAdd\n\n```java\nDim firstDate As Date, secondDate As Date\n\nfirstDate = DateValue(\"Jun 19, 2010\")\nsecondDate = DateAdd(\"d\", 3, firstDate)\n\nMsgBox secondDate\n```\n\n### TimeValue\n\nThe TimeValue function converts a string to a time serial number. The time's serial number is a number between 0 and 1. For example, noon (halfway through the day) is represented as 0.5.\n\n```java\nMsgBox TimeValue(\"9:20:01 am\")\n```\n\n## Target\n\n```java\nTarget.Address\nTarget.Value\n```\n\n## Array\n\n### One-dimensional Array\n\n```java\nDim Films(1 To 5) As String\n\nFilms(1) = \"Lord of the Rings\"\nFilms(2) = \"Speed\"\nFilms(3) = \"Star Wars\"\nFilms(4) = \"The Godfather\"\nFilms(5) = \"Pulp Fiction\"\n\nMsgBox Films(4)\n```\n\n### Two-dimensional Array\n\n```java\nDim Films(1 To 5, 1 To 2) As String\nDim i As Integer, j As Integer\n\nFor i = 1 To 5\n    For j = 1 To 2\n        Films(i, j) = Cells(i, j).Value\n    Next j\nNext i\n\nMsgBox Films(4, 2)\n```\n\n## Function and Sub\n\nThe difference between a function and a sub in Excel VBA is that a function can return a value while a sub cannot.\n\n### Function\n\n```java\nFunction Area(x As Double, y As Double) As Double\n\n    Area = x * y\n\nEnd Function\n```\n\n**Using Function**\n\n```java\nDim z As Double\n\nz = Area(3, 5) + 2\n\nMsgBox z\n```\n\n### Sub\n\n```java\nSub Area(x As Double, y As Double)\n\n    MsgBox x * y\n\nEnd Sub\n```\n\n**Using Function**\n\n```java\nArea 3, 5\n```\n\n## Application Object\n\nThe mother of all objects is Excel itself. We call it the `Application object`. The application object gives access to a lot of Excel related options.\n\n* WorksheetFunction\n* ScreenUpdating\n* DisplayAlerts\n* Calculation","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctechhindi%2FExcel-VBA-Examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fctechhindi%2FExcel-VBA-Examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fctechhindi%2FExcel-VBA-Examples/lists"}