简体   繁体   English

在用户输入上从一张纸到另一张纸获取数据

[英]Getting data from one sheet to another on the user input

I have an Excel workbook, and in that workbook I have a number of sheets. 我有一个Excel工作簿,在该工作簿中有许多工作表。
One is called main and the other sheet data which holds all the information. 一个被称为main data ,另一个被称为表data ,其中包含所有信息。
The data sheet has people's name and assigned numbers in the same cell. data表在同一单元格中具有人的名字和分配的号码。

eg: A2 would have: 123 Chris Smith 例如:A2会: 123 Chris Smith

What I am trying to achieve is for a user to start typing the number anywhere in the main sheet that it scans the data sheet and completes the rest of the data. 我要实现的目的是让用户开始在主表中的任何位置键入数字,以扫描数据表并完成其余数据。
So a user would type 123 and the macro would then fill out the rest 123 Chris Smith. 因此,用户将键入123,然后宏将填写其余的123克里斯·史密斯。

As already mentioned you can use the Worksheet_Change() Event. 如前所述,您可以使用Worksheet_Change()事件。 To Show you how it works, put this code in the main sheet in the editor. 为了向您展示其工作原理,请将此代码放在编辑器的main表中。 Now type any number that matches your data list and click out of the cell, it will be now completed. 现在,键入与您的数据列表匹配的任何数字,然后单击该单元格之外的位置,它现在将完成。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim sht As Worksheet
Dim rng As Range
Dim val As Variant: val = Target.Value

Set sht = Worksheets("Data")

If Not IsError(val) And Not IsArray(val) Then
    If val <> "" Then
        Set rng = sht.Range("A:A").Find(val & "*", LookAt:=xlWhole)
        If Not rng Is Nothing Then
            Application.EnableEvents = False
            Target.Value = rng.Value
            Application.EnableEvents = True
        End If
    End If
End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM