Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何根据行标题替换单元格的内容?_String_Excel_Vba_Search_Concatenation - Fatal编程技术网

String 如何根据行标题替换单元格的内容?

String 如何根据行标题替换单元格的内容?,string,excel,vba,search,concatenation,String,Excel,Vba,Search,Concatenation,我需要搜索第1行(a1到dv1)中的每个单元格,以查找在该行的后续单元格中“doc1”后跟“doc2”的实例。然后我需要将包含“doc1”的单元格下一行中的内容与包含“doc2”的单元格下一行中的内容连接起来,并用逗号分隔,并替换包含“doc1”的单元格下方单元格中的文本 例如,如果a1有“doc1”,b1有“doc2”,a2有“7”,b2有“8”,那么我需要用“7,8”替换a2 任何帮助都将不胜感激 谢谢, Amy这里是VBA中的一个解决方案-您可以将其复制并粘贴到VBA中的一个新模块中(首先

我需要搜索第1行(a1到dv1)中的每个单元格,以查找在该行的后续单元格中“doc1”后跟“doc2”的实例。然后我需要将包含“doc1”的单元格下一行中的内容与包含“doc2”的单元格下一行中的内容连接起来,并用逗号分隔,并替换包含“doc1”的单元格下方单元格中的文本

例如,如果a1有“doc1”,b1有“doc2”,a2有“7”,b2有“8”,那么我需要用“7,8”替换a2

任何帮助都将不胜感激

谢谢,
Amy

这里是VBA中的一个解决方案-您可以将其复制并粘贴到VBA中的一个新模块中(首先备份电子表格),然后运行它(使用F5)。要快速访问VBA,请使用alt-F11。我在代码中留下了MSGBOX语句,已注释掉。此外,代码一直到DW1,因此可以完成DV1

Option Explicit

Sub Doc1_Doc2_Merge()

    Dim CurrCol As Integer
    Dim CurrRow As Integer
    Dim NewValue As String
    Dim EndCol As String

    For CurrRow = 1 To 50 Step 2 '(assume 50 rows - skip 2 lines each time)
       CurrCol = 1

       EndCol = "$DW$" & Trim(Str(CurrRow))
       While Cells(CurrRow, CurrCol).Address <> EndCol

           'MsgBox Cells(CurrRow, CurrCol).Address & " " & Cells(CurrRow, CurrCol).Value

           If InStr(Cells(CurrRow, CurrCol).Value, "doc1") > 0 Then
               ' look at next cell
               If InStr(Cells(CurrRow, CurrCol + 1).Value, "doc2") > 0 Then
                   If Trim(Cells(CurrRow + 1, CurrCol + 1).Value) <> "" Then
                       NewValue = Cells(CurrRow + 1, CurrCol).Value & "," & Cells(CurrRow + 1, CurrCol + 1)
                       'MsgBox "New Value is " & NewValue
                       Cells(CurrRow + 1, CurrCol).Value = NewValue
                   End If
               End If

           End If

           CurrCol = CurrCol + 1
       Wend
    Next CurrRow

End Sub
选项显式
子文档1\u文档2\u合并()
Dim CurrCol作为整数
将行设置为整数
将NewValue设置为字符串
作为字符串的Dim EndCol
对于CurrRow=1到50步骤2’(假设50行-每次跳过2行)
CurrCol=1
EndCol=“$DW$”&修剪(Str(CurrRow))
While单元格(CurrRow,CurrCol).Address EndCol
'MsgBox单元格(CurrRow,CurrCol).Address&''单元格(CurrRow,CurrCol).Value
如果InStr(单元格(CurrRow,CurrCol).Value,“doc1”)>0,则
“看看下一个单元格
如果InStr(单元格(CurrRow,CurrCol+1).Value,“doc2”)>0,则
如果修剪(单元格(CurrRow+1,CurrCol+1).Value)”,则
NewValue=单元格(CurrRow+1,CurrCol)。Value&“,”&单元格(currow+1,CurrCol+1)
'MsgBox“新值为”&新值
单元格(CurrRow+1,CurrCol).Value=NewValue
如果结束
如果结束
如果结束
CurrCol=CurrCol+1
温德
下一班
端接头
以下是测试结果:

非常感谢您的回复。这似乎还不能正常工作。字符串包括“doc1”和“doc2”,但不“等于”doc1和doc2,这有关系吗?此外,单元格(1,CurrCol+1).Value=“doc2”是否应改为:Cells(1,CurrCol+1).String=“doc2”?谢谢从你的问题来看,我认为它们是doc1和doc2的硬编码值-如果值“包含”doc1和doc2,我将在一秒钟内更新我的代码。还有.Value works-不知道.StringWorks很漂亮。谢谢还有一个问题:如果我只想在doc2下面的单元格不是空的情况下进行连接,那么这是插入的正确代码吗?如果(单元格(1,CurrCol+1).Value)>0,则…关闭-因为它是一个字符串,并且如果您也不需要空格,请尝试如果修剪(单元格(1,CurrCol+1).Value)“,然后