Vba 若单元格值与UserForm ComboBox列匹配,则复制到工作表

Vba 若单元格值与UserForm ComboBox列匹配,则复制到工作表,vba,excel,excel-2010,Vba,Excel,Excel 2010,我想做的是: 从第3行开始循环通过工作表“全局”上的Q列 对于每个单元格,将值匹配到UserFormCombox2 Column2,并将整行从userform2 Columm1复制到相关工作表 循环直到最后一行。列Q中可能有几个唯一的值,但都将在Userform2的Combobox2列中 我没有代码作为例子,因为我甚至不知道从哪里开始 这是我的comboxbox,正如它显示的一样,在它的背面,每个项目都有下面的代码,因此有一个名称、一个代码“2780”和一个参考“BRREAPERS” 我需要它

我想做的是:

  • 从第3行开始循环通过工作表“全局”上的Q列

  • 对于每个单元格,将值匹配到UserFormCombox2 Column2,并将整行从userform2 Columm1复制到相关工作表

  • 循环直到最后一行。列Q中可能有几个唯一的值,但都将在Userform2的Combobox2列中

  • 我没有代码作为例子,因为我甚至不知道从哪里开始

    这是我的comboxbox,正如它显示的一样,在它的背面,每个项目都有下面的代码,因此有一个名称、一个代码“2780”和一个参考“BRREAPERS”

    我需要它在G列的全局工作表中循环遍历everycell,然后将单元格值与第2列的combobox列表项匹配。一旦找到匹配项,它将使用第1列中的代码即“2780”将整行复制到与第1列中的代码匹配的工作表中


    希望我已经解释得更好一些。

    我已经设法让它与下面的代码一起工作。它在组合框中查找正确的单元格。然后将其复制到正确位置的正确纸张上

    Private Sub CommandButton1_Click()
    
        Dim findmatch As Object
        Dim lastcell As Integer
    
        Set findmatch = ThisWorkbook.Sheets("Global").Range("G:G").Find(What:=UserForm2.ComboBox2.column(1), LookIn:=xlValues)
        If Not findmatch Is Nothing Then
        lastcell = ThisWorkbook.Sheets(UserForm2.ComboBox2.Value).Cells(100000, 7).End(xlUp).row 'here find a way to locate last cell in sheet that has your name.. it keeps returning me 1 but other than that it works fine
        ThisWorkbook.Sheets(UserForm2.ComboBox2.Value).Range(Cells(lastcell, 1), Cells(lastcell, 40)) = Range(Cells(findmatch.row, 1), Cells(findmatch.row, 40)).Value
        Else
        MsgBox "not found"
        End If
        End Sub
    
    唯一的问题是它运行得很慢!!有人能提出一些加速的方法吗

    最后一个问题是,如果一个工作表不存在,它会告诉您创建工作表,或者甚至为您创建工作表

    我真的很感谢所有帮助我的人,已经把我的头撞在墙上好几天了

    Dim i, lastD, lastG As Long
    Dim j As Integer
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .CutCopyMode = False
    End With
    
    ' find last row
    lastG = sheets("Global").Cells(Rows.Count, "Q").End(xlUp).row
    
    For i = 3 To lastG
        lookupVal = sheets("Global").Cells(i, "Q") ' value to find
        ' loop over values in "details"
        For j = 0 To Me.ComboBox2.ListCount - 1
            currVal = Me.ComboBox2.List(j, 2)
            If lookupVal = currVal Then
            sheets("Global").Cells(i, "Q").EntireRow.Copy
            sheets(Me.ComboBox2.List(j, 1)).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Insert shift:=xlDown
            End If
        Next j
    Next i
    
     With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .CutCopyMode = True
    End With
    

    请张贴您的代码,即使它不是working@R3uK,嗨,这就是问题所在,我没有任何代码,我不知道应该从哪里开始!!如果我理解正确的话。。是否要使用与其他组合框中的选定值匹配的值填充某些组合框。。?甚至连用户表单vould帮助的屏幕截图都没有。。唯一阻碍你回答的是你的解释。编码从来都不是问题我明白我想。。那么你会有很多床单是吗?组合框中有多少个选项?因此,您希望它有一个按钮,并根据所选内容启动程序。。所以我按下按钮,它从第二列获取值。。在工作表的G列中查找匹配项。。如果找到匹配项,则将该行复制到与userform第一列上的值对应的工作表?@Lance Hi,是的,有很多工作表!您就快到了,我想将工作表上的G列与组合框中的第二列相匹配,然后将该行复制到与第1列中的值对应的工作表中。您好,这给了我一个运行时错误“381”:无法获取Colum属性。无效的属性数组索引??
    Dim i, lastD, lastG As Long
    Dim j As Integer
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .CutCopyMode = False
    End With
    
    ' find last row
    lastG = sheets("Global").Cells(Rows.Count, "Q").End(xlUp).row
    
    For i = 3 To lastG
        lookupVal = sheets("Global").Cells(i, "Q") ' value to find
        ' loop over values in "details"
        For j = 0 To Me.ComboBox2.ListCount - 1
            currVal = Me.ComboBox2.List(j, 2)
            If lookupVal = currVal Then
            sheets("Global").Cells(i, "Q").EntireRow.Copy
            sheets(Me.ComboBox2.List(j, 1)).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Insert shift:=xlDown
            End If
        Next j
    Next i
    
     With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .CutCopyMode = True
    End With