Python与VBA的比较2
需求:
input文件中有两列数据,第一列为Name,第二列为Score,Name列里有重复的值,要求按照name的唯一值统计 score,输出到output文件按中。
1–用 Pandas解决:
from pandas import DataFrame
import pandas as pd
df=pd.read_excel(r"C:\Users\12078\Desktop\UIPATH_test\20200409\input.xlsx",sheet_name='Sheet1')
df=df.groupby('Name').sum()
df.to_excel(r"C:\Users\12078\Desktop\UIPATH_test\20200409\output.xlsx",sheet_name="Sheet1")
2–用 VBA解决:
Option Explicit
Option Base 1
Sub test_data()
on error goto errorhandling
Dim wb_in As Workbook
Dim wb_out As Workbook
Dim sht_in As Worksheet
Dim sht_out As Worksheet
Dim rng As Range
Dim usedrows As Integer
Dim usedrows_out As Integer
Dim input_path As String
Dim output_path As String
Dim data_dict As Object
Dim data_arr As Variant
Dim data_arr_out As Variant
input_path = "C:\Users\12078\Desktop\UIPATH_test\20200409\input.xlsx"
output_path = "C:\Users\12078\Desktop\UIPATH_test\20200409\output.xlsx"
Set wb_in = checkAndAttachWorkbook(input_path)
Set sht_in = wb_in.Worksheets("Sheet1")
Set wb_out = Workbooks.Add
wb_out.SaveAs output_path
Set sht_out = wb_out.Worksheets("Sheet1")
Set data_dict = CreateObject("Scripting.Dictionary")
usedrows = WorksheetFunction.Max(getLastValidRow(sht_in, "A"), getLastValidRow(sht_in, "B"))
data_arr = sht_in.Range("A2", "B" & usedrows)
Dim i As Integer
For i = 1 To UBound(data_arr, 1)
If Not data_dict.Exists(data_arr(i, 1)) Then
data_dict.Add data_arr(i, 1), data_arr(i, 2)
Else
data_dict(data_arr(i, 1)) = data_dict(data_arr(i, 1)) + data_arr(i, 2)
End If
Debug.Print data_arr(i, 1) & "--" & data_dict(data_arr(i, 1))
Next i
sht_out.Range("A1") = "Name"
sht_out.Range("B1") = "Score"
usedrows_out = data_dict.Count
Dim index_dict As Integer
ReDim data_arr_out(1 To UBound(data_dict.keys) + 1, 1 To 2)
For index_dict = 0 To UBound(data_dict.keys)
data_arr_out(index_dict + 1, 1) = data_dict.keys()(index_dict)
data_arr_out(index_dict + 1, 2) = data_dict(data_dict.keys()(index_dict))
Debug.Print index_dict
Debug.Print data_arr_out(index_dict + 1, 1) & "--" & data_arr_out(index_dict + 1, 2) 'for debug
Next
sht_out.Range("A2").Resize(UBound(data_arr_out), 2) = data_arr_out
Call checkAndCloseWorkbook(wb_in, False)
Call checkAndCloseWorkbook(wb_out, True)
Exit Sub
errorhandling:
Call checkAndCloseWorkbook(wb_in, False)
Call checkAndCloseWorkbook(wb_out, False)
End Sub
' 辅助函数:
'Get last row of Column N in a Worksheet
Function getLastValidRow(in_ws As Worksheet, in_col As String)
getLastValidRow = in_ws.Cells(in_ws.Rows.Count, in_col).End(xlUp).Row
End Function
Function checkAndAttachWorkbook(in_wb_path As String) As Workbook
Dim wb As Workbook
Dim mywb As String
mywb = in_wb_path
For Each wb In Workbooks
If LCase(wb.FullName) = LCase(mywb) Then
Set checkAndAttachWorkbook = wb
Exit Function
End If
Next
Set wb = Workbooks.Open(in_wb_path, UpdateLinks:=0)
Set checkAndAttachWorkbook = wb
End Function
Function checkAndCloseWorkbook(in_wb_path As String, in_saved As Boolean)
Dim wb As Workbook
Dim mywb As String
mywb = in_wb_path
For Each wb In Workbooks
If LCase(wb.FullName) = LCase(mywb) Then
wb.Close Savechanges:=in_saved
Exit Function
End If
Next
End Function
输出结果:
比对结论:
pandas简单得多!