2013-05-31 126 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

由于维护的原因,您可能希望将输入/输出变量重命名为在您的业务逻辑中有意义的内容。 –

相关问题