Presentation is loading. Please wait.

Presentation is loading. Please wait.

Different type of input Use of Basic language with Excel Silvia Patacchini.

Similar presentations

Presentation on theme: "Different type of input Use of Basic language with Excel Silvia Patacchini."— Presentation transcript:

1 Different type of input Use of Basic language with Excel Silvia Patacchini

2 referring to workbooks and sheets in your VBA code Excel offers myriad options for referring to workbooks and sheets in your VBA code. 1: Reference the active workbook VBA’s ActiveWorkbook property refers to the workbook with the focus. The active workbook may or may not contain the code that’s referencing the active workbook, which is an important distinction. It’s perfectly acceptable to use this property to reference the active workbook from code inside the active workbook. However, it’s invaluable when referencing the active workbook remotely.

3 Sub CloseActiveWBNoSave() 'Close the active workbook without saving. ActiveWorkbook.Close False End Sub Sub CloseActiveWBWithSave() 'Close the active workbook and save. ActiveWorkbook.Close True End Sub This save takes place without additional information, such as the workbook’s name, path, and so on. However, if you need such information, it’s easy enough to glean using ActiveWorkbook. An Example

4 Reference the workbook that’s currently running code VBA’s ThisWorkbook property is similar to the ActiveWorkbook property, but whereas ActiveWorkbook evaluates the workbook with the focus, ThisWorkbook refers to the workbook that’s running the current code. This added flexibility is great because the active workbook isn’t always the workbook that’s running code.

5 An example Function GetThisWB() As String GetThisWB = ThisWorkbook.Path & "\" & ThisWorkbook.Name End Function

6 Reference workbooks in the Workbooks collection The Workbooks collection contains all the open Workbook objects. Using the Workbooks property, you can refer to open workbooks. For instance, the following subprocedure populates a list box in a user form with the names of all open workbooks: Private Sub UserForm_Activate() 'Populate list box with names of open workbooks. Dim wb As Workbook For Each wb In Workbooks ListBox1.AddItem wb.Name Next wb End Sub

7 Explicitly reference a workbook If you know the name of the workbook you want to reference, an explicit reference might be the most efficient method. Although an explicit reference is easy, it does require a stable situation. If the name of the workbook changes, but the possibilities are known, you can still use an explicit reference by passing the workbook’s name. For example, the following subprocedure activates an open workbook, as determined by the passed argument, wbname:

8 An Example Function ActivateWB(wbname As String) 'Open wbname. Workbooks(wbname).Activate End Function To execute it, you simply pass the name of the workbook you want to activate as follows: ActivateWB(“pricing.xlsm") (You must include the.xlsm extension.)

9 Connection Object The ADO Connection Object is used to create an open connection to a data source. Through this connection, you can access and manipulate a database. If you want to access a database multiple times, you should establish a connection using the Connection object. You can also make a connection to a database by passing a connection string via a Command or Recordset object. However, this type of connection is only good for one specific, single query.

10 ADO Programming Code Examples how to use the ADO library to list and manipulate the objects in Access: ADO provides only limited ways to manipulate the data structure (typically via DDL query statements), unless you also use the ADOX library which provides the extensions to get to the database catalog.DDLADOX

11 Open a recordset Function AdoRecordsetExample() Dim rs As New ADODB.Recordset Dim strSql As String ‘** ipotizzo l’esistenza di una connessione aperta al db chiamata ‘ CurrentProject.Connection ********** strSql = "SELECT MyField FROM MyTable;" rs.Open strSql, CurrentProject.Connection rs.Close Set rs = Nothing End Function

12 .CopyFromRecordset rst Copies the contents of an ADO or DAO Recordset object onto a worksheet, beginning at the upper-left corner of the specified range. If the Recordset object contains fields with OLE objects in them, this method fails. This example copies the field names from a DAO Recordset object into the first row of a worksheet and formats the names as bold. The example then copies the recordset onto the worksheet, beginning at cell A2. For iCols = 0 to rs.Fields.Count - 1 ws.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name Next ws.Range(ws.Cells(1, 1), _ ws.Cells(1, rs.Fields.Count)).Font.Bold = True ws.Range("A2").CopyFromRecordset rs

Download ppt "Different type of input Use of Basic language with Excel Silvia Patacchini."

Similar presentations

Ads by Google