2015-01-18 2 views
0

너무 많은 시스템간에 마이그레이션을 수행하고 있습니다. 예전 시스템은 태그를 사용하여 제품을 분류했으며, 새로운 시스템은 유형을 도입합니다.단일 셀에서 60 개 이상의 값을 검색/일치시키는 VBA/Excel 메서드

데이터를 Excel에로드 한 수천 개의 행의 CSV로 사용할 수 있습니다. 다음은 형식의 예입니다.

A1: =IF(SEARCH("Sony",B2),"Sony", IF(SEARCH("Samsung",B2),"Samsung",etc)) 

내가 60 개 개별 태그를 통해/일치를 검색 할 단 :

Col A  | Col B | Col C 
Product | Type | Tags 
Samsung S5 |  | Android, Samsung, 5.1" Screen 
Sony Z3 |  | Android, Bluetooth, Sony, 5.2" Screen 
LG G3  |  | Android, LG, 5.5" Screen 

은 내가 이런 식으로 할 수있는 열 C.에서 하나의 태그로 열 B를 채울 수 있도록하려면 C 열을 B 열의 단일 값으로 변환하면이 접근법은 신속하게 관리하기 어려워집니다.

Excel 기능을 사용하는 다른 방법이 있습니까? 아니면 VBA를 사용해야합니까?

나는 여러 해 동안 VBA를 사용하지 않았으므로 어떤 예제/포인터도 감사 할 것입니다.

답변

1

Excel 기능을 사용하는 다른 방법이 있습니까? 아니면 VBA를 사용해야합니까?

이럴 VBA,이 시도 :

(두 번째 변종보다 느리게)
  1. 첫 번째 변종

    Sub test() 
    Dim oCellTag As Range, oCellSource As Range 
    For Each oCellTag In Sheets("Tags").Range("A1:A3") 'Range with Tags in one sheet 
        For Each oCellSource In Sheets("Source").Range("C2:C4") 'Range with data for search tags in another sheet 
         If UCase(oCellSource.Value) Like "*" & UCase(oCellTag.Value) & "*" And oCellTag.Value <> "" Then 'if cell contain tag 
          Sheets("Source").Cells(oCellSource.Row, oCellSource.Column - 1).Value = oCellTag.Value 
         End If 
        Next 
    Next 
    End Sub 
    
  2. 두 번째 변종 (빨리)

    Sub test2() 
    Dim oCellTag As Range, oCellSource As Range, KeySource, KeyTag 
    Dim Source As Object: Set Source = CreateObject("Scripting.Dictionary") 
    Dim Tags As Object: Set Tags = CreateObject("Scripting.Dictionary") 
    'Grab the dates from the WorkSheets 
    For Each oCellTag In Sheets("Tags").Range("A1:A3") 
        If oCellTag.Value <> "" Then 
         Tags.Add oCellTag.Row, oCellTag.Value 
        End If 
    Next 
    For Each oCellSource In Sheets("Source").Range("C2:C4") 
        If oCellSource.Value <> "" Then 
         Source.Add oCellSource.Row, oCellSource.Value 
        End If 
    Next 
    'Match 
    For Each KeyTag In Tags 
        For Each KeySource In Source 
         If UCase(Source(KeySource)) Like "*" & UCase(Tags(KeyTag)) & "*" Then 
          Sheets("Source").Cells(KeySource, 2).Value = Tags(KeyTag) 
         End If 
        Next 
    Next 
    End Sub 
    
관련 문제