vba Excel - 如何拥有多个输入设备 (USB)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5723220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:03:33  来源:igfitidea点击:

Excel - how to have Multiple Input Devices (USB)

excelvbaexcel-2007

提问by SergeiBubka

How do I differentiate between 2 USB input devices that are both placing data into the spreadsheet?

如何区分将数据放入电子表格的 2 个 USB 输入设备?

I have 2 USB calipers that input their current measurements as if they were keystrokes. The data is sent to excel (or any program really) and placed vertically. Currently, Excel treats both calipers as if they were the same instrument.

我有 2 个 USB 卡尺,它们输入当前的测量值,就好像它们是击键一样。数据被发送到excel(或任何程序)并垂直放置。目前,Excel 将两个卡尺视为同一种仪器。

What I would like to have is the two calipers record their data into distinct areas of the excel spreadsheet.

我想要的是两个卡尺将他们的数据记录到 Excel 电子表格的不同区域。

An example would be Tool 1 and Tool 2 are both being used simultaneously by two different people while both connected to the same computer. Tool 1 will always put its data in the following areas: (A1,B1,C1, A2,B2,C2 ... etc.) Tool 2 will record its data into the following areas: (A5,B5,C5, A6,B6,C6 ... etc.) The data does not have to go into the same sheet, it can be in different sheets or whatever is easiest.

例如,工具 1 和工具 2 都由两个不同的人同时使用,同时都连接到同一台计算机。工具 1 将始终将其数据放在以下区域:(A1、B1、C1、A2、B2、C2 ...等)工具 2 会将其数据记录到以下区域:(A5、B5、C5、A6、 B6,C6 ...等)数据不必进入同一张表,它可以在不同的表或任何最简单的表中。

Can anyone help?

任何人都可以帮忙吗?



updated to include content originally provided as an answer

A few questions:

更新以包含最初作为答案提供的内容

几个问题:

1) Where can I find the information for the "Scripting.FileSystemObject"? I assume this line needs to be replaced with an identifier for each of the individual usb tools. In device manager they are listed as HID Keyboards but I see no identifying number.

1) 在哪里可以找到“Scripting.FileSystemObject”的信息?我假设这一行需要替换为每个单独的 USB 工具的标识符。在设备管理器中,它们被列为 HID 键盘,但我看不到识别号。

2) Do I just copy-paste the above three sets of code into the VBA under Sheet1 for USB1 and Sheet2 for USB2, or do they need to be placed in individual macros?

2)我只是将上述三组代码复制粘贴到USB1的Sheet1和USB2的Sheet2下的VBA中,还是需要将它们放在单独的宏中?

回答by Martin

I'm assuming your USB devices simply register as Human Interface Devices and sent their output to any application that is currently in focus.

我假设您的 USB 设备只是注册为人机接口设备并将其输出发送到当前处于焦点的任何应用程序。

If that is the case, this is not trivial. Windows will merge all keyboard inputs into one input stream, there's usually no way to distinguish from which keyboard (or keyboard-like device) the input came.

如果是这样,这不是小事。Windows 会将所有键盘输入合并到一个输入流中,通常无法区分输入来自哪个键盘(或类似键盘的设备)。

That being said, there appears to be a raw input api that lets you catch input per device: http://www.codeproject.com/KB/system/rawinput.aspx

话虽如此,似乎有一个原始输入 api,可让您捕获每个设备的输入:http: //www.codeproject.com/KB/system/rawinput.aspx

In theory, you should be able to interface this API in VBA, but you have to decide if its worth the effort. The farsimpler way is to use two computers.

理论上,您应该能够在 VBA 中连接这个 API,但您必须决定它是否值得付出努力。在更简单的方法是使用两台电脑。

回答by osknows

One way is to set up the sources as separate objects and the output sheets as objects. Then each object can be handled separately or any way you choose.

一种方法是将源设置为单独的对象,将输出表设置为对象。然后可以单独或您选择的任何方式处理每个对象。

EG USB1 should have a different filepath to USB2. Also each workbook can be an object, each sheet can be an object a range within each sheet can be an object

EG USB1 应该与 USB2 具有不同的文件路径。每个工作簿也可以是一个对象,每个工作表都可以是一个对象,每个工作表内的范围都可以是一个对象

Eg USB1

例如 USB1

Dim objFs1 As Object, objF1 As Object, objF1 As Object, objFc1 As Object
Set objFs1 = CreateObject("Scripting.FileSystemObject")
Set objF1 = objFs1.GetFolder(strFilePath)
Set objFc1 = objF1.Files

USB2

USB2

Dim objFs2 As Object, objF2 As Object, objF2 As Object, objFc2 As Object
Set objFs2 = CreateObject("Scripting.FileSystemObject")
Set objF2 = objFs2.GetFolder(strFilePath)
Set objFc2 = objF2.Files

Not sure how you want to load set USB objects but one possibility is to use something like the file dialog to select a file/folder

不确定如何加载设置的 USB 对象,但一种可能性是使用文件对话框之类的内容来选择文件/文件夹

With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Show

If .SelectedItems.Count = 0 Then
    End
End If

For lngCount = 1 To .SelectedItems.Count
    strEndofPath = InStrRev(.SelectedItems(lngCount), "\")
    strFilePath = Left(.SelectedItems(lngCount), strEndofPath)
    strFilename = Right(.SelectedItems(lngCount), Len(.SelectedItems(lngCount)) - strEndofPath)
Next lngCount
End With

Write Objects for excel

为excel编写对象

Dim objWB1 As Workbook, objSheet1 As Worksheet, rngArea1 as Range
Dim objWB2 As Workbook, objSheet2 As Worksheet, rngArea2 as Range