Example of using VBA to traverse all sheet in Excel

Source: Internet
Author: User

The following is an example of using VBA to traverse Excel sheet, which can be used for batch processing in Excel.

Subinsert_codestring ()DimFS, FT as ObjectDimXlapp asExcel.ApplicationDimXlbook asExcel.WorkbookDimXlsheet asExcel.WorksheetDimSql as StringSQL="truncate TABLE code_string;"&Chr(Ten)DimLine as IntegerSetFS =CreateObject("Scripting.FileSystemObject")SetFT = Fs.createtextfile (Thisworkbook.path &"\"&"insert_codestring"&". SQL")SetXlbook =ThisWorkbook forx =1  toXlBook.Sheets.CountSetXlsheet =xlbook.sheets (x)IfXlsheet.cells (3,1) ="code number"  Then Line=8SQL= SQL &Chr(Ten) &Chr(Ten) &"--"& Xlsheet.cells (4,2) &Chr(Ten)                 DoSQL= SQL &"Insert into code_string (code_type,code_type_desc,code_value,code_desc,code_flag) Values ('"& Xlsheet.cells (3,2) &"', '"& Xlsheet.cells (4,2) &"', '"& Xlsheet.cells (line,2) &"', '"& Xlsheet.cells (line,3) &"' , ' 1 ');"&Chr(Ten) Line= line +1        Loop Until Len(Xlsheet.cells (line,1). Value) =0            End If    NextXSQL= SQL &Chr(Ten) &Chr(Ten) &"commit;"&Chr(Ten) ft. WriteLine (SQL) ft. CloseSetFT = Nothing:SetFS = NothingEnd Sub

Example of using VBA to traverse all sheet in 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.