Excel VBA巨集程式設計

2021-09-10 21:44:39 字數 4776 閱讀 9108

當文案小妹向你抱怨excel函式不夠用,某些特殊操作雖然簡單但繁瑣重複,那麼。。。是時候展現真正的技術了

先推薦個比較好的文件

閹割版的年月日生成

sub getid()

dim str as string

for i = 2 to 5001

str = worksheets(1).cells(i, 13)

worksheets(1).cells(i, 14) = mid(str, 7, 4) + "年"

worksheets(1).cells(i, 15) = "'" + mid(str, 11, 2) + "月" + mid(str, 13, 2) + "日"

next

end sub

mid相當於c#中的sudstring,字串擷取,引數含義也和c#中一樣

sub getid()

dim str as string

for i = 2 to 5001

str = worksheets(4).cells(int((20 * rnd) + 1), 1)

worksheets(1).cells(i, 25) = str

next

end sub

int((20 * rnd) + 1)相當於獲取1-20之間的任意整數

sub randname()

dim randomindex as integer

dim name as string

dim id as string

dim randomindex2 as integer

for i = 1 to 500

randomindex = int(1001 * rnd + 2)

name = worksheets(1).cells(randomindex, 3)

id = worksheets(1).cells(randomindex, 2)

randomindex2 = int((5001 - 2001 + 1) * rnd + 2001)

worksheets(1).cells(randomindex2, 23) = name

worksheets(1).cells(randomindex2, 24) = id

next

end sub

為了生成某個範圍內的隨機整數,可使用以下公式:

int((upperbound - lowerbound + 1) * rnd + lowerbound)

sub asd()

for i = 2 to 5001

worksheets(1).cells(i, 16) = int(650 * rnd + 50) * 100

next

end sub

隨機某地車牌子

sub awwa()

'temp用來隨機,幾種任意的車牌照組合,因為有最多兩個字母一說、、、

dim temp as integer

dim arr()

dim arr2()

arr = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0")

arr2 = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")

'車牌照五位

dim a1 as string

dim a2 as string

dim a3 as string

dim a4 as string

dim a5 as string

for i = 2 to 5001

temp = int(3 * rnd + 1)

select case temp

'前兩位有可能有字母

case 1

a1 = arr(int(35 * rnd + 1))

a2 = arr2(int(25 * rnd + 1))

a3 = int(9 * rnd)

a4 = int(9 * rnd)

a5 = int(9 * rnd)

'2,4有可能為字母

case 2

a1 = int(9 * rnd)

a2 = arr2(int(25 * rnd + 1))

a3 = int(9 * rnd)

a4 = arr(int(35 * rnd + 1))

a5 = int(9 * rnd)

'3,5可能為字母

case 3

a1 = int(9 * rnd)

a2 = int(9 * rnd)

a3 = arr2(int(25 * rnd + 1))

a4 = int(9 * rnd)

a5 = arr(int(35 * rnd + 1))

end select

worksheets(1).cells(i, 4) = "遼a" + a1 + a2 + a3 + a4 + a5

next

'msgbox arr(0)

end sub

判斷語句的寫法

sub asd()

if worksheets(3).cells(1, 6) = "" then

msgbox ("單元格為空")

else

msgbox ("單元格有值")

end if

end sub

sub ss()

dim mit as integer

dim zootemp as integer

dim r as integer

mit = 1

zoomit = 0

zoomtemp = 1

dim zooindex as integer

dim ibooindex as integer

zooindex = 1

ibooindex = 1

for i = 0 to 199

r = int(rnd * 8 + 2)

zoomtemp = mit + r

for j = 1 to 10

mit = mit + 1

'msgbox (str(mit) & (" ") & str(zoomtemp))

if (zoomtemp = mit) then

'msgbox ("已存入200人列表")

worksheets(4).cells(zooindex, 1) = worksheets(3).cells(mit, 1)

worksheets(4).cells(zooindex, 2) = worksheets(3).cells(mit, 2)

worksheets(4).cells(zooindex, 3) = worksheets(3).cells(mit, 3)

worksheets(4).cells(zooindex, 4) = worksheets(3).cells(mit, 4)

worksheets(4).cells(zooindex, 5) = worksheets(3).cells(mit, 5)

zooindex = zooindex + 1

else

'msgbox ("已存入1800人列表")

worksheets(5).cells(ibooindex, 1) = worksheets(3).cells(mit, 1)

worksheets(5).cells(ibooindex, 2) = worksheets(3).cells(mit, 2)

worksheets(5).cells(ibooindex, 3) = worksheets(3).cells(mit, 3)

worksheets(5).cells(ibooindex, 4) = worksheets(3).cells(mit, 4)

worksheets(5).cells(ibooindex, 5) = worksheets(3).cells(mit, 5)

ibooindex = ibooindex + 1

end if

next

next

'msgbox (str(zooindex) & (" ") & str(ibooindex))

end sub

//從5000裡隨機選2000個

sub asd()

dim r as integer

r = 3

for i = 2 to 2001

r = int(rnd * 3 + 1) + r

worksheets(3).cells(i, 1) = worksheets(2).cells(r, 1)

worksheets(3).cells(i, 2) = worksheets(2).cells(r, 2)

worksheets(3).cells(i, 3) = worksheets(2).cells(r, 3)

worksheets(3).cells(i, 4) = worksheets(2).cells(r, 4)

worksheets(3).cells(i, 5) = worksheets(2).cells(r, 5)

next

end sub

Excel VBA程式設計 01 入門

在excel種需要開啟 開發工具 和 啟用所有巨集 兩點 開啟開發工具選項 巨集啟用 選單欄才會有開發工具選項 點選visualbasic,編寫第乙個vba程式 右鍵新建乙個模組以編寫程式 第乙個vba程式 sub 第乙個vba程式 dim 姓名 as string 姓名 inputbox 請輸入你...

Excel VBA 程式設計 1 運算子

運算子對公式中的元素進行特定型別的運算。microsoft excel 包含四種型別的運算子 算術運算子 比較運算子 文字運算子和引用運算子。運算子的型別 算術運算子 若要完成基本的數 算,如加法 減法和乘法,連線數字和產生數字結果等,請使用以下算術運算子 算術運算子 含義 示例 加號 加法運算 3...

程式設計技巧 巨集定義

我們來看看ios的nsassert的定義 if defined nsassertbody define nsassert condition,desc,do pragma pop no extra arg warnings while 0 endif 吊吊的,就是用乙個巨集定義來實現的,那麼我們就不...