Saturday, 30 September 2017

FOR EACH LOOP


FOR EACH LOOP


for each loop
For Each element In Group
   [statement 1]
   [statement 2]
   ....
   [statement n]
   [Exit For]
   [statement 11]
   [statement 22]
Next

Example 1

Private Sub Constant_demo_Click() 
   'fruits is an array
   fruits = Array("apple", "orange", "cherries")
   Dim fruitnames As Variant

   'iterating using For each loop.
   For Each Item In fruits
      fruitnames = fruitnames & Item & Chr(10)
   Next
  
   MsgBox fruitnames
End Sub

Example 2
For Each cl In ActiveSheet.Range("A1:A10")
 'Statements to be executed inside the loop 
Msgbox cl.value
Next cl



Example 3

‘if cell is greater then 0 cell will update as blank
For Each cl In ActiveSheet.Range("A1:C11") 
 If InStr(cl.Value, "a") > 0 Then 
  cl.Value = "" 
 End If 

Next cl

Tuesday, 19 September 2017

VBA LOOPS

If you are writing a VBA program and you want to execute the same task multiple times then you can do this by using VBA for loop.

For Loops

For counter = start To end [Step stepcount]
   [statement 1]
   [statement 2]
   ....
   [statement n]
   [Exit For]
   [statement 11]
   [statement 22]
   ....
   [statement n]
Next

Example 1

‘Display message box  1 to 10 times

Private Sub example1()
   Dim a As Integer
   a = 10
  
   For i = 0 To a
      MsgBox "The value is i is : " & i
   Next
End Sub


Sub exaple2()
'Fills cells A1:A56 with values of X by looping' --- Comment
'Increase value of X by 1 in each loop' --- Comment

Dim X As Integer
    For X = 1 To 56
        Range("A" & X).Value = X
    Next X

End Sub

Sub Example3()
' Fills cells B1:B56 with the 56 background colours' --- Comment
Dim X As Integer
    For X = 1 To 56
        Range("B" & X).Select
        With Selection.Interior
            .ColorIndex = X
            .Pattern = xlSolid
        End With
    Next X
End Sub


CONTINUE

Saturday, 16 September 2017

VBA DATA TYPES



There are many VBA data types, which can be divided into two main categories, namely numeric and non-numeric data types.

Type
Range of Values
Byte
0 to 255
Integer
-32,768 to 32,767
Long
-2,147,483,648 to 2,147,483,648
Single
-3.402823E+38 to -1.401298E-45 for negative values
1.401298E-45 to 3.402823E+38 for positive values.
Double
-1.79769313486232e+308 to -4.94065645841247E-324 for negative values
4.94065645841247E-324 to 1.79769313486232e+308 for positive values.
Currency
-922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal
+/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use
+/- 7.9228162514264337593543950335 (28 decimal places).

Type
Range of Values
String (fixed length)
1 to 65,400 characters
String (variable length)
0 to 2 billion characters
Date
January 1, 100 to December 31, 9999
Boolean
True or False
Object
Any embedded object
Variant (numeric)
Any value as large as double
Variant (text)
Same as variable-length string


Wednesday, 13 September 2017

VBA Range Objects Examples

RANGE OBJECT SOME EXAMPLES

  Rem Just in case there is anything on the
    Rem worksheet, delete everything
    Range("A:K").Delete
    Range("1:20").Delete
   
    Rem Create the sections and headings of the worksheet
    Range("B2") = "Georgetown Dry Cleaning Services"
    Range("B2").Font.Name = "Rockwell Condensed"
    Range("B2").Font.Size = 24
    Range("B2").Font.Bold = True
    Range("B2").Font.Color = vbBlue
   
    Range("B5") = "Order Identification"
    Range("B5").Font.Name = "Cambria"
    Range("B5").Font.Size = 14
    Range("B5").Font.Bold = True
    Range("B5").Font.ThemeColor = 5
   
    Rem To draw a thick line, change the bottom
    Rem borders of the cells from B5 to J5
    Range("B5:J5").Borders(xlEdgeBottom).LineStyle = xlContinuous
    Range("B5:J5").Borders(xlEdgeBottom).Weight = xlMedium
   
    Range("B6") = "Receipt #:"
    Range("D6:F6").Borders(xlEdgeBottom).LineStyle = xlContinuous
    Range("D6:F6").Borders(xlEdgeBottom).Weight = xlHairline
   
    Range("G6") = "Order Status:"
    Range("I6:J6").Borders(xlEdgeBottom).LineStyle = xlContinuous
    Range("I6:J6").Borders(xlEdgeBottom).Weight = xlHairline
   
    Range("B7") = "Customer Name:"
    Range("D7:F7").Borders(xlEdgeBottom).LineStyle = xlContinuous
    Range("D7:F7").Borders(xlEdgeBottom).Weight = xlHairline
   
    Range("G7") = "Customer Phone:"
    Range("I7:J7").Borders(xlEdgeBottom).LineStyle = xlContinuous
    Range("I7:J7").Borders(xlEdgeBottom).Weight = xlHairline
   
    Rem To draw a thick line, change the bottom
    Rem borders of the cells from B5 to J5
    Range("B8:J8").Borders(xlEdgeBottom).LineStyle = xlContinuous
    Range("B8:J8").Borders(xlEdgeBottom).Weight = xlThin
   
    Range("B9") = "Date Left:"

    Range("D9:F9").Borders(xlEdgeBottom).LineStyle = xlContinuous

Tuesday, 12 September 2017

VBA Application Objects tips

Useful Application  Objects

***Use Screen Updating

Application.ScreenUpdating = False to deactivate
Application.ScreenUpdating = True to activate

**Turn Off automatic calculations

Application.Calculation = xlCalculationManual 'To turn off the automatic calculation
Application.Calculation = xlCalculationAutomatic 'To turn On the automatic calculation

***use this code for calculations whenever you want

ActiveSheet.Calculate ' To calculate the formulas of Active Worksheet
Application.Calculate ' To calculate the formulas of Active workbook or all workbooks in current

***To turn off on cut copy mode
Application.cutcopymode=False or true
Application.Displayhorizontalbar=True or false?Application.Version

***To Turn on/off alerts
Application.displayalerts=true or false
Application.displaycommentindicator
Application.dataentrymode=false or trueApplication.displayformulabar=true or false
Application.displayfullscreen=true or false




Friday, 8 September 2017

VBA Class Worksheet Objects

Worksheet Objects
The Worksheet object is a member of the Worksheets collection and contains all the Worksheet objects in a workbook.

Example
Worksheets(1).visible=false or true
Workshets(“sheetname”).visible=true or false




Range Objects
Range Objects represent a cell, a row, a column, or a selection of cells containing one or more continuous blocks of cells.
Example
Worksheets(1).range(“a3”).value=”date”

Worksheets(“sheetname”).value=”time”
TO DELETE RANGE

    Range("A:K").Delete
    Range("1:20").Delete

FONT

    Range("B2") = "Georgetown Dry Cleaning Services"
    Range("B2").Font.Name = "Rockwell Condensed"
    Range("B5") = "Order Identification"
    Range("B5").Font.Name = "Cambria"

CONTINUE

Thursday, 7 September 2017

VBA Class

Day 3

Windows  Excel objects

Application Objects
Workbooks Objects
Worksheets Objects
Range Objects


Application Objects

The Application object consists of the following −
  • Application-wide settings and options.
  • Methods that return top-level objects, such as ActiveCell, ActiveSheet, and so on.
 
Application Object example
 
Set xlapp = Createobject(“Excel.Sheet”)
Xlapp.application.workbooks.open “C:\test.xls”
Application.windows(“text.xls”).activate
 
Application.activecell.font.bold=true


Workbook Objects

The Work book object is member of the Workbooks collection and contains all the Workbook objects currently in Excel.

Example:

Workbooks.Close

Continue---

Wednesday, 6 September 2017

Shortcut for VBA

DAY 2
Short cuts VBE

·         Alt+F4  - close VBE
·         Alt+F5   - Run Error Handler
·         Alt+F6   - Swithch between last 2 windows
·         Alt+F7   - Step Error Handler
·         Alt+F11 - return to application
·         Alt+A     - AddIns Menu
·         Alt+D     - Debug Menu
·         Alt+E     - Edit Menu
·         Alt+f      - File Menu
·         Ctrl+F2  - Focus Object menu
·         Ctrl+F4  - close window
·         Ctrl+F8  - run to cursor
·         Ctrl+F10- Activate menu bar
·         Ctrl+A   - Select All
·         Ctrl+E    - Export Module
·         Ctrl+F    - Find
·         Ctrl+G   - Immediate Window
·         Ctrl+H   - Replace
·         Ctrl+I     - Turn On Quick Info
·         Ctrl+J     - List Members
·
·         Ctrl+L    - Show call stack
·         Ctrl+M  - Import File
·
·         Ctrl+N   - New Line
·         Ctrl+P    - Print
·         Ctrl+R  - Project Window
·
·         Ctrl+S    - Save
·         Ctrl+T    - Show Availabe Components
·         Ctrl+V   - Paste
·         Ctrl+downarrow-Next Procedure
·
·         Ctrl+spacebar-Turn on complete word
·         Ctrl+Tab - Cycle
VBE
Abbreviated as VBE, Visual Basic Editor is the tool used to
create, modify and maintain Visual Basic for Applications (VBA)
procedures and modules in Microsoft Office applications. The
Visual Basic Editor is included with most Office programs.

To Display Developer Tab 2007

To display the Developer tab, click on the Microsoft Office
button in the top left of the Excel window and then click on the
Excel Options button. When the Excel Options window appears,
click on the Popular option on the left. Select the option
called "Show Developer tab in the Ribbon". Then click on the OK
button.


To Display Developer Tab 2010

To display the Developer tab, follow these steps: Click the
Microsoft Office Button, and then click Excel Options. Click
Personalize. Click to select the Show Developer tab in the
Ribbon check box.

To Display Developer Tab 2013


follow these two steps:
Choose File→Options or press Alt+FT to open the Excel Options
dialog box. Click the Customize Ribbon tab, select the Developer check box
under Main Tabs in the Customize the Ribbon list box on the

right side of the dialog box, and then click OK.

JOTHIDAM KARPOM VANGA