使用VBA實現資料統計

2021-08-03 16:57:02 字數 4117 閱讀 4957

工作中經常需要用到excel,有時候會統計和計算大量資料,如果用人工來做會耗時耗力,而且容易出錯。如果使用excel的公式,稍微複雜一點的需求就沒辦法實現。

excel支援vba程式設計,所以用程式來實現一些複雜的需求,看起來是個不錯的選擇。

這裡以乙個具體的需求來講解vba程式設計

有乙個原始資料,裡面記錄了每乙個問題由誰處理的,耗時多少。這些問題,有一些是直接由某人處理,有一些是多個人處理過。要求是統計每個人直接處理了多少問題,參與處理了多少問題,花費了多少時間,並計算平均處理乙個問題需要多少時間

先在開啟的excel介面,按alt+f11,撥出vba編輯器。按照下圖方式插入模組,在模組上雙擊,便可以開始編輯**

首先要定義乙個函式,所有的邏輯都在函式裡面實現

sub test()

....

end sub

如上,定義了函式test,所有的邏輯就在sub和end sub之間實現

接著要定義一些變數

dim mantisrow as integer

dim name as string

dim resolvedmantisnum as integer

dim modifymantisnum as integer

dim resolvedmantistime as single

dim modifymantistime as single

dim resolvedmantis as boolean

dim summaryrow as integer

mantisrow表示原始資料的一行

resolve開頭的變數,表示直接解決的問題

modify開頭的變數,表示參與解決的問題

接著,我要sheet2中原有的資料清除,sheet2就是用於填充統計和計算後的資料

sheets("sheet2").range("b2:g11").clearcontents
下面這整個一大段,就是邏輯實現了

' 從sheet2中拿出人名與sheet1中比對

for summaryrow = 2 to sheets("sheet2").cells(65536, 1).end(xlup).row

name = sheets("sheet2").cells(summaryrow, 1)

resolvedmantisnum = 0

modifymantisnum = 0

resolvedmantistime = 0

modifymantistime = 0

' sheets("sheet1").cells(65536, "g").end(xlup).row 通過g列來判斷總行數,因為第一列會有空行的情況導致資料不准

for mantisrow = 2 to sheets("sheet1").cells(65536, "g").end(xlup).row

if sheets("sheet1").cells(mantisrow, 1) <> "" then ' 如果第一列取出來的是mantisid,接著對人名,如果匹配,那麼這個mantis就是被他resolved的

resolvedmantis = false

if sheets("sheet1").cells(mantisrow, "c") = name then

resolvedmantisnum = resolvedmantisnum + 1

resolvedmantis = true

end if

else ' 如果第一列取出來的是空值,那根據resolvedmantis判斷此問題是否被該人resolved的,如果不是就看看修改記錄裡面有沒此人,如果有,那麼就要記錄到modify中

if sheets("sheet1").cells(mantisrow, "g") = name then

if resolvedmantis then

resolvedmantistime = resolvedmantistime + sheets("sheet1").cells(mantisrow, "h")

else

modifymantistime = modifymantistime + sheets("sheet1").cells(mantisrow, "h")

modifymantisnum = modifymantisnum + 1

end if

end if

end if

next

sheets("sheet2").cells(summaryrow, "b") = resolvedmantisnum ' resolved掉的mantis數量

sheets("sheet2").cells(summaryrow, "c") = modifymantisnum ' 修改過的mantis數量

sheets("sheet2").cells(summaryrow, "d") = resolvedmantistime ' resolved mantis的總時間

sheets("sheet2").cells(summaryrow, "e") = modifymantistime ' 修改mantis的總時間

sheets("sheet2").cells(summaryrow, "f") = modifymantistime + resolvedmantistime ' 處理mantis使用的總時間

if (resolvedmantisnum + modifymantisnum) = 0 then ' 處理沒有mantis的情況

sheets("sheet2").cells(summaryrow, "g") = 0

else

' 平均處理一條mantis耗費的時間

sheets("sheet2").cells(summaryrow, "g") = (modifymantistime + resolvedmantistime) / (resolvedmantisnum + modifymantisnum)

end if

next

大致講解一下,sheets("sheet2").cells(65536, 1).end(xlup).row就表示統計**的最大行數,最外面的for迴圈,就是要在sheet2中,按照每行,將計算好的資料填進去。

sheet1就是原始資料**,裡面這個for迴圈就是統計每個人的資料

最後,下面這段,就是將統計和計算好的資料,填入到sheet2中相應的位置中

sheets("sheet2").cells(summaryrow, "b") = resolvedmantisnum ' resolved掉的mantis數量

sheets("sheet2").cells(summaryrow, "c") = modifymantisnum ' 修改過的mantis數量

sheets("sheet2").cells(summaryrow, "d") = resolvedmantistime ' resolved mantis的總時間

sheets("sheet2").cells(summaryrow, "e") = modifymantistime ' 修改mantis的總時間

sheets("sheet2").cells(summaryrow, "f") = modifymantistime + resolvedmantistime ' 處理mantis使用的總時間

if (resolvedmantisnum + modifymantisnum) = 0 then ' 處理沒有mantis的情況

sheets("sheet2").cells(summaryrow, "g") = 0

else

' 平均處理一條mantis耗費的時間

sheets("sheet2").cells(summaryrow, "g") = (modifymantistime + resolvedmantistime) / (resolvedmantisnum + modifymantisnum)

至此,程式完成,如果要執行的話,選擇執行選單中的執行子過程,或者直接按f5就可以

OpenMP實現資料統計

編寫openmp程式,陣列裡存放大量浮點數,資料分布在05之間,為了對資料分布有乙個更為直觀的感受,對資料進行統計,01之間 1 2之間 分別有多少浮點數。openmp實現求矩陣均值最大值以及最小值 openmp實現資料統計 pthreads實現任務佇列 pthreads實現梯形積分 visual ...

python實現快速資料統計

首先,先來看一看老闆發的這個excel資料。可以發現它的表頭具有6個標籤。老闆要求這個最終統計結果需要將得到對應實驗室的同名裝置的台數,並且它們應該被購置時間與單價所區分,並且裝置編號需要具有範圍。下面是經過程式處理後得到的結果。這個資料在未處理前是1208行,處理後為225行。1.讀取csv檔案。...

資料統計頁面

麵包屑導航區 class el icon arrow right 首頁 el breadcrumb item 資料統計 el breadcrumb item 資料包表 el breadcrumb item el breadcrumb 卡片檢視區域 為echarts準備乙個具備大小 寬高 的dom m...