2015-10-14 36 views
-1

我有一个查询词列表,我将其提交给数据库(列A)以生成列表编码匹配(列FH)。列F是原始搜索词(所以在列A中存在完全匹配),列G包含匹配,并且列H包含匹配的代码。我需要做的是在F列中查询单词并在列A中找到它的伙伴。然后我需要将相应的匹配和代码粘贴到列A中的原始搜索词旁边(在列B & C )。在“找到”单元格旁边找到非静态[值]和粘贴范围(F1:G1) - Excel VBA

我的问题在这里越来越因为拷贝到正确的单元格粘贴和粘贴位置的改变,每次的信息 - 编码的比赛中列FH不包含A列中所有项的列表

我一直在搜索互联网,我似乎无法弄清楚究竟我需要更改以允许粘贴功能工作。

我附上了我的电子表格的简化版本和我一直在使用的代码的注释版本的图像。

Sub FindMatch() 

LastRow = Cells(Rows.Count, 6).End(xlUp).Row 

For i = 1 To LastRow 
    FindMe = Cells(i, 6).Value 
    Set FoundinList = Cells.Find(What:=FindMe, After:=ActiveCell, LookAt:=xlWhole) 

    If Not FoundinList Is Nothing Then 
     FoundinList.Select 
     ActiveCell.Offset(0, 1).Select 

'At this point the cell I want the information pasted into is selected. Yay! 
'Example: I am trying to find "abnormal digits" (F1) in Column A and paste 
'G1:H1 into the appropriate cells in Columns B & C (In this case B15:C15) 
'At this point in the code my cursor is on cell B15 - which is where I need it. 

     Range(Cells(i, 7), Cells(i, 8)).Copy 

'This selects the appropriate range (G1:H1 in my example). 

     ActiveCell.Paste 

'This is the problem string. I've tried naming the "ActiveCell" before initiating the copy 
'string (ActiveCell.Name = "PasteHere") and then pasting into the named cell 
'(Cells("PasteHere").Paste), but that gives me an invalid procedure call or argument on: 
'Cells("PasteHere").Paste I've also tried pasting into a range:Range(Cells(PasteHere, 2) 
', Cells(PasteHere, 3)).Paste -AND- using the formula that is created when you a record a 
'macro (Application.CutCopyMode = False) but both of those give me an application 
'/object-defined error. 

    End If 
Next i 
End sub 

非常感谢您提前阅读本文并帮助我。

My Spreadsheet

End Product

+0

你不能在b和c中使用简单的查找公式吗? –

+0

我认为一个vlookup(或索引/匹配)公式将起作用。你介意发布一个最终应该是什么样子的例子吗? – BruceWayne

+0

我是VBA新手,所以我不确定vlookup是否可以工作。不过我绝对可以看看! – ahhn

回答

0

这VBA使用工作表函数VLOOKUP。

Sub ahhn() 

Dim ws As Worksheet 
Dim cel As Range 

Set ws = ActiveSheet 
With ws 
    For Each cel In .Range(.Range("A1"), .Range("A1").End(xlDown)) 
     cel.Offset(0, 1) = WorksheetFunction.IfError(Application.VLookup(cel, .Range("F:H"), 2, 0), "") 
     cel.Offset(0, 2) = WorksheetFunction.IfError(Application.VLookup(cel, .Range("F:H"), 3, 0), "") 
    Next 
End With 

End Sub 
+0

完美的作品!谢谢! 你介意给我解释它在做什么? – ahhn

+0

@ahhn很高兴帮助。 –

相关问题