Excel VBA and Excel Macros
Excel VBA and Excel Macros
Note
10 April 2020 03:47
10.2
Option Explicit
Dim i As Integer
Dim i As Long Dim LastRow As Integer
Dim lastrow As Long Const FirstRow As Byte = 2
Dim Myvalue As Double Dim Division As String
Const startrow As Byte = 10 Dim remu As Long
Dim newvalue As Long
Range("H10").Select Range("E:F").Clear
If Myvalue > 100 Then newvalue = (Range("F" & i).Value) * 10 Division = Range("d" & i).Value
remu = Range("b" & i).Value
If Myvalue < 0 Then Exit For
ActiveCell = Myvalue
ActiveCell.Offset(1, 0).Select If Division = "CIB" Then
Range("E" & i).Value = remu * 1.25
Range("F" & i).Value = "Moderate"
End If
Next i
Columns.ColumnWidth = 10
Columns.HorizontalAlignment = xlLeft
End Sub
Sub RefertoCell()
'*****Select range*****
'Difference between
'whether to select one cell or four cell (how many cell) depend on Range ("A1") or (A1:A4)
Columns("E:F").ColumnWidth = 10
Rows.AutoFit
Columns.AutoFit
End Sub
Sub FindTheLastRow()
'This method is good if I know the cell of start value and No empty cell extits
Range("K6").Value = Range("A4").End(xlDown).Row
Range("l6").Value = Range("A4").End(xlDown).Rows
Range("K6").Value = Range("A10000").End(xlUp).Row
'Range("K8").Value = Range("A4").End(xlToRight).Column
Range("l8").Value = Range("A4").End(xlToRight).Columns
Range("K9").Value = Range("A4").CurrentRegion.Address
Range("K10").Value = Range("A4").CurrentRegion.Rows.Count
Range("K13").Value = Range("A4").CurrentRegion.Columns.Count
Range("K11").Value = Cells.SpecialCells(xlCellTypeLastCell).Row
Range("K12").Value = Cells.SpecialCells(xlCellTypeLastCell).Column
End Sub
Sub Copy_Variable()
'**************
'Copy method
'**************
Range("I3").CurrentRegion.Clear
Newdata01.Range("A3").CurrentRegion.Copy Range("I3")
'**********************
'PasteSpecial method
'**********************
Range("A15").CurrentRegion.Clear
Range("A15").CurrentRegion.Clear
Newdata01.Range("A3").CurrentRegion.Copy
Range("A15").PasteSpecial xlPasteValues
Range("A15").PasteSpecial xlPasteComments
Range("A15").PasteSpecial xlPasteValuesAndNumberFormats
' You have to put pastespecial twice for the values and comments
' Each line of the pastespecial do one job
Range("A15").PasteSpecial xlPasteValuesAndNumberFormats
'**************************************
' Resize Property with copy Method
'**************************************
' ********* use SPACE and UNDERSCORE to cut and continue the line ************
Range("I16").CurrentRegion.Clear
Newdata01.Range("A3").CurrentRegion.Offset(1, 0) _
.Resize(Range("A3").CurrentRegion.Rows.Count - 1) _
.Copy Range("I16")
'or
Range("I16").CurrentRegion.Clear
Newdata01.Range("A3").CurrentRegion.Offset(1, 0) _
.Resize(Range("A3").CurrentRegion.Rows.Count - 1).Copy _
Range("I16").PasteSpecial xlPasteValues
Range("I16").PasteSpecial xlPasteComments
Range("I16").PasteSpecial xlPasteValuesAndNumberFormats
'Appliation cut copy mode should be false in order to remove copy selection
Application.CutCopyMode = False
'******** If we add more line in the copy region it will paste new line as well under all 3 method
End Sub
Sub ProperlyReferencingWorksheets()
Worksheets("Note").Select
End Sub
Sub ReferenceWorksheet()
' ThisWorkbook means not the active workbooks, ITs mean the Workbook where VBA code is written
' The moment I use the code name for the SHEETS, VBA assume that it is thisworkbook
'Same as
NoteCr.Range("A21").Value = "Lucky"
NoteCr.Range("A1").CurrentRegion.Copy Workbooks("Teste.xlsx").Sheets(1).Range("A1")
Workbooks("Teste.xlsx").Sheets(1).Range("A1").CurrentRegion.Clear
Application.Workbooks.Open "D:\Teste.xlsx"
Workbooks("Teste.xlsx").Sheets(1).Range("A1").CurrentRegion.Clear
NoteCr.Range("A1").CurrentRegion.Copy Workbooks("Teste.xlsx").Sheets(1).Range("A1")
Cells.Rows.AutoFit
Cells.Columns.AutoFit
ActiveWorkbook.Close True
End Sub
• By Default VBA use data type Variant which changes depending on the data types
• Smaller memory used smaller Range used like, Byte use 1 byte memory and range of it is 0 to
255.
• Double is used for when requires high and accurate precision and declaring percentages.
• Variant could be handy when you have a variable that really changing between different data
types, then it's makes sense to use VARIANT as data types
'DIM mytext
' If I did not put anything as data type, VBA will use VARIANT by default as data type
' In Variable name you can use underscore, nothing else like, symbols or space.
'Example
Sub Variable_Define()
Debug.Print lastcolumn
Debug.Print lastRow
End Sub
'Structure
' Dim Sh as Worksheet
'For Each Sh IN thisworkbook.worksheets
'Sh.protect " test"
'Next
'Dim __ as ____
' For Each ___ In _____
'____________
'Next
Option Explicit
Sub protect_WS()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
sh.Protect
Debug.Print sh.Name
Next
End Sub
FORMULA
STRUCTURE
ELSE
RESULT IF BOTH CONDITION ARE FALSE
END IF
Sub if_function()
End If
End If
End If
End Sub
Dim Sh As Worksheet
For Each Sh In ThisWorkbook.Worksheets
End If
Debug.Print Sh.Name
Next
End Sub
Sub caseSte()
Case 0 To 2000
Range("b16").Value = "Very bad"
Case Else
Range("b16").Value = "ATTENTION POINT"
End Select
Case Else
Range("b17").Value = "ATTENTION POINT"
End Select
Option Explicit
Sub monthArray()
'Next I
'or
' if you think your value can be change ,so you can use name manager
For I = 1 To 12
monthArray(I) = Range("myMonth").Cells(I, 1).Value
Next I
Range("C5:N5").Value = monthArray
' check following one, it's will give only JAN in all cell
Range("C6:c17").Value = monthArray
End Sub
Sub lowerboundandHigher()
Next I
Range("E7").Offset(r).Value = MonthArray(I)
r=r+5
Next I
End Sub