简体   繁体   English

vba / excel-根据用户输入到sheet1的单元格中,从sheet2的值填充sheet1中的单元格

[英]vba/excel - populate cells in sheet1 from values in sheet2 based on user input into cells on sheet1

I have created a generic excel file to help demonstrate what I'm looking to do. 我创建了一个通用的excel文件,以帮助演示我要执行的操作。 The file I've named Tool.xlsm contains two worksheets; 我命名为Tool.xlsm的文件包含两个工作表。 Sheet1 and Sheet2. Sheet1和Sheet2。 Sheet1 will be designed to have a few fields which will accept user input. Sheet1将被设计为具有一些将接受用户输入的字段。 Sheet2 will be hidden from the user, but will contain the various drop down list selection options and their corresponding descriptions which should be displayed in another cell on Sheet1 when a specific code is selected. Sheet2将对用户隐藏,但将包含各种下拉列表选择选项及其相应的描述,当选择特定代码时,这些选项应显示在Sheet1的另一个单元格中。 Additionally, Sheet2 will contain numerous ID#s in one column, and their corresponding usernames in the next column. 此外,Sheet2将在一列中包含许多ID#,而在下一列中将包含其相应的用户名。 The purpose of this is for the user to be able quickly associate an ID# with user it belongs to. 这样做的目的是使用户能够快速将ID#与它所属的用户相关联。

Here is what I have so far...I doubt I'm going about it as efficiently as I should be, but I'd greatly appreciate your all's expertise! 到目前为止,这就是我所拥有的...我怀疑我是否能像我应该的那样高效地工作,但是我将不胜感激你们的专业知识!

Sub Button1_Click()

'Based on selected value of C1, show corresponding message in J1'
'Can this be done by simply referencing the code descriptions in sheet2?'

If Range("C1") = "code 1" Then
    Range("J1") = "code 1 description"
End If

If Range("C1") = "code 2" Then
    Range("J1") = "code 2 description"
End If

'End of code selection'
End Sub

Sub Button2_Click()

'Based on ID# entered into C3, display corresponding name in J1 (Sheet2 contains ID#s with corresponding names)'
'There has to be an esier way to loop through 1000s of records and display corresponding ID# and Person''s name'
'Rather than assigning Person 1, to Range J1, I should be able to just reference the cell Sheet2!E3 but that doesn''t seem to work'

If Range("C3") = "1001" Then
    Range("J1") = "Person 1"
End If

If Range("C3") = "34349090" Then
    Range("J1") = "Person 83"
End If

'End ID# search'
End Sub

Sub Button3_Click()

'Clear unlocked cells'

End Sub

my file in dropbox 我在保管箱中的文件

To your queries: 对您的查询:

Can this be done by simply referencing the code descriptions in sheet2? 可以仅通过引用sheet2中的代码描述来完成此操作吗?

Yes. 是。 You can use VLOOKUP formula for this. 您可以为此使用VLOOKUP公式。

Likewise, you could use the VLOOKUP formula to return the names based on the IDs. 同样,您可以使用VLOOKUP公式根据ID返回名称。

Eg, assume your usernames are in column K and ID's in column J: 例如,假设您的用户名在K列中,ID在J列中:

On sheet 1, assuming the ID in cell C3, enter the formula: =VLOOKUP(C3, Sheet2!$J$K, 2, False) 在工作表1上,假定单元格C3中的ID,输入公式: =VLOOKUP(C3, Sheet2!$J$K, 2, False)

you can use the worksheet_change event. 您可以使用worksheet_change事件。 Kindly set rngFindCode & rngFindCode1 range accordingly to refer to your data in sheet2. 请相应地设置rngFindCode和rngFindCode1范围以引用sheet2中的数据。

Below is the code. 下面是代码。

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next

    Application.EnableEvents = False

    If Target.Address = "$C$1" And Target.Cells.Count = 1 And Target.Value <> "" Then

        Dim rngFindCode As Range '
        Dim cellCode As Range

        Set rngFindCode = Sheets("Sheet2").Range("C1:C100")    ' Refers to range where code is in sheet 2
        Set cellCode = rngFindCode.Find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)

        If Not cellCode Is Nothing Then
            Range("J1").Value = cellCode.Offset(0, 1).Value
        End If

    ElseIf Target.Address = "$C$3" And Target.Cells.Count = 1 And Target.Value <> "" Then

        Dim rngFindCode1 As Range '
        Dim cellCode1 As Range

        Set rngFindCode1 = Sheets("Sheet2").Range("E1:E100")    'Refers to range where name is
        Set cellCode1 = rngFindCode1.Find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)

        If Not cellCode1 Is Nothing Then
            Range("J1").Value = cellCode1.Offset(0, 1).Value
        End If

    Else
        Range("J1").Value = ""
    End If

    Application.EnableEvents = True
End Sub

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

相关问题 VBA Vlookup将值从sheet1填充到sheet2 - VBA Vlookup to populate values from sheet1 to sheet2 Excel VBA,如何使用条件将单元格/单元格从工作表 1 复制到工作表 2 - Excel VBA, How to Copy the cell/cells from sheet1 to the sheet2 with condition Excel VBA从sheet1复制单元格并将其插入到标题sheet2中 - Excel VBA copy cells from sheet1 and insert them in header sheet2 Excel-保存sheet1时,将某些单元格写入sheet2 - Excel - When saving sheet1, write certain cells to sheet2 将非空白单元格从 sheet1 复制并粘贴到 sheet2 - Copy and paste nonblank cells from sheet1 to sheet2 在 excel VBA 中复制行范围从 Sheet1 到 Sheet2 - Copy rows range from Sheet1 to Sheet2 in excel VBA Excel VBA-在sheet2中的sheet1中搜索值,并使用sheet1中的相邻值进行更新 - Excel VBA - Search for value from sheet1 in sheet2 and update with adjacent value from sheet1 Excel 中的特殊单元格从工作表 1 复制到工作表 2 并进行错误处理 - Special Cells in Excel to Copy from sheet1 to sheet2 with error handling 根据当前工作表(例如Sheet1)中的值,禁用另一工作表(例如Sheet2)上单元格的范围 - disable the range of cells on another sheet ( say Sheet2) based on the value in the current sheet(say Sheet1) 在sheet1和sheet2中搜索相同的值,然后将值从sheet1复制到sheet2 - Search for same values in sheet1 and sheet2 and copy the values from sheet1 to sheet2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM