2017-02-03 268 views
0

我有一個髒數據庫,其中每個人的名稱都以不同的方式寫入,我無法對它們進行分組。使用數組VBA查找和替換數據庫中的值

我想創建一個宏來查找和使用兩列列表替換數據庫中的名稱。

我發現下面的代碼,但無法理解它I'm,因此無法適應它:

Dim Sht As Worksheet 
Dim fndList As Integer 
Dim rplcList As Integer 
Dim tbl As ListObject 
Dim myArray As Variant 
Dim Rng As Range 


'Create variable to point to your table 
    Set tbl = Worksheets("How to").ListObjects("Table2") 

'Create an Array out of the Table's Data 
    Set TempArray = tbl.DataBodyRange 
    myArray = Application.Transpose(TempArray) 

'Designate Columns for Find/Replace data 
    fndList = 1 
    rplcList = 2 

'Loop through each item in Array lists 
    For x = LBound(myArray, 1) To UBound(myArray, 2) 
    'Loop through each worksheet in ActiveWorkbook (skip sheet with table in it) 
     For Each Rng In Worksheets("xxxxxxxxxx").Activate 
     If Rng.Name <> tbl.Parent.Name Then 

      Rng.Cells.replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _ 
      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ 
      SearchFormat:=False, ReplaceFormat:=False 

     End If 
     Next Rng 

    Next x 

End Sub 
+0

瓦你不明白嗎?你有沒有試過運行它?它做什麼或不做它應該做什麼或不應該做什麼? – nicomp

+0

此代碼對數據庫沒有任何內容。你的意思是一個Excel表格? –

+0

請閱讀[如何提問](http://stackoverflow.com/help/how-to-ask)並相應編輯您的問題以獲得最有效的幫助。閱讀這些內容有助於你的理解,正如所寫,這個問題幾乎不可能以任何有意義的方式回答。 –

回答

0

所以回答你的第二個問題,基本上你需要做的是去除表圈(你已經做了),然後你缺少的一部分你還需要指定你想要的代碼來執行在目標範圍內的替換剛剛細胞,而不是在板內的細胞執行它(這將是所有細胞)...請參閱下面的例子:

Public Sub demoCode_v2() 
Dim tableRange As Range 
Dim myArray() As Variant 
Dim rowCounter As Long 
Dim targetRange As Range 

'Create an Array out of the Table's Data 
Set tableRange = ThisWorkbook.Sheets(sheetName).ListObjects("Table1").DataBodyRange 
myArray = tableRange 

'Select target range 
Set targetRange = Application.InputBox("Select target range:", Type:=8) 

'Loop through each item in lookup table 
For rowCounter = LBound(myArray, 1) To UBound(myArray, 1) 
    'Replace any cells in target range that contain whats in the first column of the lookup table, with whats in the 2nd column.. 
    targetRange.Cells.Replace What:=myArray(rowCounter, 1), Replacement:=myArray(rowCounter, 2), LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False 
Next 

End Sub 

希望這有助於 TheSilkCode

+0

這有幫助,謝謝! – Urumita

1

我已經調整你的代碼,你可以看到下面;一對夫婦筆記:

1-使用Option Explicit總是一個好主意 2-如果您將數組循環放入工作表循環中,您只需執行工作表名稱檢查n次(n =工作簿中工作表的數量),如果將表單循環放入數組循環中,您將不得不執行表單名稱檢查n * x次(x =數組中的項目數)... 3-您沒有指定,但我假定Table1是垂直構建的,第一列中的查找值和第二列中的替換值 - 因此不需要轉置數組;如果你的表1實際上是在水平,那麼你就需要調整這個代碼...

Public Sub demoCode() 
Dim sheetName As String 
Dim tableRange As Range 
Dim myArray() As Variant 
Dim wsCounter As Long 
Dim rowCounter As Long 

'Store name of sheet with lookup table 
sheetName = "How to" 

'Create an Array out of the Table's Data 
Set tableRange = ThisWorkbook.Sheets(sheetName).ListObjects("Table1").DataBodyRange 
myArray = tableRange 

'Loop through each sheet 
For wsCounter = 1 To ThisWorkbook.Sheets.Count 
    With ThisWorkbook.Sheets(wsCounter) 
     'Test to make sure the sheet is not the sheet with the lookup table 
     If .Name <> sheetName Then 
      'Loop through each item in lookup table 
      For rowCounter = LBound(myArray, 1) To UBound(myArray, 1) 
       'Replace any cells that contain whats in the first column of the lookup table, with whats in the 2nd column.. 
       .Cells.Replace What:=myArray(rowCounter, 1), Replacement:=myArray(rowCounter, 2), LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False 
      Next 
     End If 
    End With 
Next 

End Sub 

希望這有助於 TheSilkCode

+0

*使用顯式的選項始終是一個好主意* - 但是,你看無論是在你的代碼或鏈接:( –

+0

嗨@ScottHoltzman你的模塊,允許它的頂部,鍵入「顯式的選項」沒有提及這一點。 ..請參閱此處的鏈接:[如何強制VBA/Access要求定義變量?](http:// stackoverflow。com/questions/1139321/how-do-i-force-vba-access-to-require-variables-to-be-defined) – TheSilkCode

+0

這很好,謝謝! – Urumita

0

TheSilkCode,這是一個很好的答案,它完美地通過循環牀單。

I'm現在正試圖使其只遍歷一個範圍,遺憾的是它不工作來適應它。可能我需要定義一個變量。我說上面已經RNG的範圍:

Option Explicit 

Public Sub demoCode() 
Dim sheetName As String 
Dim tableRange As Range 
Dim myArray() As Variant 
Dim wsCounter As Long 
Dim rowCounter As Long 
Dim Rng As Range 

'Store name of sheet with lookup table 
sheetName = "How to" 

'Create an Array out of the Table's Data 
Set tableRange = ThisWorkbook.Sheets(sheetName).ListObjects("Table2").DataBodyRange 
myArray = tableRange 

'Loop through range in Worksheet  
Worksheets("Post").Activate 
Range("ak1").Select 
Selection.End(xlDown).Select 

For Each Rng In Selection 

    'Loop through each item in lookup table 
     For rowCounter = LBound(myArray, 1) To UBound(myArray, 1) 
    'Replace any cells that contain whats in the first column of the lookup  table, with whats in the 2nd column.. 
    .Cells.Replace What:=myArray(rowCounter, 1), Replacement:=myArray(rowCounter, 2), LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False 
Next 


End Sub