我的代码在需要时激活正确的工作簿,但只返回值为0而不是返回实际值,即数字和日期。我还需要在数据传输完成后关闭DailyPerformance工作簿。
Private Sub CommandButton2_Click()
TrackingReport = ActiveWorkbook.Name
Dim xDate As Double
Dim ACD As Double
Dim Untouched As Double
Dim DailyAct As Double
Dim SchedAdherence As Double
Dim Status As Double
Dim RowCount As Long
Dim DailyPerformance As Excel.Workbook
Set DailyPerformance = Workbooks.Open("P:\DX\Daily Team Performance\Dignositc Team's Daily Performance Tracker (001).xlsx", Password:="dailyp123")
Worksheets("1359 Report").Select
xDate = Range("L2").Value
Worksheets("Matt's Tab Two").Select
ACD = Range("C4").Value
Worksheets("Matt's Tab Two").Select
DailyAct = Range("E4").Value
Worksheets("Matt's Tab Two").Select
SchedAdherence = Range("F4").Value
Worksheets("1359 Report Summary").Select
Status = Range("B5").Value
Workbooks(TrackingReport).Activate
Set TrackinReport = ActiveWorkbook
Worksheets("Data").Select
Worksheets("Data").Range("B1").Select
RowCount = Worksheets("Data").Range("B1").CurrentRegion.Rows.Count
With Worksheets("Data").Range("B1")
.Offset(RowCount, 0) = xDate
.Offset(RowCount, 1) = ACD
.Offset(RowCount, 2) = DailyAct
.Offset(RowCount, 3) = SchedAdherence
.Offset(RowCount, 4) = Status
End With
Workbooks(TrackingReport).Save
End Sub
答案 0 :(得分:1)
另一个版本
Private Sub CommandButton2_Click()
Dim xlApp
Dim DailyPerformance
Dim xDate As String
Dim ACD As String
Dim Untouched As String
Dim DailyAct As String
Dim SchedAdherence As String
Dim Status As String
Dim RowCount As Integer
Dim TrackingReport as Workbook
TrackingReport = ThisWorkbook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True ' if you want it to be visible
Set DailyPerformance = xlApp.Workbooks.Open("P:\DX\Daily Team Performance\Dignositc Team's Daily Performance Tracker (001).xlsx", Password:="dailyp123")
DailyPerformance.Activate
xDate = DailyPerformance.Worksheets("1359 Report").Range("L2").Value
ACD = DailyPerformance.Worksheets("Matt's Tab Two").Range("C4").Value
DailyAct = DailyPerformance.Worksheets("Matt's Tab Two").Range("E4").Value
SchedAdherence = DailyPerformance.Worksheets("Matt's Tab Two").Range("F4").Value
Status = DailyPerformance.Worksheets("1359 Report Summary").Range("B5").Value
TrackingReport.Activate
RowCount = TrackingReport.Worksheets("Data").Range("B1").CurrentRegion.Rows.Count
With TrackingReport.Worksheets("Data").Range("B1")
.Offset(RowCount, 0) = xDate
.Offset(RowCount, 1) = ACD
.Offset(RowCount, 2) = DailyAct
.Offset(RowCount, 3) = SchedAdherence
.Offset(RowCount, 4) = Status
End With
TrackingReport.save
DailyPerformance.close SaveChanges:=False
End Sub
答案 1 :(得分:0)
尝试避免使用select而不是ActiveWorkbook
,请使用ThisWorkbook
。 ActiveWorkbook
是顶部"中的一个"。 ThisWorkbook
是vba代码执行的地方。
由于我没有数据来测试这个,我认为你的引用是正确的。例如,您真的希望xDate为Double
(像x,xx这样的数字)而不是Date
吗?与状态相同。你可以稍后再查看。
如果您对代码有任何疑问,请询问。
Private Sub CommandButton2_Click()
Dim xDate As Double
Dim ACD As Double
Dim Untouched As Double
Dim DailyAct As Double
Dim SchedAdherence As Double
Dim Status As Double
Dim RowCount As Long
Dim DailyPerformance As Excel.Workbook
Dim TWb As Workbook
Set TWb = ThisWorkbook
Set DailyPerformance = Workbooks.Open("P:\DX\Daily Team Performance\Dignositc Team's Daily Performance Tracker (001).xlsx", Password:="dailyp123")
xDate = DailyPerformance.Worksheets("1359 Report").Range("L2").Value
ACD = DailyPerformance.Worksheets("Matt's Tab Two").Range("C4").Value
DailyAct = DailyPerformance.Worksheets("Matt's Tab Two").Range("E4").Value
SchedAdherence = DailyPerformance.Worksheets("Matt's Tab Two").Range("F4").Value
Status = DailyPerformance.Worksheets("1359 Report Summary").Range("B5").Value
RowCount = TWb.Worksheets("Data").Cells(Rows.Count,2).End(xlUp).Row
With TWb.Worksheets("Data").Range("B1")
.Offset(RowCount, 0) = xDate
.Offset(RowCount, 1) = ACD
.Offset(RowCount, 2) = DailyAct
.Offset(RowCount, 3) = SchedAdherence
.Offset(RowCount, 4) = Status
End With
TWb.Save
DailyPerformance.Close
End Sub
答案 2 :(得分:0)
尝试将声明从Double
更改为String
。此外,您不必做那么多选择。
试试这个......
Private Sub CommandButton2_Click()
TrackingReport = ActiveWorkbook.Name
Dim xDate As String
Dim ACD As String
Dim Untouched As String
Dim DailyAct As String
Dim SchedAdherence As String
Dim Status As String
Dim RowCount As Integer
Dim DailyPerformance As Excel.Workbook
Set DailyPerformance = Workbooks.Open("P:\DX\Daily Team Performance\Dignositc Team's Daily Performance Tracker (001).xlsx", Password:="dailyp123")
xDate = Worksheets("1359 Report").Range("L2").Value
ACD = Worksheets("Matt's Tab Two").Range("C4").Value
DailyAct = Worksheets("Matt's Tab Two").Range("E4").Value
SchedAdherence = Worksheets("Matt's Tab Two").Range("F4").Value
Status = Worksheets("1359 Report Summary").Range("B5").Value
Workbooks(TrackingReport).Activate
Set TrackinReport = ActiveWorkbook
RowCount = Worksheets("Data").Range("B1").CurrentRegion.Rows.Count
With Worksheets("Data").Range("B1")
.Offset(RowCount, 0) = xDate
.Offset(RowCount, 1) = ACD
.Offset(RowCount, 2) = DailyAct
.Offset(RowCount, 3) = SchedAdherence
.Offset(RowCount, 4) = Status
End With
Workbooks(TrackingReport).Save
End Sub
答案 3 :(得分:0)
使用变量数组方法,编辑Moacir代码。以上代码很好。只授予此代码。
Private Sub CommandButton2_Click()
Dim DailyPerformance As Workbook
Dim TWb As Workbook
Dim vR(1 To 5) As Variant '<~~ variant array
Set TWb = ThisWorkbook
Set DailyPerformance = Workbooks.Open("P:\DX\Daily Team Performance\Dignositc Team's Daily Performance Tracker (001).xlsx", Password:="dailyp123")
With DailyPerformance
vR(1) = .Worksheets("1359 Report").Range("L2").Value
vR(2) = .Worksheets("Matt's Tab Two").Range("C4").Value
vR(3) = .Worksheets("Matt's Tab Two").Range("E4").Value
vR(4) = DailyPerformance.Worksheets("Matt's Tab Two").Range("F4").Value
vR(5) = DailyPerformance.Worksheets("1359 Report Summary").Range("B5").Value
RowCount = TWb.Worksheets("Data").Cells(Rows.Count, 2).End(xlUp).Row + 1
TWb.Worksheets("Data").Range("B" & RowCount).Resize(1, 5) = vR
TWb.Save
DailyPerformance.Close (0)
End Sub