『
刪除sheet1
上的單元格區域
a1:d10,
並將其餘單元格左移以填補被刪除單元格的位置
sheet1.range(「a1:d10」).delete shift:=xlshifttoleft
『刪除指定行
range(「1:1」).delete
『刪除指定列
columns(5).delete
『刪除當前行
activecell.entirerow.delete
『刪除工作表中的重複行
sub deletedupes(strsheetname as string,strcolletter as string)
dim strcolrange as string
dim rngcurrentcell as range
dim rngnextcell as range
strcolrange=strcolletter & 「1」
worksheets(strsheetname).range(strcolrange).sort key1:=worksheets(strsheetname).range(strcolrange)
set rngcurrentcell=worksheets(strsheetname).range(strcolrange)
do while not isempty(rngcurrentcell)
set rngnextcell=rngcurrentcell.offset(1,0)
if rngnextcell.value=rngcurrentcell.value then
rngcurrentcell.entirerow.delete
end if
set rngcurrentcell=rngnextcell
loop
end sub
『刪除自定義數字格式
sub deletenumberformat()
msgbox 「
從當前工作簿中刪除
000-00-0000
的數字格式
」activeworkbook.deletenumberformat(「000-00-0000」)
end sub
『清除內容
sub clearcontents()
selection.specialcells(xlcelltypeconstants,23).select
selection.clearcontents
end sub
『清除選定單元格的批註
sub clearcomments()
selection.specialcells(xlcelltypecomments,23).select
selection.clearcomments
end sub
『單元格的插入 『
在資料表上的單元格區域
a1:c5
中插入新單元格,並將該位置上原來的單元格向下移動
sheet1.range(「a1:c5」).insert shift:=xlshiftdown
『在當前單元格上方插入行
sub insertrow()
dim rrow as long
msgbox 「
在當前單元格上方插入一行
」rrow=selection.row
activesheet.rows(row).insert
end sub
『在當前單元格所在列插入列
sub insertcolumn()
dim ccolumn as long
msgbox 「
在當前單元格所在列的左邊插入一列
」ccolumn=selection.column
activesheet.columns(ccolumn).insert
end sub
『在當前單元格上方插入多行
sub insertmanyrow()
msgbox 「
在當前單元格所在行上插入三行
」dim rrow as long ,i as long
for i=1 to 3
rrow=selection.row
actviesheet.rows(rrow).insert
next i
end sub
『在活動工作表的第
1-3行處插入三行
sub insertrows()
msgbox 「
在當前單元格所在行上方插入三行
」activesheet.rows(「1:3」).insert
end sub
『隱藏當前單元格所在的行
sub hiderow()
dim irow as long
msgbox 「
隱藏當前單元格所在的行
」irow=activecell.row
activesheet.rows(irow).hidden=true
msgbox 「
取消隱藏
」activesheet.rows(irow).hidden=false
end sub
『隱藏當前單元格所在的列
sub hidecolumn()
dim icolumn as long
msgbox 「
隱藏當前單元格所在的列
」icolumn =activecell.column
activesheet.rows(icolumn).hidden=true
msgbox 「
取消隱藏
」activesheet.rows(icolumn).hidden=false
end sub
『設定當前所在單元格的行高
sub setrowheight()
msgbox 「
將當前單元格所在的行高設定為
25」dim rrow as long,irow as long
rrow=activecell.row
irow=activesheet.rows(rrow).rowheight
activesheet.rows(rrow).rowheight=25
msgbox 「
恢復到原來的行高
」activesheet.rows(rrow).rowheight=irow
end sub
『設定最合適的行高和列寬
sub autofitrowcol()
rows(「9:9」).select
selection.rows.autofit
columns(「b:d」).select
selection.columns.autofit
end sub
『設定當前單元格的列寬
sub setcolumnwidth()
msgbox 「
將當前單元格所在列的列寬設定為
20」dim ccolumn as long,icolumn as long
ccolumn=activecell.column
icolumn=activesheet.columns(ccolumn).columnwidth
activesheet.columns(ccolumn).columnwidth=20
msgbox 「
恢復至原來的列寬
」activesheet.columns(ccolumn).columnwidth= icolumn
end sub
『鎖定a1:a5
單元格
range(「a1:a5」).locked=true
『接除對
sheet1
中a1:g37
區域單元格的鎖定,以便當該工作表受保護時也可以對這些單元格進行修改
sub unlockedcell()
worksheets(「sheet1」).range(「a1:g37」).locked=false
worksheets(「sheet1」).protect
end sub
『自動對有內容的單個單元格鎖定,對沒有內容的單個單元格解除鎖定
private sub worksheet_selectionchange(byval targe as range)
on error resume next
if targe.cells.count=1 then
『如果目標單元格為空
if targe=」」 then
『解除工作表的保護
me.unprotect(「password」)
target.locked=false
me.protect(「password」)
else
me.unportect(「password」)
『設定單元格的鎖定
target.locked=true
me.protect(「password」)
end if
else
msgbox 「
請選擇乙個單元格
」,vbinformation
activecell.select
end if
end sub
學點VBA之EXCEL 二
1 在vba中建立乙個窗體,初始化其位置,並呼叫之.sub callform userform1.show 顯示窗體 userform1.left 600 顯示位置 userform1.top 180 end sub private subcommandbutton1 click callform ...
Excel使用VBA動態設定列印區域
說明 本設定是實現excel在一開啟的瞬間便實現自動設定列印區域功能。假設本excel的格式是 1.標題是從第一行至第七行 列是從第a列至l列 2.從第八行開始,便是動態變化的行資料 那麼,要設定本動態excel的列印區域 開啟excel,滑鼠右鍵sheet名,點選 檢視 雙擊 thisworkbo...
VBA獲取excel行和列
sub 拷貝資訊 dim workbooktmp as workbook dim worksheettmp as worksheet dim rangtmp as range dim i dim j 當前工作 可以使用thisworkbook 也可以 debug.print thisworkbook...