2017-12-15 1 views
0

다음과 같은 매크로가 있지만 I는 시트 1 만 변경하고 H는 변경하지 않으려는 모든 활성 시트를 반복합니다. 그 이유는 국가 이름을 변경하기 때문입니다 그러나 매크로는 내가 원하지 않는 국적을 바꿉니다. 예를 들어 아프리카가 719로 바뀌면 아프리카는 719an으로 바뀝니다. 도움을 주시면 감사하겠습니다.루프에서 시트로 매크로 변경하기 1

Sub Multi_FindReplace() 


Dim sht As Worksheet 
Dim fndList As Variant 
Dim rplcList As Variant 
Dim x As Long 

fndList = Array("Libya", "New Caledonia", "St Vincent and the Grenadines", "Tanzania Untd Republic of", "Liechtenstein", "New Zealand", "Saint Pierre and Miquelon", "Thailand", "Lithuania") 
rplcList = Array("434", "540", "670", "834", "438", "554", "666", "764", "440") 
fndList = Array("Nicaragua", "Samoa", "Timor -Leste", "Luxembourg", "Niger", "San Marino", "Togo", "Macao", "Nigeria", "Sao Tome And Principe", "Tokelau") 
rplcList = Array("558", "882", "626", "442", "562", "674", "768", "446", "566", "678", "772") 
fndList = Array("Macedonia the former Yugoslav Republic of", "Niue", "Saudi Arabia", "Tonga", "Madagascar", "Norfolk Island", "Senegal", "Trinidad and Tobago", "Malawi", "Northern Mariana Islands", "Serbia", "Tunisia") 
rplcList = Array("807", "570", "682", "776", "450", "574", "686", "780", "454", "580", "688", "788") 
fndList = Array("Malaysia", "Norway", "Seychelles", "Turkey", "Maldives", "Oman", "Sierra Leone", "Turkmenistan", "Mali", "Pakistan", "Singapore", "Turks and Caicos Islands", "Malta", "Palau", "Sint Martin (Dutch part)", "Tuvalu", "Marshall Islands", "Palestine, State of") 
rplcList = Array("458", "578", "690", "792", "462", "512", "694", "795", "466", "586", "702", "796", "470", "585", "534", "798", "584", "275") 
fndList = Array("Slovakia", "Uganda", "Martinique", "Panama", "Slovenia", "Ukraine", "Mauritania", "Papua New Guinea", "Solomon Islands", "United Arab Emirates") 
rplcList = Array("703", "800", "474", "591", "705", "804", "478", "598", "90", "784") 
fndList = Array("Mauritius", "Paraguay", "Somalia", "United Kingdom", "Mayotte", "Peru", "South Africa", "United States", "Mexico", "Philippines", "South Georgia and the South Sandwich Islands") 
rplcList = Array("480", "600", "706", "826", "175", "604", "710", "840", "484", "608", "239") 
fndList = Array("US Minor Outlying Islands", "Micronesia Federated States of", "Pitcairn", "South Sudan", "Unknown", "Moldova", "Poland", "Spain", "Uruguay", "Monaco", "Portugal", "Sri Lanka", "Uzbekistan") 
rplcList = Array("581", "583", "612", "728", "999", "498", "616", "724", "858", "492", "620", "144", "860") 
fndList = Array("Mongolia", "Puerto Rico", "St Helena Ascension & Tristan da Cunha", "Vanuatu", "Montenegro", "Qatar", "Sudan", "Venezuela", "Montserrat", "Reunion", "Suriname", "Viet Nam") 
rplcList = Array("496", "630", "654", "548", "499", "634", "736", "862", "500", "638", "740", "704") 
fndList = Array("Morocco", "Romania", "Svalbard and Jan Mayen", "Virgin Islands British", "Myanmar", "Russian Federation", "Swaziland", "Virgin Islands U.S.", "Mozambique", "Rwanda", "Sweden", "Wallis and Futuna", "Namibia", "Saint Barthelemy") 
rplcList = Array("504", "642", "744", "92", "104", "643", "748", "850", "508", "646", "752", "876", "516", "652") 
fndList = Array("Switzerland", "Western Sahara", "Nauru", "Saint Kitts and Nevis", "Syrian Arab Republic", "Yemen", "Nepal", "Saint Lucia", "Taiwan Province of China", "Zambia", "Netherlands", "Saint Martin (French part)", "Tajikistan", "Zimbabwe") 
rplcList = Array("756", "732", "520", "659", "760", "887", "524", "662", "158", "894", "528", "663", "762", "716") 

'Loop through each item in Array lists 
    For x = LBound(fndList) To UBound(fndList) 
    'Loop through each worksheet in ActiveWorkbook 
     For Each sht In ActiveWorkbook.Worksheets 
     sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _ 
      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ 
      SearchFormat:=False, ReplaceFormat:=False 
     Next sht 

    Next x 



End Sub 
+1

이 줄은 : For Each Sht In ActiveWorkbook.Worksheets이 원인 일 수 있습니다. – ryyker

+0

본 적이 있습니까 [이] (https://www.techrepublic.com/blog/10-things/10-ways-to-reference-excel-workbooks-and-sheets-using-vba/) _ – ryyker

+0

'아프리카'대'아프리카 'VBA 정규식에 문제가있는 것이 좋습니다. –

답변

1

현재 스크립트에는 몇 가지 문제가 있습니다. 당신이 참조하는 사실 Sht.Cells은 하위 셀의 시작 부분에있는 모든 셀과 명령문을 포함 할 것이므로 배열을 덮어 씁니다. 코드가 실행되면 , 모두가 당신이 fndList에있을 것이다 것은 :

Array("Switzerland", "Western Sahara", "Nauru", "Saint Kitts and Nevis", "Syrian Arab Republic", "Yemen", "Nepal", "Saint Lucia", "Taiwan Province of China", "Zambia", "Netherlands", "Saint Martin (French part)", "Tajikistan", "Zimbabwe") 

다음은 fndList에 대한 하나의 문으로 통합해야하며 rplcList에 대한 하나

fndList = Array("Libya", "New Caledonia", "St Vincent and the Grenadines", "Tanzania Untd Republic of", "Liechtenstein", _ 
"New Zealand", "Saint Pierre and Miquelon", "Thailand", "Lithuania", "Nicaragua", "Samoa", "Timor -Leste", "Luxembourg", _ 
"Niger", "San Marino", "Togo", "Macao", "Nigeria", "Sao Tome And Principe", "Tokelau", _ 
"Macedonia the former Yugoslav Republic of", "Niue", "Saudi Arabia", "Tonga", "Madagascar", "Norfolk Island", "Senegal", _ 
"Trinidad and Tobago", "Malawi", "Northern Mariana Islands", "Serbia", "Tunisia", "Malaysia", "Norway", "Seychelles", _ 
"Turkey", "Maldives", "Oman", "Sierra Leone", "Turkmenistan", "Mali", "Pakistan", "Singapore", "Turks and Caicos Islands", "Malta", "Palau", "Sint Martin (Dutch part)", "Tuvalu", "Marshall Islands", "Palestine, State of", "Slovakia", "Uganda", "Martinique", "Panama", "Slovenia", "Ukraine", "Mauritania", "Papua New Guinea", "Solomon Islands", "United Arab Emirates", "Mauritius", "Paraguay", "Somalia", "United Kingdom", "Mayotte", "Peru", "South Africa", _ 
"United States", "Mexico", "Philippines", "South Georgia and the South Sandwich Islands", "US Minor Outlying Islands", "Micronesia Federated States of", "Pitcairn", "South Sudan", "Unknown", "Moldova", "Poland", "Spain", "Uruguay", "Monaco", "Portugal", "Sri Lanka", "Uzbekistan", "Mongolia", "Puerto Rico", "St Helena Ascension & Tristan da Cunha", "Vanuatu", "Montenegro", "Qatar", "Sudan", "Venezuela", "Montserrat", "Reunion", "Suriname", "Viet Nam", _ 
"Morocco", "Romania", "Svalbard and Jan Mayen", "Virgin Islands British", "Myanmar", "Russian Federation", "Swaziland", "Virgin Islands U.S.", "Mozambique", "Rwanda", "Sweden", "Wallis and Futuna", "Namibia", "Saint Barthelemy", "Switzerland", "Western Sahara", "Nauru", "Saint Kitts and Nevis", "Syrian Arab Republic", "Yemen", "Nepal", "Saint Lucia", "Taiwan Province of China", "Zambia", "Netherlands", "Saint Martin (French part)", "Tajikistan", "Zimbabwe") 

rplcList = Array("434", "540", "670", "834", "438", "554", "666", "764", "440", "558", "882", "626", "442", _ 
"562", "674", "768", "446", "566", "678", "772", "807", "570", "682", "776", "450", "574", "686", "780", "454", _ 
"580", "688", "788", "458", "578", "690", "792", "462", "512", "694", "795", "466", "586", "702", "796", "470", _ 
"585", "534", "798", "584", "275", "703", "800", "474", "591", "705", "804", "478", "598", "90", "784", "480", _ 
"600", "706", "826", "175", "604", "710", "840", "484", "608", "239", "581", "583", "612", "728", "999", "498", _ 
"616", "724", "858", "492", "620", "144", "860", "496", "630", "654", "548", "499", "634", "736", "862", "500", _ 
"638", "740", "704", "504", "642", "744", "92", "104", "643", "748", "850", "508", "646", "752", "876", "516", _ 
"652", "756", "732", "520", "659", "760", "887", "524", "662", "158", "894", "528", "663", "762", "716") 

그 다음 완료되면 코드에 약간의 변경을 가한 시트가 아닌 열별로 반복 할 수 있습니다.

For x = LBound(fndList) To UBound(fndList) 
    'Loop through each COLUMN in ActiveSheet 
     For Each col In ActiveSheet.Columns 
     If col.Address <> "$H:$H" Then 
     col.Replace What:=fndList(x), Replacement:=rplcList(x), _ 
      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ 
      SearchFormat:=False, ReplaceFormat:=False 
     End If 
     Next col 
Next x 
관련 문제