Excel VBA 資料替換

2022-01-13 15:01:34 字數 1756 閱讀 1361

原始檔.xlsx 有兩列資料,乙個是原始值,一列是替代值

根據實際資料行修改

nvaluecount = 597

修改後的資料填充為了藍色

mypath = thisworkbook.path & "\"

myfile = dir(mypath & "*.xls")

dim nrow as long

dim ncolumn as long

dim sheetname as string

dim cellvalue as string

dim nvaluecount as integer

nvaluecount = 597

dim oldvalue()

dim newvalue()

redim oldvalue(nvaluecount)

redim newvalue(nvaluecount)

dim sht2

with workbooks.open(thisworkbook.path & "\原始檔.xlsx")

set sht2 = .worksheets(1)

with sht2

for i = 2 to nvaluecount + 2

oldvalue(i - 2) = .cells(i, 1)

newvalue(i - 2) = .cells(i, 2)

next

end with

.close

end with

dim cellrange as range

dim curcellrange as range

dim hasf as variant

do while myfile <> ""

if myfile <> thisworkbook.name and myfile <> "原始檔.xlsx" then

with workbooks.open(mypath & myfile)

for each sht in .worksheets

with sht

set cellrange = sht.usedrange

sheetname = sht.name

nrow = cellrange.rows.count

ncolumn = cellrange.columns.count

for i = 1 to ncolumn

for j = 1 to nrow

set curcellrange = .cells(j, i)

if curcellrange.hasformula then

'hasf = rng.formula

exit for

else

'hasf = ""

end if

cellvalue = .cells(j, i)

for k = 0 to nvaluecount

if oldvalue(k) = cellvalue then

.cells(j, i) = newvalue(k)

curcellrange.notetext 'joker'

curcellrange.interior.color = rgb(0, 0, 255)

exit for

end if

next

'.cells(j, i)

next

next

希望對經常用excel處理資料的人有用。

Excel VBA資料排序

每天更新excel vba經典 祝你工作和學習更輕鬆!在如圖1所示資料列表中,需要按總成績從高到低進行排序,示例 如下。sub sortdemo range a1 sort key1 總成績 order1 xldescending,header xlyes endsub 執行sortdemo過程,排...

EXCEL VBA連線SQL資料庫

說明 excel vba連線sql資料庫一般有以下3個步驟 1.vba連線資料庫之前需要建立連線物件 可以採用以下方式 dim cn as object set cn createobject adodb.connection 也可以通過新增引用的方式,具體為 工具 引用 找到microsoft a...

Excel VBA連線ORACLE資料庫

sub linkoracle dim strconn as string 連線字串 dim dbconn as object 連線物件 dim resset as object 查詢結果集 dim db sid,db user,db pass as string sid,使用者名稱,密碼 設定自己的...