Presentation on theme: "Different type of input Use of Basic language with Excel Silvia Patacchini."— Presentation transcript:
Different type of input Use of Basic language with Excel Silvia Patacchini
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.
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
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.
An example Function GetThisWB() As String GetThisWB = ThisWorkbook.Path & "\" & ThisWorkbook.Name End Function
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
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:
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.)
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.
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
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
.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