2017-10-09 1 views
-1

Excel에서 VBA 코드를 찾아서 많은 단어를 찾아 바꾸십시오.VBA를 사용하여 Excel에서 여러 값 찾기 및 바꾸기

기본적으로 Sheet1에는 대체 할 이름이 포함 된 1 열의 문구가 포함 된 간단한 Excel 파일이됩니다 (전체 문구가 아닌 몇 단어로 구성된 이름 일 수도 있음). 두 번째 sheet2는 Sheet1에서 찾을 필요가있는 1 열 값 (첫 번째 열에서 값이 발견되는 경우 한 번 이상있을 수 있음)과 변환이 포함 된 열을 포함합니다. 이름은 매우 관례이므로 Google API가 필요하지 않습니다.

다음 스크립트를 보았지만 근본적으로 아무것도하지 않습니다.

Sub ReplaceValues() 

Dim dataSht As Worksheet 
Dim editSht As Worksheet 
Dim dataRange As Range 
Dim dataColumn As Long 
Dim editColumn As Long 
Dim dataEndRow As Long 
Dim editEndRow As Long 

'sheet that holds all the values we want to find 
Set dataSht = Sheet2 

'sheet we want to edit 
Set editSht = Sheet1 

Dim replaceValue As String 

'replace value is empty string 
replaceValue = "" 

'set the column of the data sheet to A 
dataColumn = 1 

'set the colmun of the sheet to edit to A 
editColumn = 5 

dataEndRow = dataSht.Cells(dataSht.Rows.count, dataColumn).End(xlUp).Row 
editEndRow = editSht.Cells(editSht.Rows.count, editColumn).End(xlUp).Row 

'this is the range of the data that we're looking for 
Set dataRange = dataSht.Range(dataSht.Cells(1, dataColumn), 
dataSht.Cells(dataEndRow, dataColumn)) 

Dim count As Long 
Dim val As String 

For i = 1 To editEndRow 

val = editSht.Cells(i, editColumn).Value 

count = Application.WorksheetFunction.CountIf(dataRange, val) 

    If count > 0 And Trim(val) <> "" Then 

    editSht.Cells(i, editColumn).Value = replaceValue 

    End If 

Next i 


End Sub 
+1

지금까지 해보신 것은 무엇입니까? 특정 문제 또는 문제로 인해 코딩 시도를 게시하십시오. – Zerk

+0

[둘러보기] (https://stackoverflow.com/tour)를 읽으시 고 [How to Ask] (https://stackoverflow.com/help/how-to-ask) 및 [Minimal, Complete, Verifiable 예] (https://stackoverflow.com/help/mcve). 그런 다음 질문을 수정하십시오. – danieltakeshi

+0

게시물을 업데이트했습니다. –

답변

0

정확하게 알고 있으므로 사전으로 번역 된 문구 목록을 단어 단위로 원합니다. 다음 스크립트는이를 수행해야합니다 - Sheet1에 2 행의 1 열에있는 구가 포함되어 있고 Sheet2에 사전 (column1은 원래 값, column2는 변환, row2는 on)이 포함되어 있다고 가정합니다. 대체/번역 된 구가 Sheet1의 column2에 나타납니다.

첫째, 원래 코드에서 vba는 Sheet1과 Sheet2를 두 개의 정의되지 않은 변수로 자동 인식하므로 이에 대한 경고는 없습니다. 워크 시트를 지정하려면 Worksheets() 콜렉션을 사용해야합니다.

두 번째로 replaceValue의 값을 변경하는 것을 잊었습니다. 실제로 매개 변수로 replace 함수에 cell.value를 직접 넣을 수 있습니다. 따라서 더 읽기 쉽게 만들고 싶지 않으면 변수를 설정할 필요가 없습니다.

마지막으로 단어가 포함되어 있는지 확인하려는 경우. InStr 함수를 사용하십시오. 그러나 귀하의 경우 대체 기능을 사용하는 것으로 충분합니다. 단어를 원하는 번역으로 바꿀 것이고 일치하는 것이 없을 경우에는 아무 것도하지 않습니다.

Sub btn_Click() 
    Dim cntPhrases As Integer 
    Dim cntDict As Integer 

    Worksheets("Sheet1").Activate 
    cntPhrases = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count - 1 
    cntDict = Worksheets("Sheet2").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count - 1 
    MsgBox (cntPhrases) 
    Dim i As Integer 
    Dim j As Integer 
    Dim phrase As String 
    Dim org As String 
    Dim rep As String 

    For i = 2 To cntPhrases + 1 
     phrase = Cells(i, 1) 
     For j = 2 To cntDict + 1 
      org = Worksheets("Sheet2").Cells(j, 1) 
      rep = Worksheets("Sheet2").Cells(j, 2) 
      phrase = replace(phrase, org, rep) 
     Next j 
     Cells(i, 2) = phrase 
    Next i 
End Sub 
+0

어떤 이유로 든 코드는 아무 일도하지 않습니다. 시트 1에는 변화가 없습니다. –

1

마지막으로 나는 아주 간단한 코드로 필요한 것을 할 수있었습니다. 숙련 된 사람들을 구하십시오!

Sub Test() 

Dim Sh1 As Worksheet 
Dim Sh2 As Worksheet 
Dim FndList, x& 


Set Sh1 = Sheets(1) 
Set Sh2 = Sheets(2) 
FndList = Sh2.Cells(1, 1).CurrentRegion 
For x = 1 To UBound(FndList) 
    Sh1.Cells.Replace What:=FndList(x, 1), Replacement:=FndList(x, 2), LookAt:=xlPart 
Next 
End Sub 
0

이렇게 할 수도 있습니다.

Sub main() 

Dim Find_text() As String 
Dim Replace_text() As String 

Dim str As String 

str = "test 150 test 160 test 170 test 200 test 220" 

Find_text = Split("150 160 170 180 190 200 210 220") 
Replace_text = Split("15 16 17 18 19 20 21 22") 

For i = 0 To UBound(Find_text) 
    For j = 0 To UBound(Replace_text) 
     If InStr(str, Find_text(j)) > 0 Then 
      str = Replace(str, Find_text(j), Replace_text(j)) 
     End If 
    Next 
Next 

MsgBox str 

End Sub 
관련 문제