Basic Operation in VBA
As we Know that VBA stands for Visual Basic Application developed by Microsoft and used for developing such type of Applications that control Excel and other Microsoft Applications like word and Power Point.So, from above descriptions you will have understood that how much VBA is useful in our day to day life now a days Excel becomes part of business and our life also for storing any information and managing information we generally prefer to Excel.So in this session we discussed five basic Operations in VBA.
These Operations are :
During any application development it is general work that we copy attributes of sheet and paste inside another sheet.for this purpose we have to need to count last row inside excel.
Below is shown a Excel sheet which contains my daily expenses like food and travel.
Now in this case if I have to perform any Operation then it must be compulsory that I have to count number of rows for performing any type of operations like counting total monthly.
expenses or weekly expenses or compare with other expenses and many other problems.
Hence at First find last Row using VBA code for this we at go to developer tab inside Excel as above inside excel sheet in between view and Load Test,then add a module and copy below code and paste inside module
Code
Sub lastRowFind()
Dim lastRow As Integer
lastRow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
MsgBox lastRow
End Sub
After go to Excel sheet Press key Alt + F8 then window open
Select lastRowfind and click run button and we obtain OutPut
OutPut :
2.)Find Last Column
Finding last column in VBA contains similar importance as last row contains in this case also we have to follow same procedure and write VBA code inside VBA module as given below.
Code :
Sub lastColumnFind()
Dim lastCol As Integer
lastCol = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
MsgBox lastCol
End Sub
Now again come to Excel Sheet Press Alt + F8 and select lastColumnFind and press on run button
and we obtain outPut
OutPut:
Now we come to our third task i.e
3.) Select Complete Sheet
For selecting complete sheet we will have to use select method and inside below code we work on Active cell i.e Sheet will be completely selected from where cell is Active (here active means i.e any cell inside which our cursor is blinking.
In this case suppose our cursor is inside cell(2,2) then complete sheet will be selected after 2,2
Code :
Sub selectCompleteSheet()
ThisWorkbook.Sheets("Sheet2").Range(ActiveCell, ActiveCell.End(xlToRight).End(xlDown)).Select
End Sub
Now we come our Forth task to Print Name of Active Worksheet
4.)Print Name of Active WorkSheet
Code:
Sub printActiveWorksheet()
MsgBox ActiveSheet.Name
End Sub
OutPut :
Now our fifth and Final task to Print Name of WorkBook
5.)Print Name of WorkBook
Code:
Sub PrintWorkbookName()
MsgBox ActiveWorkbook.FullName
End Sub
OutPut:
Download sheet click here
These Operations are :
- )Find Last Row
- )Find Last Column
- )Select Complete sheet
- )Print name of Worksheet
- )Print name of workbook
During any application development it is general work that we copy attributes of sheet and paste inside another sheet.for this purpose we have to need to count last row inside excel.
Below is shown a Excel sheet which contains my daily expenses like food and travel.
expenses or weekly expenses or compare with other expenses and many other problems.
Hence at First find last Row using VBA code for this we at go to developer tab inside Excel as above inside excel sheet in between view and Load Test,then add a module and copy below code and paste inside module
Code
Sub lastRowFind()
Dim lastRow As Integer
lastRow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
MsgBox lastRow
End Sub
Select lastRowfind and click run button and we obtain OutPut
OutPut :
2.)Find Last Column
Finding last column in VBA contains similar importance as last row contains in this case also we have to follow same procedure and write VBA code inside VBA module as given below.
Code :
Sub lastColumnFind()
Dim lastCol As Integer
lastCol = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
MsgBox lastCol
End Sub
and we obtain outPut
OutPut:
3.) Select Complete Sheet
For selecting complete sheet we will have to use select method and inside below code we work on Active cell i.e Sheet will be completely selected from where cell is Active (here active means i.e any cell inside which our cursor is blinking.
Code :
Sub selectCompleteSheet()
ThisWorkbook.Sheets("Sheet2").Range(ActiveCell, ActiveCell.End(xlToRight).End(xlDown)).Select
End Sub
Now we come our Forth task to Print Name of Active Worksheet
4.)Print Name of Active WorkSheet
Code:
Sub printActiveWorksheet()
MsgBox ActiveSheet.Name
End Sub
OutPut :
5.)Print Name of WorkBook
Code:
Sub PrintWorkbookName()
MsgBox ActiveWorkbook.FullName
End Sub
OutPut:
Thanks for the sharing information about VBA basic development. It was very useful to me. Keep up the good work!
ReplyDelete