我是Excel VBA的新手。
我的目标是创建一个命令按钮来打开受密码保护的工作簿,自动从特定工作表中的选定单元格中复制数据并粘贴到不受保护的工作簿中。当受保护工作簿中的数据每天更改时,还需要将数据放在下一个空行中。
我无法绕过范围错误
Worksheets("Sheet14").Select
xDate = Range("L2")
运行时错误'9':下标超出范围。
Private Sub CommandButton2_Click()
Workbooks.Open Filename:="P:\source.xlsx", Password:="password"
Dim xDate As String
Dim ACD As String
Dim Untouched As String
Dim DailyAct As String
Dim SchedAdherence As String
Dim Status As String
Worksheets("Sheet14").Select
xDate = Range("L2")
Worksheets("Sheet5").Select
ACD = Range("C4")
Worksheets("Sheet5").Select
DailyAct = Range("E4")
Worksheets("Sheet5").Select
SchedAdherence = Range("F4")
Worksheets("Sheet7").Select
Status = Range("B5")
Set TrackingReport = Workbooks("C:\Destination"): TrackingReport.Activate
Worksheets("Sheet2").Select
Worksheets("Sheet2").Range("B1").Select
RowCount = Worksheets("Sheet2").Range("B1").CurrentRegion.Row.Count
With Worksheets("Sheet2").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
Set DailyPerformance = Workbooks.Open("P:\source.xlsx",Password:="password")
DailyPerformance.Close
End Sub
答案 0 :(得分:0)
Private Sub CommandButton2_Click()
Workbooks.Open Filename:="P:\source.xlsx", Password:="password"
Dim xDate As String
Dim ACD As String
Dim Untouched As String
Dim DailyAct As String
Dim SchedAdherence As String
Dim Status As String
Worksheets("Sheet14").Select ' Change this sheet name
xDate = Range("L2")
Worksheets("Sheet5").Select
ACD = Range("C4")
Worksheets("Sheet5").Select
DailyAct = Range("E4")
Worksheets("Sheet5").Select
SchedAdherence = Range("F4")
Worksheets("Sheet7").Select
Status = Range("B5")
Set TrackingReport = Workbooks("C:\Destination"): TrackingReport.Activate ' `CHANGE THIS TO Workbooks.Open Filename:="P:\TrackingReport.xlsx" (or where ever it is saved)`
Worksheets("Sheet2").Select
Worksheets("Sheet2").Range("B1").Select
RowCount = Worksheets("Sheet2").Range("B1").CurrentRegion.Row.Count ' Here im assuming you are find the last row you to know where to paste from if so i would use something like this instead
Cells(Rows.Count, 1).End(xlUp).Row ' This looks at your selected column in this case columns 1. Hoowver you would need to ensure that is is a column that is always filled in
With Worksheets("Sheet2").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
Set DailyPerformance = Workbooks.Open("P:\source.xlsx",Password:="password")
DailyPerformance.Close
End Sub
在测试工作簿中,我创建了这个工作:
Private Sub CommandButton2_Click()
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 TrackingReport As Excel.Workbook
Dim DailyPerformance As Excel.Workbook
Set DailyPerformance = Workbooks.Open("P:\source.xlsx", Password:="password")
'Change you sheet names here
Worksheets("Sheet1").Select
xDate = Range("V2").Value
Worksheets("Sheet1").Select
ACD = Range("C4").Value
Worksheets("Sheet1").Select
DailyAct = Range("E4").Value
Worksheets("Sheet1").Select
SchedAdherence = Range("F4").Value
Worksheets("Sheet1").Select
Status = Range("B5").Value
Set TrackingReport = Workbooks.Open("YourPathHere")
Worksheets("Sheet1").Select
Worksheets("Sheet1").Range("B1").Select
RowCount = Cells(Rows.Count, 1).End(xlUp).Row
With Worksheets("Sheet1").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
End Sub