将多个内容从一个单元格复制到另一个单元格而不覆盖

时间:2014-05-04 05:55:37

标签: excel vba

我必须从工作簿的每个单元格中找到包含"CD /"LCD的所有步骤,并将其粘贴到相应的单元格而不会覆盖。这是一个示例输入:

//STEP002  EXEC PGM=FTP

//SYSOUT   DD SYSOUT=*

//SYSIN    DD *

NOLOG *****Dummy Member Include*****************************************

NOLOG *****Dummy Member Include*****************************************

LSITE CC OFF                                                            PHG00280

LSITE TRCC ON                                                           PHG00290

LCD POWER.LST.X                                                         PHG00300

LDIR                                                                    PHG00310

CD /REPORT/CURRENT2                                                     PHG00340

NOLOG *****Dummy Member Include*****************************************

PUT %FILE12,ESDS,F,2000,2000 Industry.txt                               PHG01700

PUT %FILE13,ESDS,F,2000,2000 SuperBank.txt                              PHG01710

PUT %FILE14,ESDS,F,2000,2000 Executive.txt   

CD /UPLOAD  

LCD SAMPLE.TXT

MPUT PHGDFM01.*                                                         PHG00350

BYE         

我创建了这个子:

Sub Search2()
Dim MatchString As String
Dim matchstrin2 As String
Dim counter As Variant
Dim Name As String
Dim Datain As String
MatchString = "CD "
MatchString2 = "CD  "
For counter = 1 To Range("A:A").Count
Datain = Range("A" & counter).Value
If (InStr(1, Datain, MatchString) > 0 Or InStr(1, Datain, MatchString2) > 0) Then
Data = Split(Datain, vbLf)
cnt = UBound(Data)
    For I = 0 To cnt
        If Data(I) Like "*CD /*" Then
        'Temp = Split(Data(i), " ")
        'Temp2 = (Data(i) + 1)
        Name = Data(I + 0)
        Range("B" & counter).Value = Name
        'Next
        End If
        Next
End If
Next counter
End Sub

它给了我输出但是用CD /REPORT/CURRENT2覆盖了第一行CD CD /UPLOAD并且它只给了我一个输出。无论如何要保留两个记录LCDCD

Desired out put: 


       A                                         B
1   input goes here.....     |        CD /REPORT/CURRENT2
2   ............             |        CD /UPLOAD
3   ..............           |        LCD POWER.LST.X 
4   .............            |        LCD SAMPLE.TXT                  

1 个答案:

答案 0 :(得分:0)

更改这些行:

MatchString = "CD "
MatchString2 = "CD  "
...
If (InStr(1, Datain, MatchString) > 0 Or InStr(1, Datain, MatchString2) > 0) Then

用这些:

MatchString = "CD "
MatchString2 = "CD  "
MatchString3 = "LCD "
...
If (InStr(1, Datain, MatchString) > 0 Or InStr(1, Datain, MatchString2) > 0)  Or InStr(1, Datain, MatchString3) > 0)  Then

您没有在代码中检查LCD术语