1 / 12

Different type of input

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

fedora
Download Presentation

Different type of input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  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. 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

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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

More Related