将宏从循环更改为工作表1

问题描述 投票:0回答:1

我有以下宏,但它循环遍历所有活动工作表,我只想更改为工作表1而不是列H,原因是我正在更改国家/地区的名称,但宏也会更改我不知道的国籍我想要。例如,当它将非洲变为719这是正确的时,它也将非洲变为719an。我将不胜感激任何帮助。

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
excel excel-vba vba
1个回答
1
投票

当前脚本存在一些问题。你引用Sht.Cells的事实将包括所有单元格,以及sub开头的语句,数组将被覆盖。当代码执行时,你在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")

一旦完成,那么您可以通过Columns迭代而不是使用稍微更改代码的工作表:

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
© www.soinside.com 2019 - 2024. All rights reserved.