VBA从另一张纸上的特定列提取数据

时间:2020-02-18 06:53:21

标签: excel vba

我从系统中提取了2张纸。对于sheet1(Data)包含多列和所有内部数据。对于sheet2(Get),我有2列,如下所示。使用sheet2(Get)上的第2号参考列(ID),我想在sheet1(Data)中搜索此值,然后提取特定的列值。我尝试在网上搜索示例代码,并发现这段提取所有列值的代码。但是我只想提取黄色突出显示的列,然后将此值提取到sheet2(Get)中。能帮我修改此代码吗?

注意:对于sheet2(Get),A列和B列上的所有数据均已预填充,因此我想将Worksheet_SelectionChange更改为普通子项,然后使用宏运行该子项。可能吗?

Colomn C (Get sheet) should extract from Colomn B (Data sheet)
Colomn D (Get sheet) should extract from Colomn M (Data sheet)
Colomn E (Get sheet) should extract from Colomn J (Data sheet)
Colomn F (Get sheet) should extract from Colomn L (Data sheet)
Colomn G (Get sheet) should extract from Colomn C (Data sheet)
Colomn H (Get sheet) should extract from Colomn G (Data sheet)

sheet2(获取) enter image description here

sheet1(数据) enter image description here

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim k As Integer, i As Long, Str As Range
    'row number
    For i = 3 To Sheets("GetData").Range("a65536").End(3).Row
        Set Str = Sheets("Data").Range("a:a").Find(Cells(i, 1).Value, , xlValues, xlWhole)
        If Not Str Is Nothing Then
            'column number
            For k = 1 To 14
                If k > 1 Then Sheets("GetData").Cells(i, k).Value = Sheets("Data").Cells(Str.Row, k).Value
            Next k
        Else
            For k = 2 To 14

                Sheets("GetData").Cells(i, k).Value = "Null"
            Next k

        End If
    Next i
End Sub

2 个答案:

答案 0 :(得分:0)

一旦您拥有该行,就可以像这样复制:

Dim col As Long
'...
'...
col = 3
For Each e In Array(2, 3, 7, 10) 'columns to fetch
    Sheets("GetData").Cells(i, col).Value = Str.EntireRow.Cells(e).Value
    col = col + 1
Next 
'...
'...

答案 1 :(得分:0)

application_title="imaging"
main_python_file="untitled.py"
import sys
from cx_Freeze import setup,Executable
base= None
if sys.platform=="win32":
   base="Win32GUI"
includes=["atexit","re"]
build_exe_options = {"packages": ['pygments.lexers', 'tvtk.pyface.ui.qt4','pkg_resources._vendor',
                'mayavi', 'traits', 'traitsui', 'sip',' 
                traitlets','tvtk.vtk_module','traits.api','traitsui.api','os','gui','gui.gui_mayavi', 
                'tvtk.vtk_module',
                             'pyface.qt','pyface.qt.QtGui','pyface.qt.QtCore','numpy'],
                "includes":['gui','gui.gui_mayavi','mayavi','PyQt4'],
              }
setup(  
    name=application_title,
    version="0.1",
    description="Simple test",
    options={"build.exe" : {"includes":includes}},
    executables=[Executable(main_python_file, base = base)])

谢谢蒂姆·威廉姆斯的帮助。