2017-03-21 2 views
1

Ich habe Schwierigkeiten, die Range Cells-Methode mit Index Match in VBA zu verwenden. Die Verwendung von Standard-Bereichen funktioniert gut, aber ich habe kein Glück mit Range Cells. Ich muss etwas Grundlegendes über Index Match nicht verstehen. Ich habe die Codezeilen auskommentiert, die fehlschlagen. Ich schätze alle Hinweise, die die Community bieten kann.Range versus Range Zellen mit Index Match in VBA

Sub IndexMatchTroubleShooting() 

'dim worksheets 
Dim Source As Worksheet 
Dim Target As Worksheet 

'set worksheets 
Set Source = ThisWorkbook.Sheets("Source") 
Set Target = ThisWorkbook.Sheets("Target") 

'dim ranges 
Dim ValuesToPull As Range 
Dim TargetIDs As Range 
Dim SourceIDs As Range 
Dim MyRange As Range 

'using range <-this works 
Set ValuesToPull = Source.Range("B1:B5682") 
Set TargetIDs = Target.Range("A1:A21") 
Set SourceIDs = Source.Range("A1:A5682") 
Set MyRange = Target.Range("B1:B21") 

'using range cells <-this produces this error: "Run-time Error 1004 Method 'Range' of object '_Worksheet' failed" 
'Set ValuesToPull = Source.Range(Cells(1, 2), Cells(5682, 2)) 
'Set TargetIDs = Target.Range(Cells(1, 1), Cells(21, 1)) 
'Set SourceIDs = Source.Range(Cells(1, 1), Cells(5682, 1)) 
'Set MyRange = Target.Range(Cells(1, 2), Cells(21, 2)) 

'apply formula 
MyRange = Application.Index(ValuesToPull, Application.Match(TargetIDs, SourceIDs, 0)) 

End Sub 

Antwort

3

Sie benötigen, um alle Bereich/Cells Referenzen mit einem Blatt, da dies Fehler zu qualifizieren, wenn ein anderes Blatt aktiv ist, wenn das Makro ausgeführt wird, beispielsweise

Set ValuesToPull = Source.Range(Source.Cells(1, 2), Source.Cells(5682, 2)) 

oder ein wenig zu sparen

With Source 
    Set ValuesToPull = .Range(.Cells(1, 2), .Cells(5682, 2)) 
    Set SourceIDs = .Range(.Cells(1, 1), .Cells(5682, 1)) 
End With 
With Target 
    Set TargetIDs = .Range(.Cells(1, 1), .Cells(21, 1)) 
    Set MyRange = .Range(.Cells(1, 2), .Cells(21, 2)) 
End With 

eingeben (nicht sicher, dass Sie einen Multi-Zellbereich in einem Match Formel so verwenden können - weiß jemand)

+0

Das OP sagte, dass die 'Range (" B1: B5682 ") Methoden funktionierten, aber ich vermutete auch, dass eine Schleife benötigt würde, um die Ergebnisse zu erhalten. Aber es scheint, dass es funktioniert tatsächlich mit einem Quellbereich statt einem Quellwert !! (So ​​können Sie Ihren letzten Kommentar entfernen.) – YowE3K

+0

@ YowE3K - aber was ist der Wert nachgeschlagen - es kann nicht alle 21 Werte nachschlagen? – SJR

+0

Es scheint, als eine Array-Formel, so dass der erste Wert von der Quelle "Array" nachgeschlagen und an der ersten Position des Ergebnis "Array" zurückgegeben wird, der zweite Quellwert nachgeschlagen und an der zweiten Position zurückgegeben wird des Ergebnisses "array" usw. Und dann wird das Ergebnisarray in die 21 Zellen von 'myRange' geschrieben. – YowE3K

0
Sub MatchMaster() 

'this script helps simplify the use of Excel's Index Match function 
'place this script in your personal macro workbook and assign it to a button 
'use it to pull data between two worksheets that share unique identifiers 

'dim ranges 
Dim ValuesToPull As Range 
Dim TargetIDs As Range 
Dim SourceIDs As Range 
Dim MyRange As Range 

'dim worksheets 
Dim Source1 As Worksheet 
Dim Target1 As Worksheet 
Dim Source2 As Worksheet 
Dim Target2 As Worksheet 

'input box dims 
Dim Prompt1 As String 
Dim Prompt2 As String 
Dim Prompt3 As String 
Dim Prompt4 As String 
Dim Title1 As String 
Dim Title2 As String 
Dim Title3 As String 
Dim Title4 As String 

'set prompts 
Prompt1 = "Select values to pull (1 column only)" 
Prompt2 = "Select unique IDs on target sheet (1 column only)" 
Prompt3 = "Select unique IDs on source sheet (1 column only)" 
Prompt4 = "Where should we put these values? (1 column only)" 

'set titles 
Title1 = "Source Sheet" 
Title2 = "Target Sheet" 
Title3 = "Source Sheet" 
Title4 = "Target Sheet" 

'error handling 
On Error GoTo OuttaHere 

'input boxes 
Set SourceIDs = Application.InputBox(Prompt3, Title3, Type:=8) 
Set Source1 = SourceIDs.Worksheet 
SourceIDcolumn = SourceIDs.Column 
LastSourceID = Source1.Cells(Rows.Count, SourceIDcolumn).End(xlUp).Row 
Source1.Activate 

Set ValuesToPull = Application.InputBox(Prompt1, Title1, Type:=8) 
Set Source2 = ValuesToPull.Worksheet 
ValuesColumn = ValuesToPull.Column 
LastValue = Source2.Cells(Rows.Count, ValuesColumn).End(xlUp).Row 
Source2.Activate 

Set TargetIDs = Application.InputBox(Prompt2, Title2, Type:=8) 
Set Target1 = TargetIDs.Worksheet 
TargetIDcolumn = TargetIDs.Column 
LastTargetID = Target1.Cells(Rows.Count, TargetIDcolumn).End(xlUp).Row '<~~ also use this for MyRange 
Target1.Activate 

Set MyRange = Application.InputBox(Prompt4, Title4, Type:=8) 
Set Target2 = MyRange.Worksheet 
MyColumn = MyRange.Column 
Target2.Activate 

'convert input to Range Cells format 
With Source1 
    Set SourceIDs = .Range(.Cells(1, SourceIDcolumn), .Cells(LastSourceID, SourceIDcolumn)) 
End With 

With Source2 
    Set ValuesToPull = .Range(.Cells(1, ValuesColumn), .Cells(LastValue, ValuesColumn)) 
End With 

With Target1 
    Set TargetIDs = .Range(.Cells(1, TargetIDcolumn), .Cells(LastTargetID, TargetIDcolumn)) 
End With 

With Target2 
    Set MyRange = .Range(.Cells(1, MyColumn), .Cells(LastTargetID, MyColumn)) 
End With 

'apply formula 
MyRange = Application.Index(ValuesToPull, Application.Match(TargetIDs, SourceIDs, 0)) 

OuttaHere: 
ActiveWorkbook.ActiveSheet.Columns.AutoFit 

End Sub 
+0

@SJR Sie könnten dieses handliche finden, nahm ich Ihre Empfehlung auf den Blatt Deklarationen und es funktioniert wie ein Charme. –

+0

für alle Interessierten versuche ich, dies auf das nächste Level zu bringen. [link] (http://stackoverflow.com/questions/42959752/use-index-match-to-pull-multiple-columns-of-data?noredirect=1#comment73015536_42959752) –