Tag nrho cov chaw ua haujlwm thov los ntawm Microsoft txhawb automation. Lawv tuaj yeem khiav ua COM servers thiab siv tau los ntawm cov ntawv sau los yog cov ntawv sab nraud. Yog li, koj tuaj yeem sau cov ntaub ntawv mus rau daim ntawv Excel los ntawm vba tsab ntawv.
Tsim nyog
- - ntsia Microsoft Excel daim ntawv thov;
- - Siv qhov muag pom qhov sau / tus sau ntawv.
Cov Lus Qhia
Kauj ruam 1
Tsim ntim tsim los tuav lub vba code. Yog hais tias tsab ntawv yuav tsum muab sau rau hauv ib daim ntawv sau cia, xa cov ntaub ntawv tsim nyog rau Microsoft Excel. Qhib Visual Basic Editor los ntawm nias Alt + F11. Yog tias tsim nyog, tsim ib qho qauv tshiab (Cov khoom hauv Module hauv cov ntawv qhia zaub mov). Qhib ib qho ntawm cov qauv lossis qauv. Tsim tus tuav tes rau koj daim ntawv tswj, lossis tsuas yog ntxiv cov txheej txheem rau tus qauv. Piv txwv li:
Sub kuaj ()
Xaus Sub
Yog tias koj tab tom txhim kho cov ntawv sau nyob ib leeg (ntawd yog, nws yuav khiav hauv Windows Script Host), tom qab ntawd tsuas yog tsim cov ntaub ntawv nrog vbs txuas ntxiv hauv ib phau ntawv kho.
Kauj ruam 2
Hauv tsab ntawv kos rau hauv daim ntawv, ntxiv cov lus tshaj tawm thaum pib ntawm txoj kev:
Dim oWorkbook Raws li Excel. Workbook
Dim oSheet Raws li Excel. Worksheet
Qhov thib ib yog rau khaws cia siv rau Excel phau ntawv lub hom phiaj, thiab qhov thib ob yog rau cov ntawv.
Kauj ruam 3
Pib pauv hloov pauv nrog cov kwv cov lus qhia. Hauv tsab ntawv vbs, tsim ib qho Excel daim ntawv thov kwv (qhov no yuav tso tawm Excel raws li COM server):
Teeb oApplication = CreateObject ("Excel. Application").
Hauv tsab ntawv kos rau hauv cov ntawv, thoob ntiaj teb Daim Ntawv Thov khoom yuav siv los hloov oApplication kuj sib txawv thiab, uas hais txog qhov tam sim no thov kwv. Xaiv cov uas twb muaj lawm lossis qhib ib phau Excel ua haujlwm tshiab. Piv txwv li:
Teeb oWorkbook = Application. Workbooks (1)
Teeb oWorkbook = Application. Workbooks ("Phau Ntawv 1")
Teeb oWorkbook = oApplication. Workbooks. Open ("D: / vic / txog / tmp / test.xls")
Tau txais qhov txuas rau cov ntawv xav tau ntawm phau ntawv:
Teeb oSheet = oApplication. Sheets ("Sheet1")
Kauj ruam 4
Sau cov ntaub ntawv rau Excel los ntawm vba tsab ntawv. Siv cov Ntawv Sau los ntawm Cells, uas yog cov cuab yeej ntawm phau ntawv nplooj ntawv cov khoom siv tau raug txheeb xyuas hauv cov kauj ruam yav dhau los, rau hauv oSheet nce mus nce los. Ib qho piv txwv ntawm kev sau txoj hlua rau ib lub xov tooj yuav zoo li no:
oSheet. Cells (1, 1) = "Txoj hlua yuav sau rau cell A1"
Kauj ruam 5
Hauv tsab ntawv vbs, ntxiv cov lej ntawv txhawm rau khaws cov ntaub ntawv thiab kaw daim ntawv thov Excel:
oWorkbook. Save
oApplication. Quit
Kauj Ruam 6
Txuag thiab tua cov ntawv. Hauv Visual Basic Editor, nias Ctrl + S thiab tom qab ntawd muab tus cursor tso rau hauv lub cev ntawm tus txheej txheem thiab nias F5. Kaw cov vbs tsab ntawv rau disk thiab mam li khiav nws li qub.