如何从多个工作表获取数据到用户表单文本框

时间:2017-05-13 05:03:18

标签: vba excel-vba excel

我有一个userform,我必须从几张纸到userform中的文本框获取数据。我的编码只是从活动表中传输数据。

我的userform中有一个多页面,其中page1(部分信息)包含7个文本框,第2页(股票信息)包含5个文本框。所有这些文本框都是从excel表填充信息。

一旦我键入textbox1中的partno,多重中的所有文本框都必须自动填充。我在获取第1页的数据方面没有任何问题,因为第1页中的所有数据都来自第1页(部分详细信息)。

但在第2页中,我需要来自以下数据:

  • textbox12的sheet4(股票更新),
  • sheetbox(发货更新)for textbox13,
  • sheetbox(拒绝更新)for textbox14,

请检查我的代码并告诉我必须做的更改。

感谢。

以下是我的代码和用户表单图片。

Sub GetData()
    '--------------------------------------
    ' Check database for entry in Textbox1 _
     and if in DB then populate other TB _
     and image
    '--------------------------------------

    Dim j As Integer
    Dim rFound As Range
    Dim wsData1 As Worksheet
    Dim shImage As Shape
    Dim sID As String
    Dim wsData2 As Worksheet

    Set wsData1 = ActiveWorkbook.Sheets("Part Details")     
    With wsData1
       ' Get the item number in sID
        sID = Me.TextBoxs1.Value
        ' check column A of the datasheet for the entry
        Set rFound = Columns("A").Find(what:=sID, _
                                    after:=.Cells(1, 1))
        ' if found, process. else quit
        If Not rFound Is Nothing Then ' This checks that rFound is set to an object and not 'nothing'
            ' Load the details in the text boxes
            For j = 2 To 11
                Me.Controls("TextBoxs" & j).Value = rFound.Offset(0, j -1).Value
            Next j

            ' load the image into the image holder
            ' rFound.Row is the row where we need to look for the data
             Set shImage = GetImage(rFound.Row)
             If Not shImage Is Nothing Then
                 ' valid image found
                 shImage.Copy
                 Set Image1.Picture = PastePicture(xlPicture)
             End If
         Else
            ClearForm bAll:=False ' keep text in textbox1
         End If
     End With

    '----------------------------------------------------------------
    ' IM STUCKED FROM HERE
    '----------------------------------------------------------------       

    Set wsData2 = ActiveWorkbook.Sheets("Stock Update")
    With wsData2
        ' Get the item number in sID
        sID = Me.TextBoxs1.Value

        ' check column A of the datasheet for the entry
        Set rFound = Columns("A").Find(what:=sID, _
                                    after:=.Cells(1, 1))
        ' if found, process. else quit
        If Not rFound Is Nothing Then ' This checks that rFound is set to an 
                                        object and not 'nothing'

            ' Load the details in the text boxes
            TextBoxs12.Value = rFound.Offset(0, 3).Value
        End If
    End With
End Sub

Page1

Page2

1 个答案:

答案 0 :(得分:1)

您的代码

Set rFound = Columns("A").Find(what:=sID, after:=.Cells(1, 1))

指的是活动工作表的A列。如果您希望它引用WsData1的A列,则必须在列引用之前加上句点,例如.Columns("A").Find。完成后,您可以使用与指定的任何数据表相同的设置。