2013-05-31 127 views
-1

是否有任何方法可以用多個單元格替換單元格,並讓它們與其他多個單元格替換有關?下面的例子顯示的工作表A的第一行的取代用多個單元格替換單元格內容

工作表答:

pic2

隨着其中在列A中的項目應與相應的項被替換在工作表B中的取代邏輯在列B.到處使用zd,它應該變成a_zd,b_zd和y_zd。到處使用fs,它應該變成c_fs,y_fs和z_fs。例如:A1 = zd和B1 = a_zd; A2 = zd和B2 = b_zd; A3 = zd和B3 = y_zd; A4 = fs和B4 = c_fs; A5 = fs和B5 = y_fs; A6 = FS和B6 = z_fs:

pic2

在工作表A的第一行的取代將具有工作表C.實施例的結果:A1 = a_zd和B1 = c_fs; A2 = a_zd和B2 = y_fs; A3 = a_zd和B3 = z_fs; A4 = b_zd和B4 = c_fs; A5 = b_zd和B5 = y_fs; A6 = b_zd和B6 = z_fs; A7 = y_zd和B7 = c_fs; A8 = y_zd和B8 = y_fs; A9 = y_zd和B9 = z_fs ;:在你的榜樣

pic2

+0

的數據並沒有真正意義的我。 「ad」和「ds」發生了什麼?您的文字描述圖像而不是已經發生的轉換。編寫詳細說明每行從開始到結束的步驟。用它來開始編寫你的代碼。 – NickSlash

+0

...這不是一個明確的問題,這可能是爲什麼它已被低估 – whytheq

+0

對不起。該示例僅顯示了工作表1中第一行的替換:zd和fs。替換表明,無論使用哪個zd,都應該顯示a_zd,b_zd或y_zd,並且在任何地方使用fs應改爲顯示c_fs,y_fs和z_fs。替換的結果是工作表3中列出的9對(底部示例)。 – CKL

回答

0
Option Explicit 

Sub CombineData() 
    Dim myInput As Range 
    Dim myOutput As Range 
    Dim inputTwoValues As Collection 
    Dim output1 As Collection 
    Dim output2 As Collection 
    Dim anOutput1 As Variant 
    Dim anOutput2 As Variant 

    Set myInput = Worksheets("Sheet1").Range("A1") 
    Set myOutput = Worksheets("Sheet3").Range("A1") 

    Do 
     Set output1 = GetRelatedValues(myInput.Value) 
     Set output2 = GetRelatedValues(myInput.Offset(0, 1).Value) 
     For Each anOutput1 In output1 
      For Each anOutput2 In output2 
       myOutput.Formula = anOutput1 
       myOutput.Offset(0, 1).Formula = anOutput2 
       Set myOutput = myOutput.Offset(1, 0) 
      Next 
     Next 
     Set myInput = myInput.Offset(1, 0) 
    Loop While myInput.Value <> "" 
End Sub 


Function GetRelatedValues(myInput As Variant) As Collection 
    Dim returnVal As New Collection 
    Dim myRelationship As Range 
    Set myRelationship = Worksheets("Sheet2").Range("A1") 

    While myRelationship.Value <> "" 
     If myRelationship.Value = myInput Then 
      returnVal.Add (myRelationship.Offset(0, 1).Value) 
     End If 
     Set myRelationship = myRelationship.Offset(1, 0) 
    Wend 
    Set GetRelatedValues = returnVal 
End Function 
+0

這工作完美。非常感謝你!!! – CKL

+0

由於維護的原因,您可能希望將輸入/輸出變量重命名爲在您的業務邏輯中有意義的內容。 –

相關問題