Homepage | Code With VBA



VBA Data TypesString Used to hold textLongLong integer (whole numbers). -2,147,483,648 to 2,147,483,647IntegerShort integer (whole number). -32,768 to 32,767BooleanTrue or FalseBooleanTrue or FalseDateHolds date data types. 1/1/100 to 12/31/9999Single/DoubleUsed to hold values with decimalsVariantCatch all data type. When an explicit data type is not declared, variant type is assignedVBA Common Operations (Required syntax in bold)If StatementIf numGrade > 90 Then letterGrade = “A”ElseIf numGrade > 80 ThenletterGrade = “B”ElseletterGrade = “F”End IfFor … Next LoopFor x=0 to 49‘Loop Over CodeNext xFor Each … Next LoopFor Each Item In SelectionItem.Offset(0, 1) = Item * 2NextDo … Loop WhileDo.Range(“A1”).Offset(Item,0) = ItemLoop While myBool = TrueDo While … LoopDo While myBool=True.Range(“A1”).Offset(Item,0) = ItemLoopVBA Interacting With UserMessage BoxMsgbox “Hello world”User InputusrInput = InputBox(“Please Enter Your Name”)Comparison OperatorsGreater Than / Greater Than or EqualGreater Than : >Greater Than or Equal: >=Less Than / Less Than or EqualLess Than : <Less Than or Equal: <=Equal / Not EqualEqual : =Not Equal: <>Logical OperatorsOrTrue Or True = TrueTrue Or False = TrueFalse Or False = FalseAndTrue And True = TrueTrue And False = FalseFalse And False = FalseNotNot True = FalseNot False = TrueCommenting CodeSingle Line CommentSingle line comments are created by using an apostorpher (‘) at the beginning of a lineMsgbox “This line of code will execute”‘Msgbox “This line of code will execute”Multi Line CommentsView -> Toolbars -> Edit Referencing Workbooks/Worksheets/RangesWorkbooksWorkbook that contains code: ThisWorkbookUsing the Active Workbook:Active WorkbookUsing Numbered Index:Workbooks(1)Using Workbook Name:Workbooks(“myWkbk”)WorksheetUsing the Active Worksheet:ActiveSheetUsing the Selected Worksheet:Windows.SelectedSheetsUsing Numbered Index:Worksheets(1)Using Worksheet Name:Worksheets(“myWksht”)RangeReference Single Cell:Range(“A1”)Refernce Multiple Adjacent Cells:Range(“A1:C5”)Reference Multiple Non Adjacent CellsRange(“A1:A5, C1:C5”)Using a Named RangeRange(“myRange”)CellsRefernce All CellsWorksheet.CellsRererence Cells with one ParameterCells(3) = “C1”Reference Cells With Two ParametersCells(3,3) = “C3”Cells(3, “E”) = “E3”Useful TipsWith … End WithWith ThisWorkbook.Worksheets(1).Range(“A1”)=MonthEnd WithOffSetFor x=0 to 100.Range(“A1”).Offset(x,0) = RndNext x ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download