Use Excel + VBA + SQL Server for Data Processing

Source: Internet
Author: User
Tags close close
Sub test () 'defines the process name
Dim I as integer, J as integer, sht as worksheet 'I, J is an integer variable, sht is an Excel worksheet object variable, pointing to a worksheet
Dim cn as new ADODB. Connection 'defines the data link object and saves the Connection database information. Add ADO reference first.
Dim RS as new ADODB. recordset 'defines the record set object and saves the data table
Dim strcn as string, strsql as string 'string variable

Strcn = "provider = sqloledb; server = server name or IP address; database = database name; uid = user login name; Pwd = password;" 'define database link string

'The following statement reads data from the data table and saves it to the Excel Worksheet: draw two tables and imagine that the worksheet is a two-dimensional table, and the record set is also a two-dimensional table.
Strsql = "select Field 1, Field 2 from table name" 'defines the SQL query command string
CN. Open strcn establishes a connection with the database. If the connection is successful, the connection object CN is returned.
Rs. Open strsql, CN 'execute the SQL commands contained in strsql and save the results in the RS record set object
I = 1
Set sht = thisworkbook. worksheets ("sheet1") 'points the sht to the sheet 1 worksheet of the current workbook.
Do while not Rs. EOF 'when the Data Pointer is not moved to the end of the record set, the following operations are cyclically performed:
Sht. cells (I, 1) = RS ("Field 1") 'Save the value of field 1 of the current record to column 1st of row I of the sheet1 Worksheet
Sht. cells (I, 2) = RS ("Field 2") 'Save the value of current field 2 to Column 2nd of row I of sheet1 Worksheet
Rs. movenext move the pointer to the next record.
I = I + 1' I + 1. You are going to save the values of the relevant fields of the next record to the next row in the worksheet.
Loop
Rs. Close close the record set. At this point, the program will save the fields 1 and 2 of a data table in columns 1st and 2 of the sheet 1 in the Excel worksheet. The number of rows equals to the number of records in the data table.

'The following statement reads the Excel worksheet data and saves it to the database after simple calculation. Here, some variables in the above program are used.
'Assume that you read the existing data in the 5th columns and 500th columns from the worksheet sheet1 8th rows to 9th rows, multiply them, and store the data in a database table.
Strsql = "" 'clear the variables defined above
For I = 5 to 500 'the cycle starts, And I starts from 5 to 500.
Strsql = strsql & "insert into Table Name (field) values (" & sht. cells (I, 8) * sht. cells (I, 9) & ");" 'constructs an SQL command string
Next
'The SQL command string is generated. The stored content is roughly insert into Table Name (field) values (value 1); insert into Table Name (field) values (value 2 );
Cn.exe cute strsql' execute this SQL command string. If the SQL command is correct, 501 records will be added to the database. You can also use Rs. Open strsql and CN to execute
CN. Close 'Close the database link and release resources
End sub

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.