MSCBSC 移動(dòng)通信論壇
搜索
登錄注冊
網(wǎng)絡(luò)優(yōu)化工程師招聘專欄 4G/LTE通信工程師最新職位列表 通信實(shí)習(xí)生/應(yīng)屆生招聘職位

  • 閱讀:1525
  • 回復(fù):0
VBS數(shù)據(jù)核對
sanren99999
初級(jí)會(huì)員
鎵嬫満鍙風(fēng)爜宸查獙璇? style=


 發(fā)短消息    關(guān)注Ta 

積分 176
帖子 33
威望 2127 個(gè)
禮品券 8 個(gè)
專家指數(shù) 11
注冊 2011-4-18
專業(yè)方向  通信
回答問題數(shù) 0
回答被采納數(shù) 0
回答采納率 0%
 
發(fā)表于 2013-08-14 10:32:05  只看樓主 
【資料名稱】:VBS數(shù)據(jù)核對

【資料作者】:sanren

【資料日期】:2013-5

【資料語言】:中文

【資料格式】:其它

【資料目錄和簡介】:

Function getFilesPath() As String
Dim Shell, myPath
Set Shell = CreateObject("Shell.Application")
Set myPath = Shell.BrowseForFolder(0, "請選擇文件夾", 0, 0)
If Not myPath Is Nothing Then
'MsgBox myPath.self.Path
Set Shell = Nothing
'Set myPath = Nothing
getFilesPath = myPath.self.Path
End If
End Function


Sub 合并()
Dim myPath$, myFile$, myFile14$, myFile15$, myFile16$, myTMP$, address$, kpiName$, sheetName$, TMP As Workbook, AK As Workbook, AK15 As Workbook, AK16 As Workbook, aRow%, tRow%, i%, j%, k%, r%, rc%, cc%, m%, devCount%, findCount%, index%
Dim arr(1 To 100, 1 To 1)'創(chuàng)建一個(gè)可以容下100行1列的數(shù)組空間,記錄計(jì)算結(jié)果

Application.ScreenUpdating = False'凍結(jié)屏幕,以防屏幕抖動(dòng)
'myPath = ThisWorkbook.Path & "\" '分表\" '把文件路徑定義給變量
myPath = getFilesPath
'myPath = "D:\000\EMS\test" '調(diào)試用
'myPath = "D:\000\EMS\20130419"

If myPath = "" Then
MsgBox "沒有選中文件夾,退出!"
Exit Sub'退出過程。
End If
myPath = myPath & "\"
'Exit Sub'退出過程。

'myFile = Dir(myPath & "*.xls") '依次找尋指定路徑中的*.xls文件
myFile14 = Dir(myPath & "*.14.csv")'依次找尋指定路徑中的*.xls文件
'myFile15 = Dir(myPath & "*.15.csv")'依次找尋指定路徑中的*.xls文件
'myFile16 = Dir(myPath & "*.16.csv")'依次找尋指定路徑中的*.xls文件
'MsgBox myFile14



findCount = 0
index = 1'模板中sheet頁序列
'tRow = ThisWorkbook.Sheets(2).UsedRange.Rows.Count

Do While myFile14 <> ""'當(dāng)指定路徑中有文件時(shí)進(jìn)行循環(huán)

If myFile14 <> ThisWorkbook.Name Then
'MsgBox myFile14
Set AK14 = Workbooks.Open(myPath & myFile14)'打開14庫的文件

'最大行數(shù)、列數(shù)
rc = AK14.Sheets(1).UsedRange.Rows.Count
cc = AK14.Sheets(1).UsedRange.Columns.Count

myFile = SplitFileName(myFile14, ".")
myFile15 = SplitFileName(myFile14, ".") & ".15.csv"
myFile16 = SplitFileName(myFile14, ".") & ".16.csv"
'MsgBox "myFile15=" & myFile15 & "myFile16=" & myFile16


Set AK15 = Workbooks.Open(myPath & myFile15)'打開15庫的文件


Set AK16 = Workbooks.Open(myPath & myFile16)'打開14庫的文件

'AK16.Sheets(1).Columns(cc).NumberFormatLocal = "0_ "

sheetName = "kpi" & index

'Application.DisplayAlerts = False '刪除工作表警告提示去消
'ThisWorkbook.Sheets(sheetName).Delete
'Application.DisplayAlerts = True
'ThisWorkbook.Save


ThisWorkbook.Sheets.Add(after:=Sheets(Sheets.Count)).Name = sheetName '添加sheet
tRow = ThisWorkbook.Sheets(sheetName).UsedRange.Rows.Count

AK14.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets(sheetName).Range("a" & tRow)



For i = 3 To rc - 2

If Trim(AK14.Sheets(1).Cells(i, cc).Text) = "NULL" Then
AK14.Sheets(1).Cells(i, cc) = 0
End If

If Trim(AK15.Sheets(1).Cells(i, cc).Text) = "NULL" Then
AK15.Sheets(1).Cells(i, cc) = 0
End If

If Trim(AK16.Sheets(1).Cells(i, cc).Text) = "NULL" Then
AK16.Sheets(1).Cells(i, cc) = 0
End If


ThisWorkbook.Sheets(sheetName).Cells(i, cc) = AK14.Sheets(1).Cells(i, cc) + AK15.Sheets(1).Cells(i, cc) + AK16.Sheets(1).Cells(i, cc)

If Left(myFile, 8) = "gn-ul-dl" Then

If Trim(AK14.Sheets(1).Cells(i, cc - 1).Text) = "NULL" Then
AK14.Sheets(1).Cells(i, cc - 1) = 0
End If

If Trim(AK15.Sheets(1).Cells(i, cc - 1).Text) = "NULL" Then
AK15.Sheets(1).Cells(i, cc - 1) = 0
End If

If Trim(AK16.Sheets(1).Cells(i, cc - 1).Text) = "NULL" Then
AK16.Sheets(1).Cells(i, cc - 1) = 0
End If

ThisWorkbook.Sheets(sheetName).Cells(i, cc - 1) = AK14.Sheets(1).Cells(i, cc - 1) + AK15.Sheets(1).Cells(i, cc - 1) + AK16.Sheets(1).Cells(i, cc - 1)
End If
Next



Workbooks(myFile14).Close False'關(guān)閉源工作簿,并不作修改
Workbooks(myFile15).Close False'關(guān)閉源工作簿,并不作修改
Workbooks(myFile16).Close False'關(guān)閉源工作簿,并不作修改

ThisWorkbook.Sheets(sheetName).Range("B3:Z" & (rc - 2)).Sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range("d2"), Order2:=xlAscending, Key3:=Range("c2"), Order3:=xlAscending

End If

myFile14 = Dir '找尋下一個(gè)*.xls文件
index = index + 1
Loop


Application.ScreenUpdating = True'凍結(jié)屏幕,此類語句一般成對使用

MsgBox "合并完成,請查看!", 64, "提示"
End Sub





Function FindName_new(ByRef what As String, ByRef currSheet As Worksheet) As String
Dim rng As Range
'Dim what As String
'what = "Error"
'Do
Set rng = currSheet.UsedRange.Find(what)
If Not rng Is Nothing Then

'Columns(rng.Column).Delete
'MsgBox "第一個(gè)數(shù)據(jù)發(fā)現(xiàn)在單元格:" & rng.address
FindName_new = rng.address(ReferenceStyle:=xlR1C1)
'sStr = ActiveCell.Address()
'Range(sStr).Offset(1, 0).Select
End If
'Loop
End Function

Function FindName(ByRef what As String, ByRef currSheet As Worksheet) As String
Dim rng As Range
'Dim what As String
'what = "Error"
'Do
Set rng = currSheet.UsedRange.Find(what)
If Not rng Is Nothing Then

'Columns(rng.Column).Delete
'MsgBox "第一個(gè)數(shù)據(jù)發(fā)現(xiàn)在單元格:" & rng.address
FindName = rng.address(ColumnAbsolute:=False)
'sStr = ActiveCell.Address()
'Range(sStr).Offset(1, 0).Select
End If
'Loop
End Function



Function statDevNum(ByRef currSheet As Worksheet) As Integer
Dim x As Integer, y As Integer, rc As Integer, cc As Integer, devCount As Integer, dateTime As String
Dim arr(1 To 10, 1 To 3)'創(chuàng)建一個(gè)可以容下10行3列的數(shù)組空間
Dim rng As Range

'最大行數(shù)、列數(shù)
rc = ActiveSheet.UsedRange.Rows.Count
cc = ActiveSheet.UsedRange.Columns.Count

'先判斷一天之內(nèi)有多少行記錄,即找出有多少個(gè)設(shè)備
dateTime = Cells(7, 2).Text
'MsgBox dateTime
devCount = 0
For x = 7 To rc
If dateTime = Cells(x, 2).Text Then
devCount = devCount + 1
Else
Exit For
End If
Next x

'MsgBox "devCount=" & devCount

statDevNum = devCount

End Function

Function SplitAddress(ByRef str As String, ByRef splitStr As String) As Integer '由查詢時(shí)間返回有效行數(shù)
Dim Val, n
'str = "資產(chǎn)分類-->硬件類-->整機(jī)-->個(gè)人處理設(shè)備-->筆記本-->中端筆記本"
Val = Split(str, splitStr)
'For n = LBound(Val) To UBound(Val)
'MsgBox Val(n)
'Next
SplitAddress = Val(UBound(Val))
End Function

Function SplitFileName(ByRef str As String, ByRef splitStr As String) As String '返回文件名
Dim Val, n
'str = "資產(chǎn)分類-->硬件類-->整機(jī)-->個(gè)人處理設(shè)備-->筆記本-->中端筆記本"
Val = Split(str, splitStr)
'For n = LBound(Val) To UBound(Val)
'MsgBox Val(n)
'Next
SplitFileName = Val(LBound(Val))
End Function


Sub 按鈕2_Click()
Sheets(1).UsedRange.Clear
End Sub

Sub 按鈕1_單擊()
Dim myPath$, myFile$, myTMP$, address$, kpiName$, TMP As Workbook, AK As Workbook, aRow%, tRow%, i%, j%, k%, r%, rc%, cc%, m%, devCount%, findCount%, index%
Dim arr(1 To 100, 1 To 1)'創(chuàng)建一個(gè)可以容下100行1列的數(shù)組空間,記錄計(jì)算結(jié)果

Application.ScreenUpdating = False'凍結(jié)屏幕,以防屏幕抖動(dòng)
'myPath = ThisWorkbook.Path & "\" '分表\" '把文件路徑定義給變量
myPath = getFilesPath
'myPath = "D:\000\EMS\test" '調(diào)試用
'myPath = "D:\000\EMS\20130419"

If myPath = "" Then
MsgBox "沒有選中文件夾,退出!"
Exit Sub'退出過程。
End If
myPath = myPath & "\"
'Exit Sub '退出過程。

'myFile = Dir(myPath & "*.xls")'依次找尋指定路徑中的*.xls文件
myFile = Dir(myPath & "*.csv")'依次找尋指定路徑中的*.xls文件

myTMP = "D:\kpi_name.xlsx"'打開指標(biāo)模板文件
Set TMP = Workbooks.Open(myTMP) '打開符合要求的文件
findCount = 0
index = 1'模板中sheet頁序列
tRow = ThisWorkbook.Sheets(1).UsedRange.Rows.Count

Do While myFile <> ""'當(dāng)指定路徑中有文件時(shí)進(jìn)行循環(huán)

If myFile <> ThisWorkbook.Name Then
'MsgBox myFile
Set AK = Workbooks.Open(myPath & myFile) '打開符合要求的文件

For i = 1 To 1 'AK.Sheets.Count '只對第一個(gè)工作本有效
aRow = AK.Sheets(i).Range("a65536").End(xlUp).Row
'tRow = ThisWorkbook.Sheets(1).Range("a65536").End(xlUp).Row + 1
'tRow = ThisWorkbook.Sheets(1).UsedRange.Rows.Count + 3
tRow = tRow + 1
'AK.Sheets(i).Select
'AK.Sheets(i).Range("a3:k" & aRow).Copy ThisWorkbook.Sheets(1).Range("a" & tRow)


devCount = statDevNum(AK.Sheets(i)) '返回一天之內(nèi)的設(shè)備數(shù)
'MsgBox "devCount=" & devCount

'---------------------------------------------------------------------------------------------------------------
For index = 1 To TMP.Sheets.Count '遍歷模板頁
'遍歷打開工作頁的第六行,找到相應(yīng)的kpi name
findCount = 0
kpiName = ""'將kpiName變量置空,以免影響下一輪判斷
Erase arr'清空數(shù)組

For m = 1 To TMP.Sheets(index).UsedRange.Rows.Count
address = FindName_new(TMP.Sheets(index).Cells(m, 1).Value, AK.Sheets(i))

If address <> "" Then'找到了對應(yīng)的指標(biāo)列名 解析address

'MsgBox "address=" & address
j = SplitAddress(address, "C")
'MsgBox "column=" & SplitAddress(address, "C")
kpiName = TMP.Sheets(index).Cells(1, 1).Value

For k = 1 To (aRow - 6) / devCount'先填上日期再說,在B列
For r = 1 To devCount
arr(k, 1) = arr(k, 1) + AK.Sheets(i).Cells(6 + devCount * (k - 1) + r, j).Value
Next
Next
findCount = findCount + 1

End If
Next



'MsgBox "findCount=" & findCount
'MsgBox " TMP.Sheets(index).name=" & TMP.Sheets(index).Name
If kpiName <> "" Then
If findCount > 1 Or TMP.Sheets(index).UsedRange.Rows.Count = 2 Then
'ThisWorkbook.Sheets(1).Range("c" & tRow).Value = kpiName

'填上結(jié)果
For k = 1 To (aRow - 6) / devCount'先填上日期再說,在B列
'If arr(k, 1) > 0 Then
ThisWorkbook.Sheets(1).Cells(tRow + k, 1).Value = kpiName
'ThisWorkbook.Sheets(1).Cells(tRow + k, 2).Value = AK.Sheets(i).Cells(6 + devCount * (k - 1) + k, 2).Value
If devCount > 1 Then
ThisWorkbook.Sheets(1).Cells(tRow + k, 2).Value = AK.Sheets(i).Cells(6 + devCount * k - 1, 2).Value
Else
ThisWorkbook.Sheets(1).Cells(tRow + k, 2).Value = AK.Sheets(i).Cells(6 + k, 2).Value
End If

ThisWorkbook.Sheets(1).Cells(tRow + k, 3).Value = arr(k, 1)
ThisWorkbook.Sheets(1).Cells(tRow + k, 4).Value = "findCount:" & findCount
ThisWorkbook.Sheets(1).Cells(tRow + k, 5).Value = "devCount:" & devCount
ThisWorkbook.Sheets(1).Cells(tRow + k, 6).Value = myFile
'End If
Next

'tRow = tRow + k + 1 '添加之后行數(shù)重新計(jì)算
'tRow = ThisWorkbook.Sheets(1).Range("a65536").End(xlUp).Row + 2
tRow = tRow + k + 1
End If
'tRow = ThisWorkbook.Sheets(1).UsedRange.Rows.Count + 3
End If

Next
'-------------------------------------------------------------------------------------------------------------------------------------------
Next
Workbooks(myFile).Close False'關(guān)閉源工作簿,并不作修改
End If

myFile = Dir '找尋下一個(gè)*.xls文件
Loop

Workbooks("kpi_name.xlsx").Close False'關(guān)閉源工作簿,并不作修改
Application.ScreenUpdating = True'凍結(jié)屏幕,此類語句一般成對使用
MsgBox "匯總完成,請查看!", 64, "提示"
End Sub

查看積分策略說明
附件下載列表:
2013-8-14 10:32:05  下載次數(shù): 9
數(shù)據(jù)核對.zip (3.22 KB)
2013-8-14 10:32:05  下載次數(shù): 6
kpi_name.xlsx (33.63 KB)
掃碼關(guān)注5G通信官方公眾號(hào),免費(fèi)領(lǐng)取以下5G精品資料
  • 1、回復(fù)“YD5GAI”免費(fèi)領(lǐng)取《中國移動(dòng):5G網(wǎng)絡(luò)AI應(yīng)用典型場景技術(shù)解決方案白皮書
  • 2、回復(fù)“5G6G”免費(fèi)領(lǐng)取《5G_6G毫米波測試技術(shù)白皮書-2022_03-21
  • 3、回復(fù)“YD6G”免費(fèi)領(lǐng)取《中國移動(dòng):6G至簡無線接入網(wǎng)白皮書
  • 4、回復(fù)“LTBPS”免費(fèi)領(lǐng)取《《中國聯(lián)通5G終端白皮書》
  • 5、回復(fù)“ZGDX”免費(fèi)領(lǐng)取《中國電信5G NTN技術(shù)白皮書
  • 6、回復(fù)“TXSB”免費(fèi)領(lǐng)取《通信設(shè)備安裝工程施工工藝圖解
  • 7、回復(fù)“YDSL”免費(fèi)領(lǐng)取《中國移動(dòng)算力并網(wǎng)白皮書
  • 8、回復(fù)“5GX3”免費(fèi)領(lǐng)取《 R16 23501-g60 5G的系統(tǒng)架構(gòu)1
  • 對本帖內(nèi)容的看法? 我要點(diǎn)評(píng)

     
    [充值威望,立即自動(dòng)到帳] [VIP貴賓權(quán)限+威望套餐] 另有大量優(yōu)惠贈(zèng)送活動(dòng),請光臨充值中心
    充值擁有大量的威望和最高的下載權(quán)限,下載站內(nèi)資料無憂

    快速回復(fù)主題    
    標(biāo)題
    內(nèi)容
     上傳資料請點(diǎn)左側(cè)【添加附件】

    (勾選中文件為要?jiǎng)h除文件)


    當(dāng)前時(shí)區(qū) GMT+8, 現(xiàn)在時(shí)間是 2025-01-09 20:54:47
    渝ICP備11001752號(hào)  Copyright @ 2006-2016 mscbsc.com  本站統(tǒng)一服務(wù)郵箱:mscbsc@163.com

    Processed in 0.604923 second(s), 13 queries , Gzip enabled
    TOP
    清除 Cookies - 聯(lián)系我們 - 移動(dòng)通信網(wǎng) - 移動(dòng)通信論壇 - 通信招聘網(wǎng) - Archiver