制作主文件

时间:2014-03-11 18:54:22

标签: excel-vba merge automation excel-2013 vba

我已经将这个宏组合成4个不同的呼叫跟踪器到一个主文件中。我的问题是它只能通过Call Tracker和Call Tracker SS向主跟踪器提取信息,只有案例表。

根本没有从Call Tracker Jess或Miri文件获取信息,也没有填写其他选项卡:任务,通知,特殊请求或跟进,从所有文件到主跟踪器。

奇怪的是,我没有收到任何错误。宏运行导入呼叫跟踪器和呼叫跟踪器SS案例文件中的前两个记录并停止...

有人可以查看代码并告知我可能做错了吗?

Sub Master_Tracker()

Dim wbMaster As Workbook
Dim wsCases As Worksheet
Dim wsTasks As Worksheet
Dim wsNotifications As Worksheet
Dim wsSpecialRequests As Worksheet
Dim wsFollowUp As Worksheet
Dim wbCall As Workbook
Dim wsSheet As Worksheet
Dim wbCallTrackerSS As Workbook
Dim wbCallTrackerJess As Workbook
Dim wbCallTrackerMiri As Workbook
Dim wsShCalls As Worksheet

Set wbMaster = Workbooks("Master Tracker.xlsm")
Set wsMaster = wbMaster.Sheets("Cases")
wsMaster.Cells.ClearContents

Set wbCall = Workbooks.Open(Filename:="H:\BX-HR\BX-INDUSTRIAL RELATIONS\HR REPRESENTATIVES\PRIVATE\HRSSC\US &CA HD\Call Tracker.xlsm")
If Not wbCall Is Nothing Then
  Set wsSheet = wbCall.Sheets("Cases")
  wsSheet.Range("A6:P" & wsSheet.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A3")
Else
  Exit Sub
End If

Set wbCallTrackerSS = Workbooks.Open(Filename:="H:\BX-HR\BX-INDUSTRIAL RELATIONS\HR REPRESENTATIVES\PRIVATE\HRSSC\US &CA HD\Call Tracker SS.xlsm")
If Not wbCallTrackerSS Is Nothing Then
  Set wsShCalls = wbCallTrackerSS.Sheets("Cases")
  wsShCalls.Range("A7:P" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Else
  Exit Sub
End If

Set wbCallTrackerJG = Workbooks.Open(Filename:="H:\BX-HR\BX-INDUSTRIAL RELATIONS\HR REPRESENTATIVES\PRIVATE\HRSSC\US &CA HD\Call Tracker Jess.xlsm")
If Not wbCallTrackerJess Is Nothing Then
  Set wsShCalls = wbCallTrackerJess.Sheets("Cases")
  wsShCalls.Range("A7:P" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Else
  Exit Sub
End If

Set wbCallTrackerMV = Workbooks.Open(Filename:="H:\BX-HR\BX-INDUSTRIAL RELATIONS\HR REPRESENTATIVES\PRIVATE\HRSSC\US &CA HD\Call Tracker Miri.xlsm")
If Not wbCallTrackerMiri Is Nothing Then
  Set wsShCalls = wbCallTrackerMiri.Sheets("Cases")
  wsShCalls.Range("A7:P" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Else
  Exit Sub
End If

wsMaster.AutoFilterMode = False
wsMaster.Rows("3:3").AutoFilter
With wsMaster.AutoFilter.Sort
  .SortFields.Clear
  .SortFields.Add Key:=Range("A3"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  .Header = xlYes
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With

Set wsMaster = wbMaster.Sheets("Tasks")
wsMaster.Cells.ClearContents

Set wsSheet = wbCall.Sheets("Tasks")
wsSheet.Range("A1:I" & wsSheet.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A1")
wsMaster.Range("A1:I1").EntireColumn.AutoFit

Set wsShCalls = wbCallTrackerSS.Sheets("Tasks")
wsShCalls.Range("A2:I" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

Set wsShCalls = wbCallTrackerJess.Sheets("Tasks")
wsShCalls.Range("A2:I" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

Set wsShCalls = wbCallTrackerMiri.Sheets("Tasks")
wsShCalls.Range("A2:I" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

wsMaster.AutoFilterMode = False
wsMaster.Rows("1:1").AutoFilter
With wsMaster.AutoFilter.Sort
  .SortFields.Clear
  .SortFields.Add Key:=Range("B1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  .Header = xlYes
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With

Set wsMaster = wbMaster.Sheets("Notifications")
wsMaster.Cells.ClearContents

Set wsSheet = wbCall.Sheets("Notifications")
wsSheet.Range("A1:I" & wsSheet.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A1")
wsMaster.Range("A1:I1").EntireColumn.AutoFit

Set wsShCalls = wbCallTrackerSS.Sheets("Notifications")
wsShCalls.Range("A2:I" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

Set wsShCalls = wbCallTrackerJess.Sheets("Notifications")
wsShCalls.Range("A2:I" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

Set wsShCalls = wbCallTrackerMiri.Sheets("Notifications")
wsShCalls.Range("A2:I" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

wsMaster.AutoFilterMode = False
wsMaster.Rows("1:1").AutoFilter
With wsMaster.AutoFilter.Sort
  .SortFields.Clear
  .SortFields.Add Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  .Header = xlYes
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With

Set wsMaster = wbMaster.Sheets("Special Requests")
wsMaster.Cells.ClearContents

Set wsSheet = wbCall.Sheets("Special Requests")
wsSheet.Range("A1:E" & wsSheet.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A1")
wsMaster.Range("A1:E1").EntireColumn.AutoFit

Set wsShCalls = wbCallTrackerSS.Sheets("Special Requests")
wsShCalls.Range("A2:E" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

Set wsShCalls = wbCallTrackerJess.Sheets("Special Requests")
wsShCalls.Range("A2:E" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

Set wsShCalls = wbCallTrackerMiri.Sheets("Special Requests")
wsShCalls.Range("A2:E" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

wsMaster.AutoFilterMode = False
wsMaster.Rows("1:1").AutoFilter
With wsMaster.AutoFilter.Sort
  .SortFields.Clear
  .SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  .Header = xlYes
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With

Set wsMaster = wbMaster.Sheets("Follow Up")
wsMaster.Cells.ClearContents

Set wsSheet = wbCall.Sheets("Follow Up")
wsSheet.Range("A1:F" & wsSheet.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A1")
wsMaster.Range("A1:F1").EntireColumn.AutoFit

Set wsShCalls = wbCallTrackerSS.Sheets("Follow Up")
wsShCalls.Range("A2:F" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

Set wsShCalls = wbCallTrackerJess.Sheets("Follow Up")
wsShCalls.Range("A2:F" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

Set wsShCalls = wbCallTrackerMiri.Sheets("Follow Up")
wsShCalls.Range("A2:F" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

wsMaster.AutoFilterMode = False
wsMaster.Rows("1:1").AutoFilter
With wsMaster.AutoFilter.Sort
  .SortFields.Clear
  .SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  .Header = xlYes
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With
Application.CutCopyMode = False

wbCallTrackerSS.Close False
wbCallTrackerJess.Close False
wbCallTrackerMiri.Close False
wbCall.Close False

Set wsShCalls = Nothing
Set wbCallTrackerSS = Nothing
Set wbCallTrackerJess = Nothing
Set wbCallTrackerMiri = Nothing
Set wsSheet = Nothing
Set wbCall = Nothing

Set wsMaster = Nothing
Set wbMaster = Nothing
End Sub

1 个答案:

答案 0 :(得分:1)

首先在模块顶部添加Option Explicit并重新运行代码。

例如:

Set wbCallTrackerJG = Workbooks.Open(Filename:="H:\BX-HR\BX-INDUSTRIAL RELATIONS\HR REPRESENTATIVES\PRIVATE\HRSSC\US &CA HD\Call Tracker Jess.xlsm")
If Not wbCallTrackerJess Is Nothing Then
  Set wsShCalls = wbCallTrackerJess.Sheets("Cases")
  wsShCalls.Range("A7:P" & wsShCalls.Range("A" & Rows.Count).End(xlUp).Row).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Else
  Exit Sub
End If

在这里,您打开工作簿并设置为wbCallTrackerJG(未声明的变量),然后检查wbCallTrackerJess的状态( 已声明)。 Option Explicit会为你带来这个错误。

另请注意,如果您的Open()失败,那么您的代码会引发错误,因此检查Nothing无法帮助您解决此问题。