将值插入2张

时间:2014-04-30 19:21:20

标签: excel vba excel-vba

我希望能够在2张纸上添加多个数据。有了这个我有一个错误:

  

运行时错误' 91' :对象变量或未设置块

参考这一行:

With Sheetclient = ThisWorkbook.Sheets(CMB_Test.Value)

第一页是通过组合框值选择的,它运行良好,第二页将自动 页面:" testbit"。

  Private Sub Save_test_Click()

Dim Sheetclient As Worksheet
Dim testbit1 As Worksheet
Dim nr As Integer, lr As Integer

 With Sheetclient = ThisWorkbook.Sheets(CMB_Test.Value)
 nr = Sheetclient.Cells(Rows.Count, 1).End(xlUp).Row + 1
 Sheetclient.Cells(nr, 5) = Me.TB_dateBit.Value
 Sheetclient.Cells(nr, 6) = Me.serial.Value
 Sheetclient.Cells(nr, 7) = Me.matrice.Value
 Sheetclient.Cells(nr, 8) = Me.CMB_config.Value
 Sheetclient.Cells(nr, 9) = Me.lifetime.Value

 End With

 With testbit1 = ThisWorkbook.Sheets("testbit")
 nr = testbit1.Cells(Rows.Count, 1).End(xlUp).Row + 1
 testbit1.Cells(nr, 1) = Me.TB_dateBit.Value
 testbit1.Cells(nr, 2) = Me.serial.Value
 testbit1.Cells(nr, 3) = Me.matrice.Value
 testbit1.Cells(nr, 4) = Me.CMB_config.Value
 testbit1.Cells(nr, 5) = Me.lifetime.Value
 End with

 End

 End Sub

1 个答案:

答案 0 :(得分:2)

您需要Set该对象 - 并且拥有Set,您可以使用.来引用它。因此,您的代码可能如下所示:

Set Sheetclient = ThisWorkbook.Sheets(CMB_Test.Value)
With Sheetclient
  nr = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  .Cells(nr, 5) = Me.TB_dateBit.Value
  .Cells(nr, 6) = Me.serial.Value
  .Cells(nr, 7) = Me.matrice.Value
  .Cells(nr, 8) = Me.CMB_config.Value
  .Cells(nr, 9) = Me.lifetime.Value
End With

代码的第二部分相同