Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.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
Vba 在字符串中的特定单词之前复制前面的单词_Vba_Excel - Fatal编程技术网

Vba 在字符串中的特定单词之前复制前面的单词

Vba 在字符串中的特定单词之前复制前面的单词,vba,excel,Vba,Excel,我有一个文本文件,它看起来像 Blade Runner 2049 http://www.imdb.com/title/tt1856101 Kingsman: The Golden Circle http://www.imdb.com/title/tt4649466 The Crucifixion http://www.imdb.com/title/tt4181782/ 我的代码可以在文本文件中找到所有行“”,并复制“”之前的单词(电影名称),然后将它们粘贴到Excel单元格中 Sub Ge

我有一个文本文件,它看起来像

Blade Runner 2049 http://www.imdb.com/title/tt1856101

Kingsman: The Golden Circle http://www.imdb.com/title/tt4649466

The Crucifixion http://www.imdb.com/title/tt4181782/
我的代码可以在文本文件中找到所有行“”,并复制“”之前的单词(电影名称),然后将它们粘贴到Excel单元格中

Sub GetText()
Dim fName As String, Word1 As String, Word2 As String, i As Long, s As String, st As String
fName = "C:\Test\test1.txt"
st = "http://www.imdb.com/title"
Open fName For Input As #1
   Do Until EOF(1)
      Word1 = Word2
      Input #1, Word2
      If (Word2 = st & ">") Or (Word2 Like st & "/*") Then
          If Trim$(Word1) <> "" Then i = i + 1: Cells(i, 1) = Word1
      ElseIf Word2 Like "* " & st & "/*" Then
          Word1 = Trim$(Split(Word2)(1))
          If Trim$(Word1) <> "" Then i = i + 1: Cells(i, 1) = Word1
      End If
   Loop
Close #1
End Sub
Sub GetText()
Dim fName为字符串,Word1为字符串,Word2为字符串,i为长,s为字符串,st为字符串
fName=“C:\Test\test1.txt”
st=”http://www.imdb.com/title"
打开fName作为#1输入
直到EOF(1)为止
Word1=Word2
输入#1,字2
如果(Word2=st&“>”)或(Word2类似于st&“/*”),则
如果Trim$(Word1)“,则i=i+1:单元格(i,1)=Word1
如果单词2像“*”&st&“/*”那么
Word1=Trim$(拆分(Word2)(1))
如果Trim$(Word1)“,则i=i+1:单元格(i,1)=Word1
如果结束
环
关闭#1
端接头

但此代码只粘贴电影名称的第一个单词。粘贴电影全名需要更改什么?

似乎您可以在Excel中打开文件并删除URL部分(未测试):

同样,要仅获取URL,请执行以下操作:

Cells.Replace "* http://www.imdb.com/title/", "http://www.imdb.com/title/", xlPart 

似乎您可以在Excel中打开文件并删除URL部分(未测试):

同样,要仅获取URL,请执行以下操作:

Cells.Replace "* http://www.imdb.com/title/", "http://www.imdb.com/title/", xlPart 

我会像在工作表中一样解析它:

Sub dural()
    Dim st As String, s As String, MovieName As String

    st = "http://www.imdb.com/title"
    s = "Blade Runner 2049 http://www.imdb.com/title/tt1856101"
    MovieName = ""

    If InStr(1, s, st) > 0 Then
        With Application.WorksheetFunction
            MovieName = Left(s, .Find(st, s) - 1)
        End With
    End If
    MsgBox MovieName
End Sub

我会像在工作表中一样解析它:

Sub dural()
    Dim st As String, s As String, MovieName As String

    st = "http://www.imdb.com/title"
    s = "Blade Runner 2049 http://www.imdb.com/title/tt1856101"
    MovieName = ""

    If InStr(1, s, st) > 0 Then
        With Application.WorksheetFunction
            MovieName = Left(s, .Find(st, s) - 1)
        End With
    End If
    MsgBox MovieName
End Sub

一个非常简单的方法是使用

函数末尾的
(0)
表示您希望在找到单词之前找到整个字符串。相反,使用
(1)
意味着您希望在找到的单词(“http:”)的第一次迭代后使用字符串,
(2)
在该工作的第二次迭代后使用字符串,等等

请注意:您仍然可以使用
Split()
,而不使用
(i)
,(
Split()
,而不是
Split()(i)
)。使用此方法时,实际上是将值返回到数组中,而不是字符串中

如果要将值返回到数组,下面是上述操作的另一个示例:

Sub Test()

    Dim OrigStr$, OrigStrArr$(), YourMovie$
    OrigStr = "Kingsman: The Golden Circle http://www.imdb.com/title/tt4649466"

    OrigStrArr = Split(OrigStr, " http:")
    YourMovie = OrigStrArr(0)
    MsgBox YourMovie

End Sub

一个非常简单的方法是使用

函数末尾的
(0)
表示您希望在找到单词之前找到整个字符串。相反,使用
(1)
意味着您希望在找到的单词(“http:”)的第一次迭代后使用字符串,
(2)
在该工作的第二次迭代后使用字符串,等等

请注意:您仍然可以使用
Split()
,而不使用
(i)
,(
Split()
,而不是
Split()(i)
)。使用此方法时,实际上是将值返回到数组中,而不是字符串中

如果要将值返回到数组,下面是上述操作的另一个示例:

Sub Test()

    Dim OrigStr$, OrigStrArr$(), YourMovie$
    OrigStr = "Kingsman: The Golden Circle http://www.imdb.com/title/tt4649466"

    OrigStrArr = Split(OrigStr, " http:")
    YourMovie = OrigStrArr(0)
    MsgBox YourMovie

End Sub

这一个使用正则表达式:

Sub GetText()
Dim fName As String
Dim i As Long
Dim FileContents As String
Dim collMatches As Collection
fName = "C:\Test\test1.txt"
Open fName For Input As #1
FileContents = Input(LOF(1), 1)
Close 1

Set collMatches = GetRegexMatches(FileContents, "^.*(?=http)")
Debug.Print collMatches.Count
For i = 1 To collMatches.Count
   Cells(i, 1) = collMatches(i)
Next i
End Sub

Function GetRegexMatches(inputstring As String, SearchPattern As String, _
                         Optional boolIgnoreCase As Boolean = True, Optional boolGlobal As Boolean = True, Optional boolMultiline As Boolean = True, _
                         Optional UniqueMatches As Boolean = False) As Collection
Dim Regex As Object
Dim rgxMatch As Object
Dim rgxMatches As Object
Dim collMatches As Collection
Dim collUniqueMatches As Collection

Set Regex = CreateObject("vbscript.regexp")
With Regex
    'search for any integer matches
    '"\d+" is the same as "[0-9]+"
    .Pattern = SearchPattern
    .IgnoreCase = boolIgnoreCase
    'Find all matches, not just the first
    .Global = boolGlobal
    '^ and $ work per-line, not just at begin and end of file
    .MultiLine = boolMultiline
    'built-in test for matches
    Set collMatches = New Collection
    Set collUniqueMatches = New Collection
    If .test(inputstring) Then
        'if matches, create a collection of them
        Set rgxMatches = .Execute(inputstring)
        For Each rgxMatch In rgxMatches
            collMatches.Add rgxMatch
            On Error Resume Next
            collUniqueMatches.Add rgxMatch, rgxMatch
            On Error GoTo 0
        Next rgxMatch
    End If
End With

If UniqueMatches Then
    Set GetRegexMatches = collUniqueMatches
Else
    Set GetRegexMatches = collMatches
End If

Set Regex = Nothing

End Function

这一个使用正则表达式:

Sub GetText()
Dim fName As String
Dim i As Long
Dim FileContents As String
Dim collMatches As Collection
fName = "C:\Test\test1.txt"
Open fName For Input As #1
FileContents = Input(LOF(1), 1)
Close 1

Set collMatches = GetRegexMatches(FileContents, "^.*(?=http)")
Debug.Print collMatches.Count
For i = 1 To collMatches.Count
   Cells(i, 1) = collMatches(i)
Next i
End Sub

Function GetRegexMatches(inputstring As String, SearchPattern As String, _
                         Optional boolIgnoreCase As Boolean = True, Optional boolGlobal As Boolean = True, Optional boolMultiline As Boolean = True, _
                         Optional UniqueMatches As Boolean = False) As Collection
Dim Regex As Object
Dim rgxMatch As Object
Dim rgxMatches As Object
Dim collMatches As Collection
Dim collUniqueMatches As Collection

Set Regex = CreateObject("vbscript.regexp")
With Regex
    'search for any integer matches
    '"\d+" is the same as "[0-9]+"
    .Pattern = SearchPattern
    .IgnoreCase = boolIgnoreCase
    'Find all matches, not just the first
    .Global = boolGlobal
    '^ and $ work per-line, not just at begin and end of file
    .MultiLine = boolMultiline
    'built-in test for matches
    Set collMatches = New Collection
    Set collUniqueMatches = New Collection
    If .test(inputstring) Then
        'if matches, create a collection of them
        Set rgxMatches = .Execute(inputstring)
        For Each rgxMatch In rgxMatches
            collMatches.Add rgxMatch
            On Error Resume Next
            collUniqueMatches.Add rgxMatch, rgxMatch
            On Error GoTo 0
        Next rgxMatch
    End If
End With

If UniqueMatches Then
    Set GetRegexMatches = collUniqueMatches
Else
    Set GetRegexMatches = collMatches
End If

Set Regex = Nothing

End Function
Split(Word2)(1)
应该是
Split(Word2,st)(0)
Split(Word2)(1)
应该是
Split(Word2,st)(0)