简体   繁体   English

Excel VBA匹配还是查找?

[英]Excel VBA Match or Find?

I've tried, I've really tried!! 我尝试过,我真的尝试过! I have two sheets "arrears" and "trends". 我有两张“欠款”和“趋势”。 Here's an image to save loads of words: Intended tables 这是保存大量单词的图像:目标

The idea is for a script to loop though (existing) records in column A of sheet Trends, Find a match in column A of sheet Arrears. 这个想法的目的是让脚本在“趋势”表A列中循环(现有)记录,在“欠款”表A列中找到匹配项。 Then with that match, post to corresponding amount from the figure column (arrears sheet) into the corresponding cell in the insert column back on the trend sheet (by moving existing records 1xToRight. 然后,通过该匹配,将图列(欠款表)中的相应金额过帐到趋势图上插入列中的相应单元格中(通过将现有记录移动1xToRight)。

I'm trying to learn VBA as I go and have had loads of attempts. 我正在尝试学习VBA,并且尝试了很多。 I was using MATCH and found that I could do everything except transfer the corresponding values from arrears to trends. 我使用MATCH时发现,除了将相应的值从欠款转换为趋势外,我可以做所有事情。

There is additional stuff to do after this stage but I don't want to ask for the full script otherwise I'll learn nothing! 在此阶段之后,还有其他事情要做,但是我不想索取完整的脚本,否则我什么也学不会!

a small snippet below of similar code I've used recently Not the most optimsed code for your problem, but it should help you 'see' the solution imo. 下面是我最近使用过的类似代码的一小段不是针对您问题的最优化代码,但它应该可以帮助您“查看”解决方案imo。

 Dim rngStr As String 'range declaration
 Dim currentRowIndex As Integer 'used to store the rowindex of certain searchvalues
 Dim searchStr As String 'word to search
 Dim ws As Worksheet

'Set searchcolumn = A
rngStr = "A:A"
searchstring = "Cellvalue of cell in loop"
currentRowIndex = SearchInRange(ws, rngStr, searchStr)

If currentRowIndex <> 0 Then
    'searchstr was found
    Worksheets("TrendsSheet").Cells(rowIndex, ColIndex).value = Worksheets("ArrearsSheet").Cells(currentRowIndex, ColIndexOfValueYouWant).value
End If



Private Function SearchInRange(ws As Worksheet, rng As String, searchstring As String) As Integer
    'return the rowindex of the first found value in a specific range
    With ws.Range(rng)
        Set c = .Find(searchstring, LookIn:=xlValues)
        If Not c Is Nothing Then
            SearchInRange = c 'searchstring found
        Else
            SearchInRange = 0 'searchString not found
        End If
    End With
End Function

From what I have understood I have made some code that does what you are asking for 据我了解,我编写了一些代码来满足您的要求

Sub Transfer()
Dim Wks1 As Excel.Worksheet
Dim Wks2 As Excel.Worksheet
Dim copyCell As Long
Dim pasteCell As Long
Dim RowMatched As Long
Dim SearchItem As Double
Dim NumberOfEntries As Long
Dim RowMoved As Boolean
Set Wks1 = Worksheets("Sheet1") '<== One worksheet
Set Wks2 = Worksheets("Sheet2") '<== Another worksheet
NumberOfEntries = Application.WorksheetFunction.CountA(Wks2.Range("A:A")) '<=== Finds the number of entries
RowMoved = False '<===== Checks if row has been inserted
For x = 2 To NumberOfEntries '<==== For all your entries
SearchItem = Wks2.Cells(x, 1) '<=== What it is looking for
On Error Resume Next
    RowMatched = Application.WorksheetFunction.Match(SearchItem, Wks1.Range("A:A"), 0) '<== Match Items
On Error GoTo 0
If RowMatched <> 0 Then '<=== If found
    If RowMoved = False Then '<== If no column has been added yet
        Wks2.Range("E:E").EntireColumn.Insert '<=== Add new row in column E
    End If
    RowMoved = True '<=== Set row moved to true to indicate inserted column
    Wks2.Cells(x, 5) = Wks1.Cells(RowMatched, 5) '<==== Copy Cell values
End If
Next x



End Sub

Name the worksheets what ever you have called them and put this in a new module. 将工作表命名为您曾经称之为的工作表,然后将其放入新模块中。 You can also mess around with the column numbers if you need to. 如果需要,您也可以弄乱列号。 Let me know if you need anything else :) 需要帮助请叫我 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM