-1

有没有办法用多个单元格替换一个单元格并使它们与其他多个单元格替换相关?以下示例显示了替换工作表 A 的第一行。

工作表 A:

图2

使用工作表 B 中的替换逻辑,其中 A 列中的项目应替换为 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:

图2

替换工作表 A 中的第一行将得到工作表 C 的结果。示例:A1=a_zd and 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;:

图2

4

1 回答 1

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
于 2013-06-03T14:00:06.740 回答