两张纸之间的循环

时间:2019-07-09 12:44:54

标签: excel vba loops for-loop

我遇到了RUn时间错误,同时运行了一个工作表中的循环副本值并将其粘贴到另一工作表中。 这是我的代码;

 Option Explicit

 Sub TeknikerData()

 Dim Lag As String
 Dim lr1 As long
 Dim lr2 As Long
 Dim i As Integer
 Dim j As Integer
 Dim wh1 As Worksheet
 Dim wh2 As Worksheet

 Set wh1 = Sheets("Data")
 Set wh2 = Sheets("Dashboard")
 lr1 = wh1.Cells(Rows.Count, 4).End(xlUp).Row


 Lag = wh2.Cells(12, 1).Value

 For i = 2 To lr1
 If wh1.Cells(i, 4) = Lag Then
 wh1.Range(Cells(i, 2), Cells(i, 2)).Copy
 wh2.Range("B50").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats



  End If

  Next i



  End Sub

我知道在运行循环时激活正确的工作表似乎存在问题,但我无法解决

2 个答案:

答案 0 :(得分:2)

似乎您已将for循环的上限声明为字符串而不是数字数据类型,请参见下文。

 Dim lr1 As String

应更改为

Dim lr1 As Long

我也刚刚注意到

wh1.Range(Cells(i, 2), Cells(i, 2)).Copy

应该是

wh1.Range(wh1.Cells(i, 2), wh1.Cells(i, 2)).Copy

答案 1 :(得分:0)

您遇到哪行错误?

您是要在for循环中使用j吗?

wh1.Range(Cells(i, 2), Cells(i, 2)).Copy '<- are one of these i's supposed to be j?