我必须使用 Ctrl A + Ctrl C 从网页复制文本才能在 Excel 中使用。
复制的文本大约有 100 行,大小不一。假设一行有 200 个字符的字符串,下一行有 500 个字符,第三行可能有 20 个字符。
有没有办法循环剪贴板数据线并将它们复制到数组中?
复制文本的示例(使用页面中的 Ctrl A Ctrl C 制作):
注意:我删除了一些行
Usernames are XXXXXXXXXXXXXXXXX
DashboardAnalyticsPolicyAdministration
Web Insights
Print View
Start Over
1Select Chart Type
Logs
Apply Filters
2Choose a Timeframe
Custom: 9/1/2015 12:00:00 AM - 9/30/2015 12:00:00 AM
3Select Filters
Add Filter
2.4 TB
2.0 TB
879.9 GB
656.8 GB
472.0 GB
442.4 GB
242.1 GB
213.5 GB
189.3 GB
103.8 GB
Office 365 - SSL Bypass
Professional Services
Streaming Media
Sites everyone
Internet Services
Corporate Marketing
Miscellaneous
Web Search
News and Media
Social Networking
URL CategoryTop 10TransactionsBytes
最佳答案
要跟进我的评论,如果您按照 here 中的说明进行操作添加对 Microsoft Forms Library 2.0
的引用(在 VBA 编辑器中的 Tools/References
下),以下函数获取剪贴板的内容并将其拆分为行:
Function ClipToArray() As Variant
Dim clip As New MSForms.DataObject
Dim lines As String
clip.GetFromClipboard
lines = clip.GetText
lines = Replace(lines, vbCr, "")
ClipToArray = Split(lines, vbLf)
End Function
你可以像这样测试它:
Sub test()
Dim A As Variant
Dim i As Long
A = ClipToArray()
For i = LBound(A) To UBound(A)
Debug.Print A(i)
Next i
End Sub
然后我去了this网站并复制这首诗然后运行
test
.我在即时窗口中得到以下输出:Some say the world will end in fire,
Some say in ice.
From what I've tasted of desire
I hold with those who favor fire.
But if it had to perish twice,
I think I know enough of hate
To say that for destruction ice
Is also great
And would suffice.
这工作得很好,尽管您不必对从 Internet 复制的文本进行许多实验,然后才能看到使用
split
进行的表面解析。有很多不足之处。
关于excel - 将剪贴板数据复制到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33156317/