.Rows()不接受字符串值

时间:2018-06-18 10:07:31

标签: excel vba excel-vba

我正在编写一个例程来插入一些行,并复制一些值。 需要插入的行的数量和位置是动态的,并存储在变量中。

我知道我可以使用Activesheet.rows("2:12").Insert shift:=xlDown

插入行

现在因为2和12是动态的,我声明了数据类型Long的变量来保存这些值。例如:

Dim Startvalue as Long
Startvalue = 2
Dim Lengthofinsert as Long
Lengthofinsert = 10
Dim Endvalue as Long
Endvalue = Startvalue + Lengthofinsert

然后我写了以下内容:Activesheet.rows("""" & Startvalue & ":" & Endvalue & """").Insert shift:=xlDown导致类型不匹配。

使用debug.print("""" & Startvalue & ":" & Endvalue & """"),结果为"2:12",与我在上面的第一个insert()语句中输入的内容完全相同。

我想也许首先将字符串存储为string变量可能有所帮助,但不幸的是,这也会导致相同的类型不匹配。

现在,我认为行应该采用某种ArrayRange作为参数,但如何将我的两个Long转换为?

2 个答案:

答案 0 :(得分:4)

只需使用

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
<div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>S.No</th>
                                    <th>Product Name</th>
                                    <th>BF/AF</th>
                                    <th>M</th>
                                    <th>A</th>
                                    <th>E</th>
                                    <th>N</th>
                                    <th ng-model="TotalQuantity" class="ng-pristine ng-untouched ng-valid ng-empty">Total Quantity</th>
                                    <th ng-model="Price" class="ng-pristine ng-untouched ng-valid ng-empty">Price</th>
                                    <th ng-model="interaction" class="ng-pristine ng-untouched ng-valid ng-empty">Interaction</th>

                                </tr>
                            </thead>
                            <tbody>
                                <!-- ngRepeat: product in products --><tr ng-repeat="product in products" class="ng-scope">
                                    <td class="ng-binding">1</td>
                                    <td class="ng-binding">1 AL</td>
                                    <td class="ng-binding">AF</td>
                                    <td class="ng-binding">1</td>
                                    <td class="ng-binding">0</td>
                                    <td class="ng-binding">0</td>
                                    <td class="ng-binding">1</td>
                                    <td class="ng-binding">
                                        1

                                    </td>
                                    <td class="ng-binding">24</td>
                                    <td><a data-toggle="modal" data-target="#myModal" data="1 AL" ng-click="getProductName(product.ProductID)">Details</a>
                                        <div class="modal fade" id="myModal" role="dialog">
                                            <div class="modal-dialog">

                                                <!-- Modal content-->
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h4 class="modal-title ng-pristine ng-untouched ng-valid ng-binding ng-empty" ng-model="WProductName"> Suppository related warnings</h4>
                                                        <button type="button" class="close" data-dismiss="modal">×</button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <div class="firstone">
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/alcohol.png" title="Alcohol">
                                                                <span class="ng-binding">CAUTION</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/pregnancy.png" title="Pregnancy">
                                                                <span ng-model="Pregnancy" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">PROBABLY SAFE</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/lactation.png" title="Lactation">
                                                                <span ng-model="Lactation" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">SAFE</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/driving.png" title="Driving">
                                                                <span ng-model="Driving" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">SAFE</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/kidney.png" title="Kidney">
                                                                <span ng-model="Kidney" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">CAUTION</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/liver.png" title="liver">
                                                                <span ng-model="Liver" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">SAFE</span>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                                    </div>
                                                </div>

                                            </div>
                                        </div>
                                    </td>
                                    <!-- Modal -->


                                </tr><!-- end ngRepeat: product in products --><tr ng-repeat="product in products" class="ng-scope">
                                    <td class="ng-binding">2</td>
                                    <td class="ng-binding">Practin</td>
                                    <td class="ng-binding">AF</td>
                                    <td class="ng-binding">1</td>
                                    <td class="ng-binding">0</td>
                                    <td class="ng-binding">1</td>
                                    <td class="ng-binding">1</td>
                                    <td class="ng-binding">
                                        1

                                    </td>
                                    <td class="ng-binding">26</td>
                                    <td><a data-toggle="modal" data-target="#myModal" data="Practin" ng-click="getProductName(product.ProductID)">Details</a>
                                        <div class="modal fade" id="myModal" role="dialog">
                                            <div class="modal-dialog">

                                                <!-- Modal content-->
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h4 class="modal-title ng-pristine ng-untouched ng-valid ng-binding ng-empty" ng-model="WProductName"> Suppository related warnings</h4>
                                                        <button type="button" class="close" data-dismiss="modal">×</button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <div class="firstone">
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/alcohol.png" title="Alcohol">
                                                                <span class="ng-binding">CAUTION</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/pregnancy.png" title="Pregnancy">
                                                                <span ng-model="Pregnancy" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">PROBABLY SAFE</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/lactation.png" title="Lactation">
                                                                <span ng-model="Lactation" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">SAFE</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/driving.png" title="Driving">
                                                                <span ng-model="Driving" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">SAFE</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/kidney.png" title="Kidney">
                                                                <span ng-model="Kidney" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">SAFE</span>
                                                            </div>
                                                            <div class="alcohol">
                                                                <img src="/img/Interaction/liver.png" title="liver">
                                                                <span ng-model="Liver" class="ng-pristine ng-untouched ng-valid ng-binding ng-empty">SAFE</span>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                                    </div>
                                                </div>

                                            </div>
                                        </div>
                                    </td>
                                    <!-- Modal -->


                                </tr><!-- end ngRepeat: product in products -->
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th colspan="8" style="font-size: 1.5em;">Total</th>
                                    <th id="right" colspan="3" style="font-size: 1.5em;"><span ng-bind="total" style="margin-left: -1em;" class="ng-binding"></span><span class="ng-binding"> ₹ 50</span></th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
</body>

</html>

您不需要ActiveSheet.Rows(Startvalue & ":" & Endvalue).Insert shift:=xlDown ,因为""已经是一个字符串(由于与Startvalue & ":" & Endvalue的串联,长值会自动转换为字符串。)

同样

&

或@Chronocidal在评论中指出:
比较输出

Dim MyRows As String
MyRows = Startvalue & ":" & Endvalue
ActiveSheet.Rows(MyRows).Insert shift:=xlDown

答案 1 :(得分:0)

您是否创建了Thisworksheet?你的声明中没有提到它。

假设你有,

尝试

ThisWorkSheet.rows(Startvalue & ":" & Endvalue).Insert shift:=xlDown

否则尝试:

Activesheet.rows(Startvalue & ":" & Endvalue).Insert shift:=xlDown