vba lesson 13

Upload: khoaa7

Post on 14-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 VBA Lesson 13

    1/21

    VBA Lesson 13: VBA for Excelfor the ApplicationApplication is a VBA object, IT IS EXCEL. For example: Application.Quit will closeExcel all together.Exercise 1aStep 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visualbasic editor (VBE).Step 2: Copy the following macro in thecode windowof any sheet. As you can read,you are asking Excel to close itself.Sub testLesson13a1()

    Application.QuitEnd SubStep 3: As you have learned in lesson 7, go to Excel and run the macro from the menubar(Excel before 2007) or the ribbon (Excel since 2007).Step 4: You will be asked if you want to save the workbook. Answer "No" and Excel willclose itself.Exercise 1bIf you do not want to be bothered by the alert to save your workbook you will add a lineof code to the small macro:ActiveWorkbook.Saved = TrueStep 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visualbasic editor (VBE).Step 2: Copy the following macro in thecode windowof any sheet. As you can read,you are asking Excel to close itself but saying first that the workbook has already beensaved.Sub testLesson13a1()

    ActiveWorkbook.Saved = TrueApplication.Quit

    End SubStep 3: Run the macro from Excel as you did with the previous one.

    http://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-7-excel-macro-recorder.htm#running_Excel_Macro_1997http://www.excel-vba.com/vba-prog-1-7-excel-macro-recorder.htm#running_Excel_Macro_1997http://www.excel-vba.com/vba-prog-1-7-excel-macro-recorder.htm#running_Excel_Macro_1997http://www.excel-vba.com/vba-prog-1-7-excel-macro-recorder.htm#running_Excel_Macro_2007http://www.excel-vba.com/vba-prog-1-7-excel-macro-recorder.htm#running_Excel_Macro_2007http://www.excel-vba.com/vba-prog-1-7-excel-macro-recorder.htm#running_Excel_Macro_2007http://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-7-excel-macro-recorder.htm#running_Excel_Macro_2007http://www.excel-vba.com/vba-prog-1-7-excel-macro-recorder.htm#running_Excel_Macro_1997http://www.excel-vba.com/vba-prog-1-4-editor-code.htm
  • 7/29/2019 VBA Lesson 13

    2/21

    Excel will just close itself without asking you anything.There is a word that you can use with Application that will neutralise all the alerts thatExcel can send your way. Discover this word and many others that you can use incombination with Application in thedownloadable course on Excel macros.

    There are many other words that you can use in combination with Application. Amongthem, two important words are:ScreenUpdating (Application.ScreenUpdating)When you do not want to see your screen follow the actions of your VBA procedure(macro), you start and end your code with the following sentences:Application.ScreenUpdating = FalseThen at the end:

    Application.ScreenUpdating = True

    ExerciseStep 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visualbasic editor (VBE).Step 2: Copy the following macro in thecode windowof any sheet. As you can read:starting in cell A1 a value of "99" will be entered in the selected cell then the cursor willmove one cell down to enter "99", repeat the process until the row number of theselected cell is 3000 and come back to cell A1.Sub testLesson13b1()

    Range("A1").SelectDo Until Selection.Row = 3000

    Selection.Value = 99Selection.Offset(1, 0).Select

    LoopRange("A1").Select

    End SubStep 3: Run the macro from Excel as you did with the previous one. Step 4: Remove all the "99" from the cells

    http://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-excel-download.htm
  • 7/29/2019 VBA Lesson 13

    3/21

    Step 5: Copy the following macro in thecode windowof a new workbook and run it.Two lines of code have been added to the previous macro to prevent all the steps of theaction to be seen on the screen.Sub testLesson13b2()

    Application.ScreenUpdating = FalseRange("A1").SelectDo Until Selection.Row = 3000

    Selection.Value = 99Selection.Offset(1, 0).Select

    LoopRange("A1").SelectApplication.ScreenUpdating = True

    End SubStep 6: Run the macro from Excel as you did with the previous one. You will see ablank sheet, no movement whatsoever and then a sheet where cells A1 to A3000 areequal to "99".Sometimes you or the users might want to see the action. Some other times you or theuser do not want to see the action. It is up to you to use the sentence or not. You can even use the pair of sentences (as below) anywhere within a long macro torefresh the screen at significant points in the process. With the pair of sentences youcall for a refreshment withApplication.ScreenUpdating = Trueand then interrupt therefreshment process until the next refreshment with Application.ScreenUpdating= False.Before the end of the macro you will use a final Application.ScreenUpdating= True.The pair of refreshing sentences:Application.ScreenUpdating = TrueApplication.ScreenUpdating = False

    http://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htmhttp://www.excel-vba.com/vba-prog-1-4-editor-code.htm
  • 7/29/2019 VBA Lesson 13

    4/21

    VBA Lesson 14: VBA for Excelfor WorkbooksTo develop a VBA procedure that is triggered by an event relating to the workbook(when you open it, when you save it, when you close it) see theVBA lesson onevents.ThisWorkbookThisWorkbook is the workbook within which your VBA procedure runs. So if you write:ThisWorkbook.SaveThe workbook within which your VBA procedure (macro) runs will be saved. If you want to close the workbook within which your VBA procedure (macro) runswithout saving it you will write these two lines of code:ThisWorkbook.Saved=TrueThisWorkbook.CloseVerifying the existence of a fileWhen you want to verify if a certain file exists on your disk you will use the followingcode that means "If the file "C:\Stuff\toto.xls" does not exist then":If Dir("C:\Stuff\toto.xls") = "" ThenYou could also use a sentence that means "If the file "C:\Stuff\toto.xls" does exist then":If Dir("C:\Stuff\toto.xls") "" ThenIf you are looking in the same folder as the file in which the macro runs you can simplify

    the VBA code:If Dir("toto.xls") "" ThenIn the downloadable tutorial on Excel macros you will find many other usesforDirincluding opening all the files of a folder to generate a consolidated database(whatever the number of files in the folder). You will also learnabout Path,ActiveWorkbook, Windows, Kill, and many other VBA words to work withone or many workbooks.

    http://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/02-19-2010-19-00-very-good/000-Sites%20Web/a-excel-vba/vba-prog-1-10-vba-excel-events.htmhttp://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/02-19-2010-19-00-very-good/000-Sites%20Web/a-excel-vba/vba-prog-1-10-vba-excel-events.htmhttp://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/02-19-2010-19-00-very-good/000-Sites%20Web/a-excel-vba/vba-prog-1-10-vba-excel-events.htmhttp://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/02-19-2010-19-00-very-good/000-Sites%20Web/a-excel-vba/vba-prog-1-10-vba-excel-events.htmhttp://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/02-19-2010-19-00-very-good/000-Sites%20Web/a-excel-vba/vba-prog-1-10-vba-excel-events.htmhttp://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/02-19-2010-19-00-very-good/000-Sites%20Web/a-excel-vba/vba-prog-1-10-vba-excel-events.htm
  • 7/29/2019 VBA Lesson 13

    5/21

    VBA Lesson 15: VBA for Excelfor WorksheetsTo develop a VBA procedure that is triggered by an event relating to the worksheet(when you select it, when you leave it...) see theVBA lesson on events.SheetsYou access a worksheet named " Balance" with:Sheets("Balance").SelectNote that the word "Sheets" is plural and always use the quotes within the parenthesisYou cannot select a sheet that is hidden so you will need to write: Sheets("Balance").Visible= TrueSheets("Balance").Selectand then if you want to hide the sheet again:Sheets("Balance").Visible= FalseThe name of a sheet must not have more than 31 characters and should not includecertain special characters like " ? : \ / [ ]" . If you do not respect these rules yourprocedure will crash.The following lines of code will generate an error message:Sheets("Sheet1").Name= "Balance and Introduction to Numbers" because thereare more than 31 characters including the spacesSheets("Sheet1").Name= " Balance: Introduction" because of the special character :Sheets("Sheet1" ).Name= " " because the name cannot be blankYou can not go directly from a sheet to a cell on another sheet. For example if the activesheet is "Balance" and you want tot go to cell A1 of a sheet named " Results" youcannot write:Sheets("Results").Range("A1").SelectYou must take two steps:Sheets("Results").SelectRange("A1").Select

    http://www.excel-vba.com/vba-prog-1-10-vba-excel-events.htmhttp://www.excel-vba.com/vba-prog-1-10-vba-excel-events.htmhttp://www.excel-vba.com/vba-prog-1-10-vba-excel-events.htmhttp://www.excel-vba.com/vba-prog-1-10-vba-excel-events.htm
  • 7/29/2019 VBA Lesson 13

    6/21

    VBA Lesson 16: Cells, Ranges, Columns and Rows in VBA for ExcelMany beginners start their career using Cells. For example:Cells(1,1).Select means (row 1, column 1) and is the same thingas Range("A1").Select and

    Cells(14,31).Select means (row 14, column 31) and is the sameas Range("AE14").Select.We strongly recommend that you use Range instead ofCells to work with cells andgroups of cells. It makes your sentences much clearer and you are not forced toremember that column AE is column 31.The only time that you will use Cells is when you want to select all the cells of aworksheet. For example:Cells.SelectTo select all cells and then empty all cells of values or formulas you will use:

    Cells.ClearContents

    RangeTo select a single cell you will write: Range("A1").SelectTo select a set of contiguous cells you will use the colon and write:Range("A1:G5").SelectTo select a set of non contiguous cells you will use the comma and write:

    Range("A1,A5,B4").SelectTo select a set of non contiguous cells and a range you will use both the colon and thecomma:Range("A1,A5,B4:B8").SelectOffsetThe Offset property is the one that you will use the most with Range to move aroundthe sheet.To move one cell down (from B2 to B3): Range("B2").Offset(1,0).SelectTo move one cell to the right (from B2 to C2): Range("B2").Offset(0,1).SelectTo move one cell up (from B2 to B1): Range("B2").Offset(-1,0).SelectTo move one cell to the left (from B2 to A2): Range("B2").Offset(0,-1).SelectTo move one cell down from the selected cell:ActiveCell.Offset(1,0).Select

  • 7/29/2019 VBA Lesson 13

    7/21

    As you notice the first argument between the parentheses forOffset is the number ofrows and the second one is the number of columns. So to move from A1 to G6 you willneed:Range("A1").Offset(5,6).SelectYou will use very often the following piece of code . It selects a cell PLUS 4 more to theright to be copied/pasted somewhere else:Range(ActiveCell,ActiveCell.Offset(0,4)).CopyNotice the comma after the first ActiveCell and the double closing parentheses beforethe Copy.

    There are many important VBA words to discover in the downloadableCourse onExcel Macros. You have already read somethingabout Range, Cells, Offset, ActiveCell, read some more about them and about many

    other powerful wordslike CurrentRegion, UsedRange, End(xlDown), Formula, Value,FormulaR1C1, ClearContents, Delete, and many more.

    http://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htm
  • 7/29/2019 VBA Lesson 13

    8/21

    VBALesson 17: Message and Input Boxes(MsgBox, InputBox) in ExcelIn VBA for Excel the message box (MsgBox) is the primary tool to interact with the user.

    For example you might want to tell the user that a long macro has finished running.Exercise 1Step 1: Open a new workbook and use the ALT/F11 keys to move to the Visual BasicEditor.Step 2: Copy/Paste the following macro from here into the code window of any sheet.

    Sub proLessson17a()Sheets("Sheet1").Select

    Range("A1").Value = 695MsgBox "The macro has finished running"

    End SubNotice the space following MsgBox and the use of quotation marks surrounding the text Step 3:Use the ALT/F11 keys to go back to Excel and run the macro proLessson17a.The value 695 is entered in cell A1 and the following message box appears.

    Step 4: Delete the macro in the Visual Basic Editor and the value 695 from cell A1 Exercise 2You might want to tell the user where he will find the result. Step 1: Use the ALT/F11 keys to move to the Visual Basic Editor.Step 2: Copy/Paste the following macro from here into the code window of any sheet.

    Sub proLessson17b()Sheets("Sheet1").SelectRange("A1").Value = 695

  • 7/29/2019 VBA Lesson 13

    9/21

    MsgBox "The result is in cell ""A1"""End Sub

    Notice the space following MsgBox, the use of quotation marks surrounding the textand the double quotation mars around A1 because we want the address to show on the

    message box between quotation marks.

    Step 3:Use the ALT/F11 keys to go back to Excel and run the macro proLessson17b.The value 695 is entered in cell A1 and the following message box appears

    Step 4: Delete the macro in the Visual Basic Editor and the value 695 from cell A1 Exercise 3Instead of telling the user that the value is in cell A1, you might want to tell him what theresult is in the message box itself.Step 1: Use the ALT/F11 keys to move to the Visual Basic Editor.Step 2: Copy/Paste the following macro from here into the code window of any sheet.

    Sub proLessson17c()

    Sheets("Sheet1").SelectRange("A1").Value = 695MsgBox "The result is " & Range("A1").Value

    End SubNotice the space following MsgBox, the use of quotation marks surrounding the text,the space at the end of the text and the spaces surrounding the ampersand. Step 3:Use the ALT/F11 keys to go back to Excel and run the macro proLessson17c.

    The value 695 is entered in cell A1 and the following message box appears

  • 7/29/2019 VBA Lesson 13

    10/21

    Step 4: Close Excel without saving anything.You can use the message box to inform the user. You might also ask the user (with aYes/No message box) if he is sure that he wants a certain critical procedure to run(deleting things).There are many types of message boxes (information, alert, exclamation or questions.Then if you need an input from the user you will start using the input box. For more elaborate message boxes and input boxes see thedownloadable course onExcel macros.

    http://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htm
  • 7/29/2019 VBA Lesson 13

    11/21

    VBA Lesson 18: Excel VBA Vocabulary to Filterand Sort DataWhen Excel recognises youset of data as a databaseit offers you very powerful

    database functionalities like sorting and filtering.Deactivating filtersWhen you work in an Excel database you might want to make sure that all data filtersare off. To this end you will start your procedure with two " If"statements. For examplewith a database starting in cell A1 here are the two sentences: Range("A1" ).Select

    If ActiveSheet.AutoFilterMode = True Then Selection.AutoFilterIf ActiveSheet.FilterMode = True Then ActiveSheet.ShowAllData

    Sorting DataHere is a simplified Excel macro to sort data using a criteria in one field. The followingExcel macro will work with any size database starting in cell A1 and it will work in anyversion of Excel (1997 to 2010).Sub proFilter()Range("A1").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYesEnd SubTry the Excel macro above with a small table like the following (as you have leand howin the basic exercises for beginners):

    Name NumberJones 1Tom 2Barry 3Peter 4

    Here is another simplified Excel macro sorting data using criteria in three different fields. Sub proFilter()

    http://www.excel-vba.com/excel-database.htmhttp://www.excel-vba.com/excel-database.htmhttp://www.excel-vba.com/excel-database.htmhttp://www.excel-vba.com/excel-database.htm
  • 7/29/2019 VBA Lesson 13

    12/21

    Range("A1").Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range( _"B2"), Order2:=xlAscending, Key3:=Range("C2"), Order3:=xlAscending, _Header:=xlYes

    End SubThe code in the two procedures above is much simpler than the following recordedmacro in Excel 2007 and 2010. This recorded macro will not work in earlier versions ofExcel (1997 to 2006).

    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear

    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.AddKey:=Range("A2:A7"), _

    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormalActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add

    Key:=Range("B2:B7"), _

    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormalActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.AddKey:=Range("C2:C7"), _

    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormalWith ActiveWorkbook.Worksheets("Sheet1").Sort

    .SetRange Range("A1:E7")

    .Header = xlYes

    .MatchCase = False

    .Orientation = xlTopToBottom

    .SortMethod = xlPinYin

    .Apply

    End With

    In thedownloadable course on Excel macroswe offer you much more vocabulary towork with Excel databases and also many more simplified macros that can be usedin all versions of Excel. You can you can copy/paste any of them into your ownworkbooks.

    http://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htm
  • 7/29/2019 VBA Lesson 13

    13/21

    VBA Lesson 19: VBA for ExcelVariablesYou will start developing complex and sophisticated programs in Excel and you will startworking with very large sets of data when you discover the variables.

    A variable is an object that you create and in which you can store text, dates, numbersor almost anything else. Why should you use variable? The first good reason is to makeyour code dynamic, to avoid hard coding some values.Hard Coding vs Dynamic CodingYou are hard coding when you write:Workbooks.Open "MyFile.xls"You are dynamically coding when you enter the name of the file in an cell (A1) of yourExcel sheet and you write.varWorkbook=Range("A1").ValueWorkbooks.Open varWorkbook

    At this point you or the user can change the name of the workbook to open in cell A1instead of going to the VBA code in the Visual Basic Editor. You will also create variables to count the number of rows, store the result in a variableand then do something as many time as there are rows. For varCounter = 1 to varNbRows

    Selection.Value=Selection.Value*2Selection.Offset(1,0).select

    NextIn the VBA procedure above the value in each cell is multiplied by 2 then the cell belowis selected. This action is repeated as many times as there are rows in the set of data.

  • 7/29/2019 VBA Lesson 13

    14/21

  • 7/29/2019 VBA Lesson 13

    15/21

    VBA Lesson 20: VBA for ExcelStatementsAmong the VBA statements that you will discover in the downloadable tutorial on Excelmacros, there are the "If" statement including Then, ElseIfand End If, there is the "Do"statement including Loop, Until, While and Exit, there is the "For" statementincluding To, Step, Next and Exit, there is the powerful "Select Case" statementincluding Case,End Select and Exit and other statements.

    A lot of visitors ask us how they can delete the entire lines when a certain cell is empty.For example, in the table below rows 2 and 5 should be deleted:

    First enter xxx where you want the loop to stop (below the last value: B7). Select the cellat the top of the column containing the values to be considered (B1)and run the macro.Sub proDelete()

    Range("B1").SelectDo Until Selection.Value = "xxx"

    If Selection.Value = "" Then

    Selection.EntireRow.DeleteElse

    Selection.Offset(1, 0).SelectEnd If

    LoopRange("A1").Select

    End SubIf you have completed the free exercises "Free Basics", just copy/paste the macro

    above in the Visual Basic editor and run it.Exiting a LoopIn the loop above if you want the loop to stop when it finds the value 99 you can add thisline of code within the loop:If Selection.Value = 99 Then Exit Do

    http://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/07-09-2010/000-Sites%20Web/a-excel-vba/excel-macros-beginners.htmhttp://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/07-09-2010/000-Sites%20Web/a-excel-vba/excel-macros-beginners.htmhttp://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/07-09-2010/000-Sites%20Web/a-excel-vba/excel-macros-beginners.htmhttp://www.excel-vba.com/Documents%20and%20Settings/Pierre%20Leclair/Desktop/Sites%20Web/07-09-2010/000-Sites%20Web/a-excel-vba/excel-macros-beginners.htm
  • 7/29/2019 VBA Lesson 13

    16/21

    Exit allows you to get out of almost anything like:Exit SubExit ForExit Do

  • 7/29/2019 VBA Lesson 13

    17/21

    VBA Lesson 21: Functions in VBA for ExcelThere are three topics in this lesson:- using Excel functions within macros,- using VBA functions within macros,- creating new Excel functions with VBA.

    Excel FunctionsSome of the functions that you find in Excel are available through macros in this form:Range ("C1").Value= Application.WorksheetFunction.Sum(Range("A1:A32"))

    this sentence sums the values of cell A1 to A32 and stores the total in cell C1.

    VBA FunctionsHere are two VBA functions that you will use within your Excel macros:LCase, UCaseThe " If" statements are case sensitive. When you test a string of characters and you donot know if the user will enter upper case or lower case letters, use the LCase or UCase

    functions within your " If" statement so that however the user enters his answer thestatement will work.If LCase(Selection.Value)= "yes" then...orIf UCase(Selection.Value)= "YES" then...

  • 7/29/2019 VBA Lesson 13

    18/21

    VBA Lesson 24: Forms (Userforms) in VBA for ExcelWhen themessage boxor theinput boxare not sufficient any more to communicatewith the user you need to start developing userforms.The form is used to require information from the user to feed the VBA procedure.Different basic controls can be added to the userform they are called:labels,textboxes,combo boxes,list boxes,check boxes, option buttons, frames,commandbuttons,spin buttonsandimages. To learn more about all the controls see lessons26 to 33.Creating a Userform in ExcelUserforms are created in theProject Windowof the Visual Basic Editor. You will alsofind the toolbox that allows you to add controls to your userforms in the Visual BasicEditor.In the Visual Basic Editor you right click in the project window and you will see thismenu appear:

    Go to "Insert" and select "UserForm". You will then see the following:

    http://www.excel-vba.com/vba-code-2-7-message-input.htmhttp://www.excel-vba.com/vba-code-2-7-message-input.htmhttp://www.excel-vba.com/vba-code-2-7-message-input.htmhttp://www.excel-vba.com/vba-code-2-7-message-input.htmhttp://www.excel-vba.com/vba-code-2-7-message-input.htmhttp://www.excel-vba.com/vba-code-2-7-message-input.htmhttp://www.excel-vba.com/vba-forms-3-5-labels.htmhttp://www.excel-vba.com/vba-forms-3-5-labels.htmhttp://www.excel-vba.com/vba-forms-3-5-labels.htmhttp://www.excel-vba.com/vba-forms-3-6-text-boxes.htmhttp://www.excel-vba.com/vba-forms-3-6-text-boxes.htmhttp://www.excel-vba.com/vba-forms-3-6-text-boxes.htmhttp://www.excel-vba.com/vba-forms-3-6-text-boxes.htmhttp://www.excel-vba.com/vba-forms-3-7-combo-boxes.htmhttp://www.excel-vba.com/vba-forms-3-7-combo-boxes.htmhttp://www.excel-vba.com/vba-forms-3-7-combo-boxes.htmhttp://www.excel-vba.com/vba-forms-3-8-list-boxes.htmhttp://www.excel-vba.com/vba-forms-3-8-list-boxes.htmhttp://www.excel-vba.com/vba-forms-3-8-list-boxes.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-4-command-buttons.htmhttp://www.excel-vba.com/vba-forms-3-4-command-buttons.htmhttp://www.excel-vba.com/vba-forms-3-4-command-buttons.htmhttp://www.excel-vba.com/vba-forms-3-4-command-buttons.htmhttp://www.excel-vba.com/vba-forms-3-10-spin-buttons.htmhttp://www.excel-vba.com/vba-forms-3-10-spin-buttons.htmhttp://www.excel-vba.com/vba-forms-3-10-spin-buttons.htmhttp://www.excel-vba.com/vba-forms-3-3-controls-properties.htmhttp://www.excel-vba.com/vba-forms-3-3-controls-properties.htmhttp://www.excel-vba.com/vba-forms-3-3-controls-properties.htmhttp://www.excel-vba.com/vba-prog-1-2-editor-project.htmhttp://www.excel-vba.com/vba-prog-1-2-editor-project.htmhttp://www.excel-vba.com/vba-prog-1-2-editor-project.htmhttp://www.excel-vba.com/vba-prog-1-2-editor-project.htmhttp://www.excel-vba.com/vba-forms-3-3-controls-properties.htmhttp://www.excel-vba.com/vba-forms-3-10-spin-buttons.htmhttp://www.excel-vba.com/vba-forms-3-4-command-buttons.htmhttp://www.excel-vba.com/vba-forms-3-4-command-buttons.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-9-check-option-frame.htmhttp://www.excel-vba.com/vba-forms-3-8-list-boxes.htmhttp://www.excel-vba.com/vba-forms-3-7-combo-boxes.htmhttp://www.excel-vba.com/vba-forms-3-6-text-boxes.htmhttp://www.excel-vba.com/vba-forms-3-6-text-boxes.htmhttp://www.excel-vba.com/vba-forms-3-5-labels.htmhttp://www.excel-vba.com/vba-code-2-7-message-input.htmhttp://www.excel-vba.com/vba-code-2-7-message-input.htm
  • 7/29/2019 VBA Lesson 13

    19/21

    On the right you see the userform that you have just added to your workbook. On theleft is the toolbox with all the controls that you can add to your userform. You can hide

    that toolbox by clicking on the "X" and bring it back by clicking on the toolbox

    icon or by going to the menu bar "View/Toolbox". We will use the toolbox later inthis section.

  • 7/29/2019 VBA Lesson 13

    20/21

    VBA Lesson 25: Userforms Properties and VBACodeIn this lesson we will review some of the properties of the userform, we will develop

    some programming to call the userform and some other programming within theuserform itself.Userforms PropertiesWhen you double click on the userform name in the project window of the Visual BasicEditor the properties windows shows 35 properties of the userform. On this website wewill work with two of them. For the other 33 properties see thedownloadable course onExcel macros (VBA)

    VBA Code within the UserFormIn lesson 9 you have learned about events. The events trigger the macros. There aremany events that happen around the userform. For example, a macro can start whenthe userform is shown (or activated) and another macro can start when a user clicks on

    http://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htm
  • 7/29/2019 VBA Lesson 13

    21/21

    a command button. You will learn all these two events in thedownloadable the tutorialon Excel macros.

    http://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htmhttp://www.excel-vba.com/vba-excel-download.htm