简体   繁体   English

根据Excel中的组合框值将数据从一张纸传输到另一张纸

[英]Transfer data from from one sheet to another based on combobox value in excel

I have 2 sheets and a userform in my workbook. 我的工作簿中有2张纸和一张用户表格。

Sheet1,has 3 rows which contains shift type info.(A2=A,A3=B and A4=C),I also have additional data in col B (start time),col C (end Time) and so on.. Sheet1有3行,其中包含班次类型信息。(A2 = A,A3 = B和A4 = C),我在col B(开始时间),col C(结束时间)等中还有其他数据。

I use a form to enter employee names(txtName) and their shift type(cboShift) in sheet 2. col A= name and col B=shift type) now i want when i complete the employee data in shift 2,by clicking UPDATE button their relevant shift info(start time,end time...) from sheet 1 copied into next cells. 我现在使用表格在工作表2中输入员工姓名(txtName)及其班次类型(cboShift)。col A =名称,col B =班次类型)现在我想在班次2中通过单击UPDATE按钮完成员工数据时他们将工作表1中的相关班次信息(开始时间,结束时间...)复制到下一个单元格中。

 Private Sub cmdUpdate_Click()

    Dim LastRow As Long
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rng As Range
    Dim rgnSearch As Range
    Dim rngFound As Range

    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Sheet2")

    LastRow = ws2.Range("A" & ws2.Rows2.Count).End(xlUp).row + 1
    ws2.Range("A" & LastRow).Value = Me.txtName.Text
    ws1.Range("A" & LastRow).Offset(0, 1).Value = Me.cboShift.Text

    Set rgnSearch = ws1.Range("A1", ws1.Cells(Rows2.Count, 1).End(xlUp))

    With rgnSearch
        Set rngFound = .Find(Me.txtName.Text, LookIn:=xlValues)
        If Not rngFound Is Nothing Then
            ws1.Range("C" & rngFound.row & ":F" & rngFound.row).Copy Destination:=ws2.Range("C" & LastRow)
        End If
    End With
End Sub
End Sub

I have header in both sheets. 我在两张纸上都有标题。

When i run the code i get error "method or data member not found). 当我运行代码时,出现错误“未找到方法或数据成员)。

LastRow = ws2.Range("A" & ws2.**Rows2**.Count).End(xlUp).row + 1

My next question is: is there any way to transfer data for each person when i click ADD Button instead of doing it after for all Thanx 我的下一个问题是:当我单击“添加”按钮时,是否有任何方法可以为每个人传输数据,而不是在所有Thanx之后进行数据传输?

you probably meant 你可能是说

LastRow = ws2.Range("A" & ws2.Rows.Count).End(xlUp).row + 1

instead of 代替

LastRow = ws2.Range("A" & ws2.Rows2.Count).End(xlUp).row + 1

The error caused already answered in @h2so4 above. 上面@ h2so4中已经回答的错误导致。

However, a few things to consider in your code: 但是,在代码中需要考虑以下几点:

  1. ws1.Range("A" & LastRow).Offset(0, 1).Value = Me.cboShift.Text , not sure it's the same LastRow for both ws1 ans ws2 , is it ??? ws1.Range("A" & LastRow).Offset(0, 1).Value = Me.cboShift.Text ,不知道它的同一LastRow两个ws1 ANS ws2 ,是它???

  2. Once you already have a successful find, in If Not rngFound Is Nothing Then , you can use a different coding to copy the contents of cells C:F of the same row. 成功查找后,在If Not rngFound Is Nothing Then ,可以使用其他编码来复制同一行的单元格C:F的内容。

Instead of: 代替:

ws1.Range("C" & rngFound.row & ":F" & rngFound.row).Copy Destination:=ws2.Range("C" & LastRow)

You can use: 您可以使用:

rngFound.Offset(, 2).Resize(1, 4).Copy Destination:=ws2.Range("C" & LastRow)

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

相关问题 Excel VBA - 根据条件文本字符串将数据范围从一个工作表传输到另一个工作表 - Excel VBA - Transfer Data range from one sheet to another based on conditional text string Excel 如何将数据从一张纸传输到另一张纸 - Excel how to transfer data from one sheet to another sheets VBA将数据从一张纸传输到另一张纸 - VBA Transfer of data from one sheet to another 将数据从一张纸传输到另一张纸 - Transfer data from one sheet to another excel根据另一个单元格值从另一个工作表中提取数据 - excel pulling data from another sheet based on another cell value Excel宏-根据值将单元格从一张纸复制到另一张纸 - Excel Macro - copy cells from one sheet into another based on value 根据单元格值将行从一个 Excel 工作表复制到另一个 - Copying rows from one Excel sheet to another based on cell value Excel:数据从一张纸传输到另一张纸上的下一个空行错误 - Excel: transfer data from one sheet to next empty row on another sheet error Excel 根据条件将数据从一张工作表复制到另一张工作表 - Excel copy data from one sheet to another based on a condition VBA-根据文本将数据从一张纸传输到另一张纸,然后放入另一张纸的特定位置 - VBA- Transfer data from one sheet to another based on text and placing in into a specific location in the other sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM