【问题标题】:Copying all @mentions and #hashtags from column A to Columns B and C in Excel在 Excel 中将 A 列中的所有 @mentions 和 #hashtags 复制到 B 列和 C 列
【发布时间】:2012-11-18 09:22:55
【问题描述】:

我有一个非常大的推文数据库。大多数推文都有多个#hashtags 和@mentions。我希望所有#hashtags 在一列中用空格分隔,而所有@mentions 在另一列中。我已经知道如何提取#hashtag@mention 的第一次出现。但我不知道要全部拿到吗?一些推文有多达 8 个#hashtags。手动浏览推文并复制/粘贴#hashtags 和@mentions 对于超过 5,000 条推文来说似乎是一项不可能完成的任务。

这是我想要的一个例子。我有 A 列,我想要一个可以填充 B 列和 C 列的宏。(我在 Windows 和 Excel 2010 上)

Column A
-----------
Dear #DavidStern, @spurs put a quality team on the floor and should have beat the @heat. Leave #Pop alone. #Spurs a classy organization.
Live broadcast from @Nacho_xtreme: "Papelucho Radio"http://mixlr.com nachoxtreme-radio … #mixlr #pop #dance
"Since You Left" by @EmilNow now playing on KGUP 106.5FM. Listen now on http://www.kgup1065.com  #Pop #Rock
Family Night #battleofthegenerations Dad has the #Monkeys Mom has #DonnieOsman @michaelbuble for me #Dubstep for the boys#Pop for sissy
@McKinzeepowell @m0ore21 I love that the PNW and the Midwest are on the same page!! #Pop

我希望 B 列看起来像这样:

Column B
--------
#DavidStern #Pop #Spurs
#mixlr #pop #dance
#Pop #Rock
#battleofthegenerations #Monkeys #DonnieOsman #Dubstep #Pop
#pop

C 列看起来像这样:

Column C:
----------
@spurs @heat
@Nacho_xtreme
@EmilNow
@michaelbuble
@McKinzeepowell @m0ore21

【问题讨论】:

  • 你付多少钱 :) ?说真的,如果您能描述您尝试过的内容和您的编程技能水平,这将有所帮助:创建宏、使用 VBA、使用 Excel 对象模型和正则表达式。来自常见问题解答:您的问题应该有合理的范围。如果你能想象一整本书都能回答你的问题,那你就问得太多了。。另请参阅here

标签: excel twitter extract vba


【解决方案1】:

考虑使用正则表达式。

您可以在 VBA 中使用正则表达式,方法是从 Tools -> References 添加对 Microsoft VBScript Regular Expressions 5.5 的引用。

Here 是一个很好的起点,有许多有用的链接。


更新

添加对Regular Expressions库的引用后,将以下函数放入VBA模块中:

Public Function JoinMatches(text As String, start As String)
Dim re As New RegExp, matches As MatchCollection, match As match
re.pattern = start & "\w*"
re.Global = True
Set matches = re.Execute(text)
For Each match In matches
    JoinMatches = JoinMatches & " " & match.Value
Next
JoinMatches = Mid(JoinMatches, 2)
End Function

然后,在单元格B1 中输入以下公式(用于主题标签):

=JoinMatches(A1,"#")

C1 列中输入以下公式:

=JoinMatches(A1,"@")

现在您可以一直复制公式。

【讨论】:

  • 这更像是一个评论而不是一个答案。
【解决方案2】:

如果您不熟悉正则表达式,请参阅 (@Zev-Spitz)

【讨论】:

    猜你喜欢
    • 2014-09-22
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2016-07-14
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多