2015-11-03 50 views
0

我的目标是记录构建过程记录库存标签,然后跟进验证成品的检查表。由于我只在第一阶段(记录库存标签),我还有很长的路要走。将用户窗体中的数据传输到工作表

我想用我的UserForm做什么是使用手持式扫描仪扫描条形码(库存标签),并让他们填充底层的电子表格,当我点击“提交”按钮。但是,下面的代码会生成此错误:

Run Time Error '424' object required

我在做什么错?

Private Sub cmdSubmit_Click() 
    Dim eRow As Long 

     eRow = Database.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 

     Cells(eRow, 1).Value = txtDeviceID.Text 
     Cells(eRow, 2).Value = txtUserName.Text 
     Cells(eRow, 3).Value = txtUserNumber.Text 
     Cells(eRow, 4).Value = txtCloneDevice.Text 
     Cells(eRow, 5).Value = txtCartAssembly.Text 
     Cells(eRow, 6).Value = txtPC.Text 
     Cells(eRow, 7).Value = txtMonitor.Text 
     Cells(eRow, 8).Value = txtUPS.Text 
     Cells(eRow, 9).Value = txtHub.Text 
     Cells(eRow, 10).Value = txtKeyboard.Text 
     Cells(eRow, 11).Value = txtMouse.Text 
     Cells(eRow, 12).Value = txtPrinter.Text 
     Cells(eRow, 13).Value = txtWebcam.Text 
     Cells(eRow, 14).Value = txtScanner.Text 
     Cells(eRow, 15).Value = txtRFID.Text 
+0

我假设'Database'是一个工作表变量。你需要把它放在每个'... Cells ...的前面''像这样'Database.Cells ...' –

+0

数据库就是表格名称(所以我猜它也是一个变量)。将做出改变并看看它是如何工作的。 – CimpleSypher

+0

嘿Pnuts,非常感谢。我需要你的帮助和Neuralgroove。 – CimpleSypher

回答

0

两种解决方案(包括承担的“数据库”是在您的工作簿表名称)

  1. 无法通过这样的名字参考表。您必须将其背景名称设置为“数据库”(在VBA IDE中 - >项目资源管理器 - >您的项目 - > Microsoft Excel对象 - >查找名为数据库的工作表,右键单击属性 - >更改“名称”属性到“数据库”)然后,您将看到括号中的“数据库”作为项目资源管理器中Microsoft Excel对象下的基础工作表名称,您可以在代码中引用它。

  2. 其他更简单的方法是将其添加到您的代码中。

    Dim Database as Worksheet 
    Set Database = Worksheets("Database") 
    

然后,你可以参考它,你是在你的代码。

CimpleSypher是正确的,您需要将单元格调用绑定到工作表,否则它会将值放入当前处于活动状态的任何表单中。

+0

谢谢!我会做出改变并让你知道它是如何工作的。我开始时错过了那个领域。 – CimpleSypher

+0

嘿Neuralgroove, – CimpleSypher

0

Both Pnuts & Neuralgroove回答了这个问题,因为我的代码有一些问题。

由于PNUTS建议,因为这行代码表示:

eRow = Database.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 

那么后续行应该读过:

Database.Cells(eRow, 1).Value = txtDeviceID.Text 

代替我最初是如何写的:

Cells(eRow, 1).Value = txtDeviceID.Text 

我还需要将对象的名称属性更改为数据库,如Neuralgroove所述。

相关问题