Excel 用多个单元格替换单元格内容

Excel 用多个单元格替换单元格内容,excel,vba,Excel,Vba,有没有办法用多个细胞替换一个细胞,并让它们与其他多个细胞替换相关?以下示例显示工作表A第一行的替换 工作表A: 使用工作表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: 工作表A中第一

有没有办法用多个细胞替换一个细胞,并让它们与其他多个细胞替换相关?以下示例显示工作表A第一行的替换

工作表A:

使用工作表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:

工作表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;:

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

选项显式
子组合数据()
将输入设置为范围
暗输出作为量程
将输入值设置为集合
Dim output1作为集合
Dim output2作为集合
Dim anOutput1作为变体
Dim anOutput2作为变体
设置myInput=工作表(“Sheet1”)。范围(“A1”)
设置myOutput=工作表(“表3”)。范围(“A1”)
做
Set output1=GetRelatedValues(myInput.Value)
Set output2=GetRelatedValues(myInput.Offset(0,1).Value)
对于output1中的每个anOutput1
对于输出2中的每一个输出2
myOutput.公式=输出1
myOutput.Offset(0,1)。公式=输出2
设置myOutput=myOutput.Offset(1,0)
下一个
下一个
设置myInput=myInput.Offset(1,0)
在myInput.Value“”时循环
端接头
函数GetRelatedValues(myInput作为变量)作为集合
Dim returnVal作为新系列
模糊关系作为范围
设置myRelationship=工作表(“Sheet2”)。范围(“A1”)
而myRelationship.Value“”
如果myRelationship.Value=myInput,则
returnVal.Add(myRelationship.Offset(0,1).Value)
如果结束
设置myRelationship=myRelationship.Offset(1,0)
温德
设置GetRelatedValues=returnVal
端函数

您的示例中的数据对我来说毫无意义。“广告”和“ds”怎么了?您的文本描述的是图像,而不是已发生的转换。编写说明,详细说明每行从开始到结束所采取的步骤。用它来开始写你的代码……这不是一个明确的问题,这可能就是为什么它一直被否决的原因。该示例仅显示了工作表1中第一行的替换:zd和fs。替换规定,使用zd的任何地方都应显示a_zd、b_zd或y_zd,使用fs的任何地方都应显示c_fs、y_fs和z_fs。替换的结果是工作表3(底部示例)中列出的九对。那么工作表A是输入1,输入2在A列和B列?然后第二个屏幕截图显示输入值和输出值之间的一对多关系?然后第三个显示第一个输入行产生的所有组合?我不认为您使用的是Microsoft Query附带的旧版本Excel?这是一个用SQL比用Excel公式简单得多的问题。你说得对Denise。我有Excel 2010,不熟悉Microsoft Query。出于维护原因,您可能希望将输入/输出变量重命名为在业务逻辑中有意义的变量。