从字符串中获取子字符串

时间:2018-05-21 13:24:35

标签: excel vba excel-vba

我有一些代码将单元格中的值与工作表中的其他值相邻。单元格中的值是“美国的Nat Rep可行性检查 - 按地区划分的细节”,其中国家变化 - 它可以是“澳大利亚”;“阿根廷”等我需要提取country并将其放在原因值旁边:

b

我设法“Nat Rep可行性检查”,但我还是需要得到其余的字符串,你能帮助我吗? 提前谢谢!

4 个答案:

答案 0 :(得分:3)

您可以通过以下公式实现:

=MID(F8,LEN("Nat Rep feasibility check for ")+1,LEN(F8)-LEN("Nat Rep feasibility check for ")-LEN("Details by Region")-2)

enter image description here

干杯

答案 1 :(得分:3)

如何删除字符串的两个固定部分 - 正如您在一个案例中所做的那样:

<script src="https://use.fontawesome.com/fcc45c3f3f.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container-fluid" id="core-content">

    <form name="search-filter-form" method="POST" class="">
        <div class="container-fluid">
            <div class="">
                <div class="row">

                    <div class="text-center col-xs-2">
                        <i class="fa fa-search-plus zoom-in" aria-hidden="true"></i>
                        <i class="fa fa-search-minus zoom-out" aria-hidden="true"></i>
                    </div>
                    <div class="text-center col-xs-1">
                        <a href="#" class="btn btn-default btn-primary ">Reset</a>
                    </div>

                </div>
            </div>

        </div>
    </form>


    <div id="loading-msg" style="display: none;">
        <div class="row">
            <div class="spacer-sml"></div>
        </div>
        <div class="row">
            <div class="spacer-sml"></div>
        </div>
        <div class="col-xs-12 text-center">
            <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            <span class="sr-only">Loading...</span>
            <p>Loading...</p>
        </div>

    </div>
    <div id="portfolio-container" class="container-fluid portfolio-container" style="">

        <div class="spacer-sml"></div>
        <form action="#" id="portfolio-form" name="portfolio-form" method="POST" class="">

            <div class="board-canvas">
                <div id="board" class="u-fancy-scrollbar js-no-higher-edits js-list-sortable ui-sortable">

                    <div class="js-list list-wrapper ">
                        <div class="list js-list-content">
                            <div class="list-header js-list-header u-clearfix is-menu-shown">
                                <div class="list-header-target js-editing-target"></div>
                                <h4 class="list-header-name-assist js-list-name-assist text-center" dir="auto">
                                    Awaiting Approval
                                </h4>
                            </div>
                            <div class="sortable-list ui-sortable" data-column-id="awaitapprove" data-column-value="awaitapprove Awaiting Approval">


                                <div data-portfolio-path="Tennis" data-portfolio-id="5ac752e61c99a112046cf391" class="list-card js-member-droppable is-covered ui-droppable search-display-block sfilter ui-sortable-handle">

                                    <div class="list-card-cover js-card-cover portfolio-card" style="background-color: #02bf6f;
              
             color: #000000; ">

                                        <div class="portfolio-card-filter-block">
                                            <div class="portfolio-card-title portfolio-card-filter-heading ">
                                                <div class="accordion-toggle collapsed portfolio-toggle" data-toggle="collapse" data-parent="#portfolioAccordion" data-target="#portfolio5ac752e61c99a112046cf391" aria-expanded="false">
                                                    <span class="portfolio-accordion"></span>

                                                    <i class="fa fa-user-circle " aria-hidden="true" title="You are a member of this portfolio"></i>&nbsp;

                                                    <span class="archiware-archive-state" data-portfolio-path="Tennis" data-portfolio-id="5ac752e61c99a112046cf391"><i class="fa fa-database" aria-hidden="true" title="Has been archived"></i></span>                                                    Tennis
                                                    <span class="text-right"></span>

                                                </div>
                                            </div>
                                            
                                        </div>

                                    </div>

                                    
                                    <hr>
                                    <div class="list-card-details text-center">
                                        <div class="list-card-members js-list-card-members">
                                            <div class="portfolio-icon-list " data-example-id="portfolio-icon-list">
                                                <div class="btn-group portfolio-specific-btn portfolio-overview-btn" title="Portfolio Overview">
                                                    <a href="#" class="portfolio-overview">
                        <i class="fa fa-eye fa-1-5-font-size" aria-hidden="true"></i></a>
                                                </div>
                                                <div class="btn-group portfolio-specific-btn portfolio-checklist-btn" title="Portfolio Checklist">
                                                    <a href="#" class="portfolio-checklist">
                        <i class="fa fa-list fa-1-5-font-size" aria-hidden="true"></i></a>
                                                </div>

                                                <div class="btn-group portfolio-specific-btn open-portfolio-metadata-btn" data-dialog-id="5ac752e61c99a112046cf391" title="Portfolio Metadata">
                                                    <a href="#" class="portfolio-metadata">
                    <i class="fa fa-file-text fa-1-5-font-size" aria-hidden="true"></i></a>
                                                </div>

                                                <div class="btn-group portfolio-specific-btn open-portfolio-forum-btn" data-dialog-id="5ac752e61c99a112046cf391" title="Portfolio Forum">
                                                    <a href="#" class="portfolio-forum">
                        <i class="fa fa-commenting fa-1-5-font-size" aria-hidden="true"></i></a>

                                                </div>

                                            </div>
                                        </div>

                                        <div class="list-card-project-section">

                                            <div class="project-block project-block-primary open-project-dialog-btn" name="open-project-dialog-btn" data-dialog-id="project-dialog-motion-5ac752e61c99a112046cf391">
                                                Motion Sports: <span class="badge">1</span>
                                            </div>

                                            <div class="dialog project-dialog project-dialog-motion" name="project-dialog-motion" id="project-dialog-motion-5ac752e61c99a112046cf391" title="Awaiting Approval Motion Sports">

                                                <div class="project-card">
                                                    <div class="project-card-content">

                                                        <div class="fps-tag-block fps-tag-Motion">
                                                            <dl class="dl-horizontal">
                                                                <dt class="text-left">Storyline:</dt>
                                                                <dd>Motion</dd>
                                                            </dl>
                                                        </div>

                                                        <div class="fps-tag-block fps-tag-">
                                                            <dl class="dl-horizontal">
                                                                <dt class="text-left">Comment:</dt>
                                                                <dd></dd>
                                                            </dl>
                                                        </div>

                                                        <div class="fps-tag-block fps-tag-1">
                                                            <dl class="dl-horizontal">
                                                                <dt class="text-left">Version:</dt>
                                                                <dd>1</dd>
                                                            </dl>
                                                        </div>

                                                        <div class="fps-tag-block fps-tag-createddate">
                                                            <dl class="dl-horizontal">
                                                                <dt class="text-left">Created:</dt>
                                                                <dd>6-4-2018</dd>
                                                            </dl>
                                                        </div>

                                                        <div class="fps-tag-block fps-tag-inprogress">
                                                            <dl class="dl-horizontal">
                                                                <dt class="text-left">Status:</dt>
                                                                <dd>In Progress</dd>
                                                            </dl>
                                                        </div>

                                                        <div class="fps-tag-block fps-tag-{day=11, month=4, year=2018}">
                                                            <dl class="dl-horizontal">
                                                                <dt class="text-left">Completed Date:</dt>
                                                                <dd>11-4-2018</dd>
                                                            </dl>
                                                        </div>

                                                    </div>
                                                </div>

                                            </div>

                                            

                                            
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="js-list list-wrapper ">
                        <div class="list js-list-content">
                            <div class="list-header js-list-header u-clearfix is-menu-shown">
                                <div class="list-header-target js-editing-target"></div>
                                <h4 class="list-header-name-assist js-list-name-assist text-center" dir="auto">
                                    Approved
                                </h4>
                            </div>
                            <div class="sortable-list ui-sortable" data-column-id="approved" data-column-value="approved Approved">

                                <div class="ui-sortable-handle">

                                </div>

                            </div>
                        </div>
                    </div>

                    <div class="js-list list-wrapper ">
                        <div class="list js-list-content">
                            <div class="list-header js-list-header u-clearfix is-menu-shown">
                                <div class="list-header-target js-editing-target"></div>
                                <h4 class="list-header-name-assist js-list-name-assist text-center" dir="auto">
                                    Editing in Progress
                                </h4>
                            </div>
                            <div class="sortable-list ui-sortable" data-column-id="editinprog" data-column-value="editinprog Editing in Progress">

                                <div class="ui-sortable-handle">

                                </div>

                            </div>
                        </div>
                    </div>

                    <div class="js-list list-wrapper ">
                        <div class="list js-list-content">
                            <div class="list-header js-list-header u-clearfix is-menu-shown">
                                <div class="list-header-target js-editing-target"></div>
                                <h4 class="list-header-name-assist js-list-name-assist text-center" dir="auto">
                                    Awaiting Graphics
                                </h4>
                            </div>
                            <div class="sortable-list ui-sortable" data-column-id="awaitgraph" data-column-value="awaitgraph Awaiting Graphics">

                                <div class="ui-sortable-handle">

                                </div>

                            </div>
                        </div>
                    </div>

                    <div class="js-list list-wrapper ">
                        <div class="list js-list-content">
                            <div class="list-header js-list-header u-clearfix is-menu-shown">
                                <div class="list-header-target js-editing-target"></div>
                                <h4 class="list-header-name-assist js-list-name-assist text-center" dir="auto">
                                    Ready for Dubbing
                                </h4>
                            </div>
                            <div class="sortable-list ui-sortable" data-column-id="readydubbing" data-column-value="readydubbing Ready for Dubbing">

                                <div class="ui-sortable-handle">

                                </div>

                            </div>
                        </div>
                    </div>

                    <div class="js-list list-wrapper ">
                        <div class="list js-list-content">
                            <div class="list-header js-list-header u-clearfix is-menu-shown">
                                <div class="list-header-target js-editing-target"></div>
                                <h4 class="list-header-name-assist js-list-name-assist text-center" dir="auto">
                                    Ready for Grading
                                </h4>
                            </div>
                            <div class="sortable-list ui-sortable" data-column-id="readygrading" data-column-value="readygrading Ready for Grading">

                                <div class="ui-sortable-handle">

                                </div>

                            </div>
                        </div>
                    </div>

                    <div class="js-list list-wrapper ">
                        <div class="list js-list-content">
                            <div class="list-header js-list-header u-clearfix is-menu-shown">
                                <div class="list-header-target js-editing-target"></div>
                                <h4 class="list-header-name-assist js-list-name-assist text-center" dir="auto">
                                    Completed
                                </h4>
                            </div>
                            <div class="sortable-list ui-sortable" data-column-id="completed" data-column-value="completed Completed">

                                <div class="ui-sortable-handle">

                                </div>

                            </div>
                        </div>
                    </div>

                    <div class="js-list list-wrapper ">
                        <div class="list js-list-content">
                            <div class="list-header js-list-header u-clearfix is-menu-shown">
                                <div class="list-header-target js-editing-target"></div>
                                <h4 class="list-header-name-assist js-list-name-assist text-center" dir="auto">
                                    Not Approved -Do Not Distribute
                                </h4>
                            </div>
                            <div class="sortable-list ui-sortable" data-column-id="notapproved" data-column-value="notapproved Not Approved -Do Not Distribute">

                                <div class="ui-sortable-handle">

                                </div>

                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <div class="spacer-sml"></div>

        </form>

    </div>

</div>

答案 2 :(得分:0)

这应该有所帮助,将在第二部分提供具体案例演示:

    Sub newNew()


        Dim str As String
        str = "Nat Rep feasibility check for United States - Details by Region "
        str = Replace(str, "Nat Rep feasibility check for", "")

        findValue = InStr(1, str, "-", vbBinaryCompare)

        str = Left(str, findValue - 2)
    End Sub

具体案例:

Sub RowInt()
    Dim rng As Range
    For i = 1 To Sheets.Count
    Sheets(i).Select
    Cells.Range("A1").Select
    ActiveCell.EntireColumn.Insert
    ActiveCell.EntireColumn.Insert
    Range("A7").Value = "Channel"
    Range("B7").Value = "Country"

    NumRows = Range("C8", Range("C8").End(xlDown)).Rows.Count
        For j = 1 To NumRows - 1
         dim mystring as string
        ' you probably want to change 1 or 3 to a j not sure what your ws looks like
         mystring = Cells(1, 3).Value

        'add a space to end of string that you are replacing
            mystring = Replace(mystring, "Nat Rep feasibility check for ", "")
        'find the space where - is in the string
            Dim findValue As Integer
                findValue = InStr(1, mystring, "-", vbBinaryCompare)
        'take only the part of the string up to two characters before -
            mystring = Left(mystring, findValue - 2)
            Cells(j + 7, 2).Value = mystring
            Cells(j + 7, 1).Value = Range("C2").Value
        Next j
    Next i
End Sub

答案 3 :(得分:0)

使用拆分

示例测试子

Public Sub test()
    Debug.Print Split(Split("Nat Rep feasibility check for United States - Details by Region", "for" & Chr(32))(1), Chr(32) & Chr(45))(0)
End Sub

作为表单中的UDF

Public Function GetCountry(ByVal rng As Range) As String
    GetCountry = Split(Split(rng.Text, "for" & Chr$(32))(1), Chr$(32) & Chr$(45))(0)
End Function

在工作表中使用

Image