VBA Operations Excel

Source: Internet
Author: User

A few small knowledge:

1. Format Cells:

Sht. Cells (4, Pcol). numberformatlocal = "Yyyy-mm-dd hh:mm:ss"

Where the text format is: "@", the General format is: g/Universal format

2. Set the cell to automatic column width or automatic height:

#1, VBA Auto-generated code is: Columns ("A:d"). Entirecolumn.autofit

#2, but the letters A to D, we are not easy to access in VBA, is generally data, so use

Range (Cells (1, 1), cells (1, 40)). Entirecolumn.autofit-----(This method is adapted according to the format of a line)

Or, Range (Columns (1), Columns (40)). Entirecolumn.autofit

3, about the DoEvents event:

It's not good, just record it,

The following code:

 Public SubGettablekey ()DimStrkey as String    DimSql as String    DimstrTableName as String    DimStrpk as String    'Cue box identification    Dimbln as Boolean: bln =False    'SQLsql ="Select A.column_name from User_cons_columns A, user_constraints b where a.constraint_name = B.constraint_name and B.C Onstraint_type = ' P ' and a.table_name = '"    'Table namestrTableName =Gettablename ()'set the table name to uppercasestrTableName =UCase(strTableName)'write back to ExcelActiveSheet.Cells (8,1). Value =strTableName strkey=GETUSERPK ()IfStrkey =""  Then        'Connection String        DimConndb asADODB. ConnectionSetCONNDB =NewADODB. ConnectionDimConnStr as String        'result set        DimDbrst asADODB. RecordsetSetDbrst =NewADODB. Recordset ConnStr=getconnstr () conndb.open connstr'DoEventsProcessfm.show0DoEvents Dbrst.open SQL& strTableName &"'", ConnStrIfDbrst.eof andDbrst.bof ThenUnload PROCESSFMMsgBox "table name is wrong! ", vbcritical,"Error"            Exit Sub        End IfSTRPK= DBRst.Fields.Item (0). Value'write back to ExcelActiveSheet.Cells (6,1). Value =STRPK bln=True        'Closedbrst.close Conndb.closeEnd If    Ifbln ThenUnload PROCESSFMMsgBox "table name updated successfully! ", vbinformation,"Tips"    End IfEnd Sub

The DoEvents can ensure that the window appears normally.

This is still to be researched, but there is not much to be done in VBA to remove large amounts of data.

4. VBA reads the contents of Excel cells:

#1, read the actual value, use Cells.value

#2, read the values displayed by Excel, use Cells.text-------------------but it is important to note that Excel is likely to be displayed as #######, and the column widths need to be adjusted, as described above.

VBA Operations Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.