|
楼主 |
发表于 2018-9-18 06:32
|
显示全部楼层
本帖最后由 ptnjust007 于 2018-9-18 09:44 编辑
这是我自己写的,但是一运行就出错,“运行时错误 自动化。。。。”
另外 针对重复的A列ID 也没有替换一整行
Sub 汇总订单表()
Dim Wb As Workbook
Dim i, j, k, n, iRow, tt
Dim d, arr
Application.ScreenUpdating = False '冻结屏幕,以防屏幕抖动
Set Wb = GetObject(ThisWorkbook.Path & "\*.txt")
iRow = Wb.Sheets(1).Range("A65536").End(xlUp).Row
j = Sheets("订单表").Range("A65536").End(xlUp).Row
For i = 2 To iRow
Set tt = Sheets("订单表").Columns("A:A").Find(Wb.Sheets(1).Cells(i, "A"), Lookat:=xlPart) '判断订单号是否存在
If tt Is Nothing Then ' 判断是否存在
Wb.Sheets(1).Range("A" & i & ":X" & i).Copy Sheets("订单表").Range("A" & (j + 1)) '复制评价内容
Sheets("订单表").Range("X" & (j + 1)) = Format(Format(Replace(Sheets("订单表").Range("C" & (j + 1)), "T", " "), "yyyy/mm/dd hh:mm:ss") - 7 / 24, "yyyy/mm/dd") '添加当天日期
j = j + 1
End If
Next i
Wb.Close False
'以下SQL求和
Dim m As Byte, brr, cnn As Object, rst As Object, sql$, p As Byte
Set cnn = CreateObject("adodb.connection")
cnn.Open "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data Source =" & ThisWorkbook.FullName
brr = Array("销量", "销售额")
For m = 0 To 1
For n = 3 To 10
With Sheets(brr(m))
.UsedRange.Clear
sql = "transform sum(" & brr(m) & ") select sku from [订单表$A:X] where 购买日期=.cells(1,n) and order-status<>'Cancelled' "
Set rst = cnn.Execute(sql)
.[A2].CopyFromRecordset rst
For p = 1 To rst.Fields.Count
.Cells(1, p) = rst.Fields(p - 1).Name
Next p
.UsedRange.Borders.LineStyle = xlContinuous
rst.Close
End With
Next n
Next m
cnn.Close
Set rst = Nothing
Set cnn = Nothing
MsgBox "已更新数据"
Application.ScreenUpdating = True '冻结屏幕,此类语句一般成对使用
End Sub |
-
|