我在使用直接引用时遇到问题。我试图在不使用select或activate的情况下将值放在根工作簿中。但是,如果需要复制数据的工作表不活动,我的代码会出错。如果工作表处于活动状态,则代码正常工作......
这是我的代码
Workbooks(root).Activate
Dim wb As Workbook
Set wb = Application.Workbooks(root)
wb.Sheets("d_eff_stress").Cells(1, n).Value = filename
wb.Sheets("d_eff_stress").Range(Cells(3, n), Cells(100, n)).Value = varray1
wb.Sheets("eff_stress").Cells(1, n).Value = filename
wb.Sheets("eff_stress").Range(Cells(3, n), Cells(100, n)).Value = varray2
解决方案:完全参考范围...我可以知道
Workbooks(root).Activate
Dim wb As Workbook
Set wb = Application.Workbooks(root)
wb.Sheets("d_eff_stress").Cells(1, n).Value = filename
wb.Sheets("d_eff_stress").Range(wb.Sheets("d_eff_stress").Cells(3, n), wb.Sheets("d_eff_stress").Cells(100, n)).Value = varray1
wb.Sheets("eff_stress").Cells(1, n).Value = filename
wb.Sheets("eff_stress").Range(wb.Sheets("eff_stress").Cells(3, n), wb.Sheets("eff_stress").Cells(100, n)).Value = varray2
答案 0 :(得分:3)
您需要将工作表父级分配给范围内的Cells
:
wb.Sheets("d_eff_stress").Range(wb.Sheets("d_eff_stress").Cells(3, n), wb.Sheets("d_eff_stress").Cells(100, n)).Value
使用With
块进行更少的输入:
Workbooks(root).Activate
Dim wb As Workbook
Set wb = Application.Workbooks(root)
With wb.Sheets("d_eff_stress")
.Cells(1, n).Value = filename
.Range(.Cells(3, n), .Cells(100, n)).Value = varray1
End With
With wb.Sheets("eff_stress")
.Cells(1, n).Value = filename
.Range(.Cells(3, n), .Cells(100, n)).Value = varray2
End With