VBA错误如果对象'范围'插入表时失败,则为'_Default'

我正在链接到位于networkingpath上的Excel工作簿UserForm。 在笔记本中,我有一个名为源的表。 该表包含一个ID和源名称。 在用户窗体中有一个button来添加一个新的源到表中。 我现在的VBA如下:

Private Sub bFinishAdd_Click() Dim wb As Workbook Dim ws As Worksheet Dim rng As Range Dim lr As Long Set wb = Workbooks.Open("\\datapath\datasub1\datasub2\filename.xlsx") Set ws = wb.Worksheets("Source") Set rng = ws.Range("Source[Source]") If tbNewSourceName <> "" Then If Application.WorksheetFunction.CountIf(rng, tbNewSourceName) > 0 Then MsgBox "Source System already exists!" lbSourceSystems.Enabled = True bAddSource.Enabled = True frameAddSource.Enabled = False lblNewSourceName.Enabled = False bFinishAdd.Enabled = False bCancelAdd.Enabled = False tbNewSourceName = "" tbNewSourceName.Enabled = False tbNewSourceName.BorderStyle = fmBorderStyleNone Exit Sub Else lr = ws.Cells(Rows.Count, 1).End(xlUp).Row ws.Cells(lr + 1, 1) = lr - 1 + 1000 ws.Cells(lr + 1, 2) = tbNewSourceName End If End If End Sub 

如果对象'范围'失败',则添加新源会触发错误“Method of _Default”。 Excel只是崩溃,我无法debugging,但我知道错误是由以下原因造成的:

  ws.Cells(lr + 1, 1) = lr - 1 + 1000 ws.Cells(lr + 1, 2) = tbNewSourceName 

但是,我不明白为什么我收到错误或如何解决它。 有任何想法吗?

显然使用默认的方法出错了。 您不能将文本框分配给单元格。 所以要明确。 尝试:

 ws.Cells(lr + 1, 1).Value = lr - 1 + 1000 ws.Cells(lr + 1, 2).Value = tbNewSourceName.Text 

我发现我试图添加到同一个表被列为我的用户窗体上的列表框的行来源。 我更新了代码之前添加并添加它之前删除RowSource。

  lbSourceSystems.RowSource = "" lr = ws.Cells(Rows.Count, 1).End(xlUp).Row ws.Cells(lr + 1, 1).Value = lr - 1 + 1000 ws.Cells(lr + 1, 2).Value = tbNewSourceName.Text lbSourceSystems.RowSource = ("'filename.xlsx'!SourceSystems")