如果回答了类似的问题,我们深表歉意。我进行了搜索,但找不到我要找的东西。
我有一个电子表格,在其中复制并粘贴了有关各种类型啤酒的大量数据。我想将包含文本的字符串单个单元格拆分为与啤酒类型和酒精百分比相对应的任意数量的单元格。我遇到的问题是,有些啤酒有两种或更多类别,而大多数啤酒只有一种。
这是我尝试拆分的文本字符串的示例:
American Pale Ale (APA) / 6.40% ABV
Quadrupel (Quad) / 10.20% ABV
American Double / Imperial IPA / 8.00% ABV
American Wild Ale / 7.75% ABV
预期结果是:
Category 1 | Category 2 | Alcohol %
American Pale Ale (APA) | | 6.40% ABV
Quadrupel (Quad) | | 10.20% ABV
American Double | Imperial IPA | 8.00% ABV
American Wild Ale | | 7.75% ABV
对于仅一种类别的啤酒,我一直使用以下公式:
=RIGHT(D3,LEN(D3)-SEARCH("/",D3,1))
然而,我想要一些无论啤酒有多少类别都能发挥作用的东西。我觉得这一定是可能的,因为有一个共同的分隔符(/)。
有人可以帮忙吗?
假设您的工作簿如下所示
试试这个代码。我已经对代码进行了注释,这样您就不会在理解它时遇到问题。
Option Explicit
Sub Sample()
Dim MYAr
Dim ws As Worksheet, wsOutput As Worksheet
Dim Lrow As Long, i As Long, j As Long, rw As Long, SlashCount As Long, col As Long
'~~> Set this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> get the last row
Lrow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Count the max number of "/" in a particular cell
'~~> This will decide the span of the columns
For i = 1 To Lrow
MYAr = Split(.Range("A" & i).Value, "/")
If UBound(MYAr) + 1 > SlashCount Then SlashCount = UBound(MYAr) + 1
Next i
'~~> Add a new worksheet for output
Set wsOutput = ThisWorkbook.Sheets.Add
rw = 1
For i = 1 To Lrow
MYAr = Split(.Range("A" & i).Value, "/")
col = 1
'~~> Write values to column. We will not write the
'~~> Last value of the array here
For j = LBound(MYAr) To (UBound(MYAr) - 1)
wsOutput.Cells(rw, col).Value = MYAr(j)
col = col + 1
Next j
'~~> Write Last value of the array to the last column
wsOutput.Cells(rw, SlashCount).Value = MYAr(UBound(MYAr))
rw = rw + 1
Next i
End With
End Sub
OUTPUT
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)