如何从excel单细胞中选择多个值

时间:2017-09-19 06:11:40

标签: excel vba excel-formula

我有excel单元格,其中包含多行数据和图片网址。

现在我要选择具有1500值的所有图像。所以基本上我想选择以http开头并以1500.jpg结束的行。

请注意,在我的单个单元格中,值也不是1500.jpg。示例数据如下所示

colorImages': { 'initial': [{"hiRes":"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UL1500_.jpg","variant":"MAIN","lowRes":null},{"hiRes":"https://images-na.ssl-images-amazon.com/images/I/716mECZ9JDL._UL1500_.jpg","thumb":"https://images-na.ssl-images-amazon.com/images/I/313QD20m4WL._SR38,50_.jpg","large""thumb":"https://images-na.ssl-images-amazon.com/images/I/313QD20m4WL._SR38,50_.jpg","large":"https://images-na.ssl-images-amazon.com/images/I/313QD20m4WL.jpg","main":{"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY445_.jpg":[445,117],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY500_.jpg":[500,132],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY550_.jpg":[550,145],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY606_.jpg":[606,160],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY679_.jpg":[679,179],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY741_.jpg":[741,195],"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._UY879_.jpg":[879,231]},

2 个答案:

答案 0 :(得分:1)

假设数据位于从Column A开始的Cell A2中,并且所有以1500.jpg结尾的网址都需要显示在相邻列中,即同一行Column B, Column C, Column D,....,则以下内容可能有所帮助。

Sub Demo()
    Dim ws As Worksheet
    Dim lastRow As Long, colIndex As Long
    Dim rng As Range, cel
    Dim X As Long, DotCount As Long
    Dim Pat As String, EndPat As String, Parts() As String

    Set ws = ThisWorkbook.Worksheets("Sheet3")  'change Sheet3 to your data sheet
    With ws
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row        'get last row with data in Column A
        For Each cel In .Range(.Cells(2, 1), .Cells(lastRow, 1))    'loop through A2 to last cell with data in Column A
            colIndex = 1

            Pat = "*[!&-;?-[_a-z~=!" & Chr$(1) & "]."
            EndPat = "[!&-;?-[_a-z~=!" & Chr$(1) & "]*"
            Parts = Split(cel.Value, """")      'split cell value into an array
            For X = 0 To UBound(Parts)
              If Parts(X) Like "*?.?*" Then
                DotCount = Len(Parts(X)) - Len(Replace(Parts(X), ".", ""))
                If """" & Replace(Parts(X), "]", Chr$(1)) & """" Like Application.Rept(Pat, DotCount) & EndPat Then
                    Parts(X) = ""
                ElseIf Right(Parts(X), 8) <> "1500.jpg" Then
                    Parts(X) = ""
                Else
                    cel.Offset(0, colIndex) = Parts(X)  'display URL
                    colIndex = colIndex + 1
                End If
              Else
                Parts(X) = ""
              End If
            Next X
        Next cel
    End With
End Sub

enter image description here

使用here中的Function URLs来获取此解决方案。

答案 1 :(得分:0)

这可以通过VBA轻松完成,但我不是专家。所以我已经为你做了一些事情,只需按照指令操作,它仍适用于只获取单个搜索条目,即在单元格中它只能找到一个1500.jpg条目。

要在同一个单元格中获得第二个条目,您需要通过更改或从G1单元格字符串中获取子字符串进行一些努力,然后再次按照说明重复该步骤。

  1. 在A1 Cell中,放入1500.jpg
  2. 在B1 Cell中,将您的实际字符串放在上面
  3. 在C1单元格中,输入公式&#34; = SEARCH(A1,B1)&#34;,在字符串中找到搜索1500
  4. 在D1单元格中,输入公式&#34; = MID(B1,1,C1)&#34;,提取子字符串
  5. 对于E1,我们需要通过VBA代码反转字符串 - 添加Reversestr函数(要添加此函数,请参阅this link
  6. 在F1单元格中,输入公式&#34; = SEARCH(CHAR(34),E1)&#34;,搜索&#34;在上面的反向字符串
  7. 在G1细胞中,放入公式&#34; = MID(B1,C1-F1 + 1,C1)&#34;
  8. 最后,您将获得G1 Cell中的字符串"https://images-na.ssl-images-amazon.com/images/I/71GOT-L%2BOSL._1500.jpg" 对于VBa公式,请检查此链接

    http://analystcave.com/excel-substring-vba-substring/

相关问题