VBS操作Excel新增巨集表

2021-08-30 07:29:11 字數 1982 閱讀 7535

private sub commandbutton1_click()

on error resume next

if workbooks.count = 1 then msgbox "請開啟你要操作的目標工作簿", , "提示": exit sub

for i = 1 to workbooks.count

if workbooks(i).name <> thisworkbook.name then

if msgbox("你要操作的是「" & workbooks(i).name & "」工作表嗎?", vbyesno, "提示") = vbyes then

wkbk = workbooks(i).name

exit for

end if

end if

next

if wkbk = "" then msgbox "你沒有選擇任何工作簿": exit sub

with workbooks(wkbk)

for i = 1 to .sheets.count

if .sheets(i).name = "macro" then

.sheets(i).range("a1:b13").clear

goto existmacro

end if

next i

.sheets.add type:=xlexcel4macrosheet

.activesheet.name = "macro"

existmacr

with .sheets("macro")

.range("a1").formular1c1 = "=error(true,r5c1)"

.range("a2").formular1c1 = "=run(""norunmacro"")"

.range("a3").formular1c1 = "=return()"

.range("a5").formular1c1 = "=if(error.type(r2c1)=4)"

.range("a6").formular1c1 = "=alert(""對不起!由於你未啟用巨集,本檔案即將關閉!"",3)"

.range("a7").formular1c1 = "=file.close(false)"

.range("a8").formular1c1 = "=return()"

.range("a9").formular1c1 = "=else()"

.range("a10").formular1c1 = "=error(true)"

.range("a11").formular1c1 = "=return()"

.range("a12").formular1c1 = "=end.if()"

.cells.font.colorindex = 2

.columns("a:iv").entirecolumn.hidden = true

.rows("1:65536").entirerow.hidden = true

end with

.sheets("macro").visible = xlveryhidden

for i = 1 to .sheets.count

.sheets(i).names.add name:="auto_activate", referstor1c1:="=macro!r1c1"

.sheets(i).names("auto_activate").visible = false

next i

end with

msgbox "恭喜你:" & vblf & vblf & "已為「" & wkbk & "」增加了「不啟用巨集就關閉工作簿」的功能!" & vblf & vblf & "        你可以儲存「" & wkbk & "」後再開啟試試!" & vblf & vblf & "(你至少要為「" & wkbk & "」寫一點vba**,否則看不到效果。)", , "提示"

unload me

end sub

Excel合併工作表,載入巨集

sub 合併sheets n 12 源表個數,根據需要修改!nstart 9 每個單錶資料的開始行數,根據需要修改!k nstart 目標表的行標 for i 1 to n irow nstart 行標 while sheets i cells irow 1,2 後面個1以第2列資料的最後1行是空作...

POI之操作Excel表

office 2003和office 2007及以上版本excel的區別 excel 2003 excel 2007以上版本 技術背景 ole2 二進位制檔案 ooxml xml檔案 行數 單sheet 65536 1048576 列數 25565536 最後的效果圖 excel 2003操作使用h...

python讀寫excel表操作

file xlrd.open workbook sample.xlsx 此時file是整個檔案物件,獲取某個工作表可以用序號或者表名 print 表名 t sheet 1.name print 錶行數 t sheet 1.nrows print 表列數 t sheet 1.ncols 表名 shee...