vba中Range物件的Replace方法

2022-01-15 04:04:46 字數 3178 閱讀 7767

replace是range物件的乙個方法,用於單元格替換.

'指定lookat引數為whole,從而避免將21等包含2的數字也替換掉'

'指定lookat引數為whole,從而避免將21等包含2的數字也替換掉'

range("b2:e4").replace what:=2, replacement:=3, lookat:=xlwhole, replaceformat:=true

end sub

sub findlastrow()

dim r as range

'set r = range("b2").end(xldown)'

set r = cells(rows.count, 2).end(xlup)

msgbox r.row

end sub

sub findtablelastnum()

dim r as range, maxrow as long, i as long

'迴圈掃瞄第2列到5列'

for i = 2 to 5

'獲取第i列最後乙個資料的行號'

set r = cells(rows.count, i).end(xlup)

'如果該行號大於之前找到的最大行號,則更新最大行號'

if r.row > maxrow then maxrow = r.row

next i

msgbox "最後乙個資料在第" & maxrow & "行"

end sub

sub lastrow()

dim i as long

i = 3

do while cells(i, 2) <> "" and i < rows.count

i = i + 1

loop

if cells(rows.count, 2) = "" then i = i - 1

msgbox "最後一行是" & i

end sub

sub lastrowtwo()

dim i as long, r as range

set r = activesheet.usedrange

i = r.row + r.rows.count - 1

msgbox "最後一行是" & i

end sub

'找到乙個**的最後乙個單元格'

sub usespecialcell()

dim r as range

set r = cells.specialcells(xlcelltypelastcell)

msgbox r.row

end sub

sub usespecialcelltwo()

dim r as range

'按行序,從後向前查詢'

'xlrows'

set r = cells.find("*", after:=range("a1"), searchorder:=xlcolumns, searchdirection:=xlprevious)

if r is nothing then

msgbox "**中沒有資料"

else

'msgbox r.row'

msgbox r.column

end if

end sub

'找到最後乙個單元格,包括隱藏的,有空格的'

sub usedo()

dim i as long

i = rows.count

do while i > 0

if cells(i, 2) <> "" then exit do

i = i - 1

loop

msgbox "最後一行是第" & i & "行"

end sub

sub demo1()

dim i as long, k as long, name as string, amount as long

for i = 2 to 9

name = cells(, 2): amount = cells(i, 4)

for k = 3 to 5

if cells(k, 6) = name and amout > cells(k, 7) then

cells(i, 1).interior.color = vbred

exit for

end if

next k

next i

end sub

VBA系列 4 VBA中的物件和集合

vba的物件是以分層的結構組織的,本節介紹excel中的物件和集合 關於集合 在vba裡邊有乙個重要概念,即集合。集合是一組屬於同一類的物件,集合 本身也是物件。如workbooks是當前開啟的所有workbook物件的集合 worksheets是包含在特定workbook物件中的所有workshe...

Swift 中的Range型別和 Range運算子

swift中有五個最常用的range型別 closedrange a b range a 對應的,有五個range運算子,用來定義上面的range型別 closed range operator a b half open range operator a closedrange型別代表乙個閉區間 ...

VBA中Dictionary物件使用小結

自 vba中dictionary物件使用小結 dim dict 建立dictionary set dict createobject scripting.dictionary 增加專案 dict.add a 300 dict.add b 400 dict.add c 500 統計專案數 n dict...