2017-04-09 60 views
1

我在其中有数字,后面跟着TAB分隔符特定列的单元格多行文本,我想下面的文件上工作请参阅表宏细胞,多行文字截断

LINK TO FILE

的名字实际数据运行超过1000行,要在新插入的列中生成结果输出1和2。

任何人都可以帮助我在特定的列“ABC”上运行,并且只保留tab前的文本和两个下一个字符文本连接的另一列。进一步这个ABC专栏可以改变Excel表中的位置如果有人能够帮助我,我将非常感谢。

代码我触动了是

Sub RahulSplit() 
Dim colNum As Integer 
    colNum = ActiveSheet.rows(1).Find(what:="ABC", lookat:=xlWhole).Column 
     ActiveSheet.Columns(colNum + 1).Insert 
     ActiveSheet.Cells(1, colNum + 1).Value = "Results 2 Anticipated" 

    With CreateObject("VBScript.RegExp") 
     .Global = True 
     .MultiLine = True 
     .Pattern = "\w*\t\w*\s" 
     Dim x 
     For Each x In .Execute(s) 
      InBrackets = InBrackets & Mid(x, 2, Len(x) - 2) & vbLf 
     Next 
    End With 

EndSub 
+0

请发表您目前有代码麻烦与。 (并删除链接到一个非现场文件 - 问题应该是自包含的,不要依赖于可能在未来打破的链接。) – YowE3K

+0

我同意,但它很难指定多行文本,它只是让专家在精确的Excel表格上工作。 –

+0

@ YowE3K我希望编辑是可以接受的 –

回答

1

你可以使用这个UDF,以提取[...]括号括起来的字符串:

Function InBrackets(s As String) As String 
    With CreateObject("VBScript.RegExp") 
     .Global = True 
     .MultiLine = True 
     .Pattern = "\[\w*\]" 
     Dim x 
     For Each x In .Execute(s) 
      InBrackets = InBrackets & Mid(x, 2, Len(x) - 2) & vbLf 
     Next 
    End With 
End Function 

用法:

B2:=InBrackets(A2)

编辑

好了,让你更好的指定目标,与无关括号后,尝试follwing:

Sub InsertAnticipatedResults(sh As Worksheet) 
    Dim colABC As Long, abc As Range 
    With sh 
     colABC = .Rows(1).Find(what:="ABC", lookat:=xlWhole).Column 
     .Columns(colABC + 1).Insert 
     .Cells(1, colABC + 1).value = "Results 1 Anticipated" 

     .Columns(colABC + 2).Insert 
     .Cells(1, colABC + 2).value = "Results 2 Anticipated" 

     Set abc = .Range(.Cells(2, colABC), .Cells(.Rows.Count, colABC).End(xlUp)) 
    End With 

    Dim res1 As String, res2 As String, result1 As String, result2 As String, x 
    With CreateObject("VBScript.RegExp") 
     .Global = True 
     .MultiLine = True 
     .Pattern = "^\S*\s*\S*\s" 
     For Each abc In abc.Cells 
      result1 = "": result2 = "" 
      For Each x In .Execute(abc.Text) 
       res2 = Trim(x) 
       res1 = Trim(Left(res2, Len(res2) - 2)) 
       res2 = Replace(res2, " ", "") 
       res2 = Replace(res2, vbTab, "") 

       If Len(result1) > 0 Then result1 = result1 & vbLf: result2 = result2 & vbLf 
       result1 = result1 & res1 
       result2 = result2 & res2 
      Next 
      abc.Offset(, 1).value = result1 
      abc.Offset(, 2).value = result2 
     Next 
    End With 
End Sub 

Sub Testing() 
    InsertAnticipatedResults ActiveSheet 
End Sub 

Test for Rahul

+0

谢谢ASH,但我的要求稍有不同,我只需要数据,它是每个多行的开始字,例如在EP1189062 A1 2002-03-20 [EP1189062]中,我希望在标签前说明第一组词,即第二列中的EP1189062,我希望将两个字母串EP1189062 A1连接在一起,没有空间 –

+0

请注意,列名,所以我不打包使用A2 B2类引用,卡住请帮助 –

+0

您的代码完美地工作在括号中的字母表,但如果在括号' - '存在它不会工作,它会非常适合,如果你能告诉我如何通过宏只提取每个多行的前两个单词,我的同事们对功能的工作就没有多少想法。 –