Excel中多个图像的Vlookup

deyfvvtc  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(115)

我有一个包含产品所有细节的主表,包括它的图像。目前,如果我想根据客户的产品要求向他们发送报价或库存报告,下面是它的流程-

  • 客户向我发送产品SKU(项目编号)列表
  • 我创建了一个新的Excel表并输入产品SKU。
  • 使用Vlookup,我从主表中填充其他产品的详细信息。但是对于图像,Vlookup不起作用。因此,我必须为每个产品手动复制和粘贴相应产品的图像。

我想自动化这个过程。我屏幕录制了一个简短的视频,我希望它如何工作。请检查。Link to video
如您所见,当我在第二个工作表中输入产品SKU后,所有详细信息都将使用vlookup自动填充,但对于图像,我需要手动复制粘贴。因此,我正在寻找VBA代码,该代码可以自动从主工作表中获取与产品SKU对应的图像,并将其粘贴到单元格中,与vlookup完全相同。而且也适用于输入SKU的所有行(就像在vlookup中一样,我只需向下拖动公式,当第一列中没有数据时,它显示#N/A,但一旦数据在那里,它会自动从主表中填充数据。我对图像也想要同样的东西)。
现在,如果我从客户那里得到一个产品SKU列表,我只需要在第一列中输入它,所有其他的细节都会自动填充,包括图片。这可以为大量的产品列表保存大量的时间。
我还在视频中展示了主表单中的图像名称与产品SKU相对应。
我希望我正确地说明了我的观点,如果我的英语很难理解,我道歉。

xfb7svmp

xfb7svmp1#

请使用下一个代码。您应该将strSKU修改为您的真实的产品代码。但是该产品代码必须存在于图片应复制的表单的B:B中

Sub copyPicturesFromMaster()
   Dim wsM As Worksheet, wsOf As Worksheet, sh As Shape, shP As Shape, strSKU As String, rngSKU As Range
   
   strSKU = "123ABC"      'use here your SKU code
   Set wsM = ActiveSheet  'use here your master sheet (maybe Worksheets("Master")
   Set wsOf = wsM.Next     'use here the sheet where you need to paste the copied picture
   
   For Each sh In wsM.Shapes 'iterate between master sheet shapes:
        If TypeName(sh.OLEFormat.Object) = "Picture" And sh.name = strSKU Then 'if its name is the searched SKU and is a Picture
            'find the cell where the SKU product code exists (in B:B):
            Set rngSKU = wsOf.Range("B:B").Find(What:=strSKU, After:=wsOf.Range("B2"), LookIn:=xlValues, LookAt:=xlWhole)
            If Not rngSKU Is Nothing Then  'if it has been found:
                sh.Copy: wsOf.Paste                'copy - paste the necessary shape
                
                Set shP = wsOf.Shapes(wsOf.Shapes.count) 'set the last copied sheet
                shP.left = rngSKU.Offset(, 1).left 'move it in the right place
                shP.top = rngSKU.Offset(, 1).top
            Else
                MsgBox "Product """ & strSKU & """ could not be found in B:B column..." 'if no SKU code in columln B:B
            End If
            Exit For
        End If
   Next sh
   Debug.Print TypeName(Selection)
End Sub

编辑

下一个版本的事件代码不需要任何Vlookup公式。无论如何,最好复制不带公式的工作表,以不存在的工作簿为目标:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim wsM As Worksheet, wsOf As Worksheet, sh As Shape, shp As Shape, strSKU As String
    Dim sHeight As Double, sWidth As Double, rngProduct As Range, i As Long
    
    If Target.Value = "" Then Exit Sub
    If Target.column = 1 Then
        If Target.cells.count > 1 Then MsgBox "This code works only for a single cell (in column A:A) modification)!": Exit Sub
        Set wsM = Worksheets("Sheet1")  'use here your master sheet
        Set wsOf = Me                   'the active sheet (this one)
        'find the product code introduced in the offer sheet:
        Set rngProduct = wsM.Range("A:A").Find(What:=Target.Value, After:=wsM.Range("A1"), LookIn:=xlValues, LookAt:=xlWhole)
        If rngProduct Is Nothing Then MsgBox "No product """ & Target.Value & """ found in the master sheet": Exit Sub
        
        'Copy the fields brought until now using Vlookup:
        Application.EnableEvents = False
         For i = 2 To 4 'it copies the next three columns after B:B. If more columns necessary to be copied, increas from 4 to  necessary
              Target.Offset(, i).Value = rngProduct.Offset(, i).Value
         Next i
        Application.EnableEvents = True
        
        'format C:C column as text (even aleready having numbers formatted as scientifique:
        Me.UsedRange.Columns(3).EntireColumn.TextToColumns FieldInfo:=Array(1, 2)

        For Each sh In wsM.Shapes 'iterate between master sheet shapes:
             If TypeName(sh.OLEFormat.Object) = "Picture" And sh.name = Target.Value Then 'if its name is the searched SKU and is a Picture
                     sh.Copy:                     'copy  the necessary shape
                    Application.Wait Now + TimeValue("00:00:01")
                    wsOf.Paste
                    Set shp = wsOf.Shapes(wsOf.Shapes.count) 'set the last copied/created shape
                                      
                     sHeight = shp.height: sWidth = shp.width 'extract initial height and width
                     
                     'determine which dimension should be diminished, to be sure that both of them are inside the cell:
                     If shp.height < Target.Offset(, 1).height And shp.width < Target.Offset(, 1).width Then
                                If shp.height > shp.width Then
                                  shp.height = Target.Offset(, 1).height - 2
                                  If shp.width > Target.Offset(, 1).width Then shp.width = Target.Offset(, 1).width
                                  sWidth = shp.width: sHeight = shp.height
                            Else
                                 shp.width = Target.Offset(, 1).width - 2
                                 If shp.height > Target.Offset(, 1).height Then shp.height = Target.Offset(, 1).height
                                 sWidth = shp.width: sHeight = shp.height
                            End If
                     ElseIf shp.height < Target.Offset(, 1).height And shp.width > Target.Offset(, 1).width Then
                               shp.width = Target.Offset(, 1).width - 2: sWidth = shp.width: sHeight = shp.height:: sWidth = shp.width
                     ElseIf shp.height > Target.Offset(, 1).height And shp.width > Target.Offset(, 1).width Then
                            If shp.height > shp.width Then
                                  shp.height = Target.Offset(, 1).height - 2
                                  If shp.width > Target.Offset(, 1).width Then shp.width = Target.Offset(, 1).width
                                  sWidth = shp.width: sHeight = shp.height
                            Else
                                 shp.width = Target.Offset(, 1).width - 2:
                                 If shp.height > Target.Offset(, 1).height Then shp.height = Target.Offset(, 1).height
                                 sWidth = shp.width: sHeight = shp.height
                            End If
                     End If
                     
                     'set the correct top and left, to be centered on cell:
                      shp.top = Target.Offset(, 1).top + (Target.Offset(, 1).height - sHeight) / 2
                      shp.left = Target.Offset(, 1).left + Target.Offset(, 1) + (Target.Offset(, 1).width - sWidth) / 2
                     Exit For
             End If
        Next sh
    End If
End Sub

请在测试后发送一些反馈。

相关问题