我有两个工作簿,一个是活动列表(数据库),另一个是项目跟踪器(仪表板)。
两个工作簿都有一个项目ID。
我希望工作簿和活动列表应该有一个循环来匹配确切的项目ID。
如果在活动列表中找到项目ID,它将从该行检索信息并覆盖项目跟踪器中包含该项目ID的现有行。
这是我所做的代码的一个例子,我做了一些相关的事情,但它似乎不起作用:
MyObject
答案 0 :(得分:1)
您误解了如何使用Range
对象。此.Range("A").Value
不起作用,您还需要包含行号,例如.Range("A1").Value
。
您的逻辑假定两个列表的顺序完全相同。使用Range.Find
方法解决了这个问题。
Sub AAA()
Dim source As Worksheet
Dim target As Worksheet
Dim cell As Range
Dim cellFound As Range
Set target = Workbooks("Target.xlsm").Sheets("Sheet1")
Set source = Workbooks("Source.xlsm").Sheets("Sheet2")
For Each cell In target.Range("A2:A50")
' Try to find this value in the source sheet
Set cellFound = source.Range("A:A").Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
If Not cellFound Is Nothing Then
' A matching value was found
' So copy the cell 2 columns across to the cell adjacent to matching value
' Do a "normal" copy & paste
cell.Offset(ColumnOffset:=2).Copy cellFound.Offset(ColumnOffset:=1)
' Or do a copy & paste special values
'cell.Offset(ColumnOffset:=2).Copy
'cellFound.Offset(ColumnOffset:=1).PasteSpecial xlPasteValues
Else
' The value in this cell does not exist in the source
' Should anything be done?
End If
Next
End Sub
您是否知道自己使用source
和target
的不同工作表?
答案 1 :(得分:0)
target.Activate
For a = 2 To 50
If source.Range("A" & a).Value = target.Range("A" & a).Value Then
target.Range("C" & a).EntireRow.Select
Selection.Copy
source.Range("B" & a).PasteSpecial
End If
接下来
答案 2 :(得分:0)
不确定您要使用的数据量是多少,但您也可以使用arrays来实现您的目标。
java.lang.NullPointerException
at org.example.utils.Utils.getUsername(Utils.java:47)
at org.example.services.ServiceTest.startUploadFileTest(ServiceTest.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
处理
Option Explicit Sub AAA() Dim i As Long, j As Long, k As Integer Dim source As Worksheet, target As Worksheet Dim arrTarget() As Variant, arrSource() As Variant Dim lrowSrc As Long, lcolSrc As Long, lrowTrgt As Long, lcolTrgt As Long Set target = Workbooks("Book4.xlsb").Sheets("Sheet1") Set source = Workbooks("Book3.xlsb").Sheets("Sheet1") lrowSrc = source.Cells(target.Rows.Count, 1).End(xlUp).Row lcolSrc = source.Cells(2, source.Columns.Count).End(xlToLeft).Column lrowTrgt = target.Cells(target.Rows.Count, 1).End(xlUp).Row lcolTrgt = target.Cells(2, target.Columns.Count).End(xlToLeft).Column target.Activate arrTarget = target.Range(Cells(2, 1), Cells(lrowTrgt, lcolSrc)) source.Activate arrSource = source.Range(Cells(2, 1), Cells(lrowSrc, lcolSrc)) target.Activate For i = LBound(arrTarget, 1) To UBound(arrTarget, 1) For j = LBound(arrSource, 1) To UBound(arrSource, 1) If arrTarget(i, 1) = arrSource(j, 1) Then For k = LBound(arrSource, 2) To UBound(arrSource, 2) arrTarget(i, k) = arrSource(j, k) Next k Exit For End If Next j Next i target.Range("A2").Resize(UBound(arrTarget, 1), UBound(arrTarget, 2)).Value = arrTarget End Sub
中的12,000行数据和Target workbook
中的25,000行数据,共有6,000个匹配项,代码运行时间为9.91秒。