120 likes | 237 Views
Different type of input . Use of Basic language with Excel Silvia Patacchini. Excel offers myriad options for referring to workbooks and sheets in your VBA code. 1: Reference the active workbook
E N D
Different type of input Use of Basic language with Excel Silvia Patacchini
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. referring to workbooks and sheets in your VBA code
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
VBA’s ThisWorkbook property is similar to the ActiveWorkbookproperty, 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. Reference the workbook that’s currently running code
Function GetThisWB() As String GetThisWB = ThisWorkbook.Path & "\" & ThisWorkbook.Name End Function An example
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 Reference workbooks in the Workbooks collection
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: Explicitly reference a workbook
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.) An Example
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. Connection Object
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. ADO Programming Code Examples
Function AdoRecordsetExample() Dim rs As New ADODB.Recordset Dim strSql As String ‘** ipotizzol’esistenza di unaconnessioneaperta al db chiamata ‘ CurrentProject.Connection ********** strSql = "SELECT MyField FROM MyTable;" rs.OpenstrSql, CurrentProject.Connection rs.Close Set rs= Nothing End Function Open a recordset
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").CopyFromRecordsetrs .CopyFromRecordsetrst