比较范围并复制整行,当某些单元格匹配时?

时间:2017-05-30 06:05:15

标签: excel-vba compare rows vba excel

我希望比较2个不同表格中的2个范围。

Sheet1("Raport")包含未详细说明的客户信息以及他们应获得的产品类型 Sheet2("Dane")包含有关客户的详细信息,应根据客户和产品列表(Sheet3("Produkt1")Sheet4("Produkt2")等将(作为1个客户=整行)复制到特定工作表中(例如Sheet1("Raport")Sub DeleteBlankRows1() Dim i As Long With Application .Calculation = xlCalculationManual .ScreenUpdating = False For i = Selection.Rows.Count To 1 Step -1 If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then Selection.Rows(i).EntireRow.Delete End If Next i .Calculation = xlCalculationAutomatic .ScreenUpdating = True End With End Sub 等。 1}})。

删除空行(有效)

Sub SelectBetween()
    Dim findrow As Long, findrow2 As Long

    findrow = Range("B:B").Find("Produkt1", Range("B1")).Row
    findrow2 = Range("B:B").Find("Laczna ilosc Produkt1", Range("B" & findrow)).Row
    Range("B" & findrow + 1 & ":M" & findrow2 - 1).Select
End Sub

Produkt1的范围(工作)

Sub SelectBetween2()
    Dim findrow As Long, findrow2 As Long

    findrow = Range("B:B").Find("Produkt2", Range("B1")).Row
    findrow2 = Range("B:B").Find("Laczna ilosc Produkt2", Range("B" & findrow)).Row
    Range("B" & findrow + 1 & ":M" & findrow2 - 1).Select
End Sub

Produkt2的范围(工作)

Sub Compare()
    Dim w1 As Worksheet, w2 As Worksheet, w3 As Worksheet
    Dim findrow1 As Long, findrow2 As Long
    Dim range1 As Range, range2 As Range, c As Range

    Set w1 = Worksheets("Raport")
    Set w2 = Worksheets("Dane")
    Set w3 = Worksheets("Produkt1")

    findrow1 = w1.Range("B:B").Find("Produkt2", w1.Range("B1")).Row
    findrow2 = w1.Range("B:B").Find("Laczna ilosc Produkt2", w1.Range("B" & findrow1)).Row
    Set range1 = w1.Range("B" & findrow1 + 1 & ":M" & findrow2 - 1)
    Set range2 = w2.Range("2:137")

    If range1 = w2.range2 Then
        range2.EntireRow.Copy w3.Cells(Rows.Count, 1).End(xlUp)(2)
    End If
End Sub

我该怎么写"如果"比较表格并将详细的客户信息复制到另一张表格?

C:\Users\RedCode\PycharmProjects\DatabaseExample>python setup.py build
running build
running build_exe
Traceback (most recent call last):
  File "setup.py", line 26, in <module>
    executables = [Executable("cardb.py", base = base)])
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
    distutils.core.setup(**attrs)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\command\build.py", line 135, in run
    self.run_command(cmd_name)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\dist.py", line 219, in run
    freezer.Freeze()
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\freezer.py", line 623, in Freeze
    self._FreezeExecutable(executable)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\freezer.py", line 199, in _FreezeExecutable
    finder.IncludeFile(exe.script, exe.moduleName)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 633, in IncludeFile
    deferredImports)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 474, in _LoadModule
    self._ScanCode(module.code, module, deferredImports)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 564, in _ScanCode
    module, relativeImportIndex)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 310, in _ImportModule
    deferredImports, namespace = namespace)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 403, in _InternalImportModule
    parentModule, namespace)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 416, in _LoadModule
    namespace)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 485, in _LoadPackage
    self._LoadModule(name, fp, path, info, deferredImports, parent)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 463, in _LoadModule
    self._RunHook("load", module.name, module)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 536, in _RunHook
    method(self, *args)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\hooks.py", line 613, in load_tkinter
    tclSourceDir = os.environ["TCL_LIBRARY"]
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'TCL_LIBRARY'

在附件中有一个包含最终结果的文件(详细的客户信息只是在不使用宏的情况下复制到Produkt1和Produkt2表中)。 - &GT; https://uploadfiles.io/ttmck

1 个答案:

答案 0 :(得分:0)

使用

复制所需范围后
range2.EntireRow.Copy

下一行应粘贴:

Worksheets(1).Paste Destination:=Worksheets(1).Cells(Rows.Count, 1).End(xlUp)(2, 1)

Worksheets(1)替换为您的目的地。这会将所有复制的行放在目标表上的连续行中,最后可能需要RemoveDuplicates到该范围。