zoukankan      html  css  js  c++  java
  • VBScript

    本文主要记录,使用 VBScript,如何找到 Excel 页面中的,最后一行。


    参考 VBA 中的解决方式:

    很多 VBA 与 VBScript 的解决方法都是通用的,尤其是针对 Excel 的时候,
    所以,我们先来看下 VBA 中,常用的3中方法:

    
    '从页面最后一行,按 Ctrl + Up 箭头
      LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    
    '使用 UsedRange 属性
      LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row
    
    '使用 SpecialCells 函数
      LastRow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row
    
    

    VBScript 中的解决方式:

    然后,我们来看下,VBScript中,要怎么达到上面的效果,
    如果,要是把上面代码,直接放到 VBScript 中运行,就会出现位置错误,
    这中间确实,有一个 Trick,是我之前没注意到的,
    就是,VBScript 中必须手动设置,Constant (常量),
    而,VBA 中,这些常量的值,是默认的,不用设置,
    那么,来看下 VBScript 的代码:

    
    '先设定要使用到的 Objects(对象)
        Set oExcel = GetObject(,"Excel.Application")
        Set wb = oExcel.Workbooks("Book10 - Copy.xlsm")
        Set sht = wb.worksheets("Data")
        wb.Activate
    
    '从页面最后一行,按 Ctrl + Up 箭头
        Const xlUp = -4162
        LastRow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
        MsgBox LastRow 
    
    '或者,使用 Range,和 Ctrl + Up 箭头
        Const xlUp = -4162
        LastRow = sht.Range("G" & sht.Rows.Count).End(xlUp).Row
        MsgBox LastRow 
    
    '或者,使用 UsedRange 属性
        LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row
        MsgBox LastRow 
    
    '或者,使用 SpecialCells 函数
        Const xlCellTypeLastCell = 11
        LastRow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row
        MsgBox LastRow 
    
    

    以上,就是 VBScript 中的实现方法,
    常量的数值可以去,微软官方文档中去找,
    常用的常量,有下面四种,列出来供大家参考:

        Constant	    Value
        xlDownward	    -4170
        xlHorizontal    -4128
        xlUpward	    -4171
        xlVertical	    -4166
    

    参考阅读:

    1. 5 Different Ways to Find The Last Row or Last Column Using VBA — The Spreadsheet Guru
    2. Find bottom of a column in Excel using VBScript - Stack Overflow
    3. Microsoft Excel Constants [Excel 2003 VBA Language Reference] | Microsoft Docs


  • 相关阅读:
    HTML tabIndex 和 accesskey属性
    JS加载顺序
    CSS3 Box Shadow
    CSS中背景的Linear Gradients(线性渐变)
    CSS优先级
    CSS3 圆角(borderradius)
    JavaScript window.location对象
    JsDoc Toolkit
    Javascript标准DOM Range操作
    CSS3 Text Shadow
  • 原文地址:https://www.cnblogs.com/bitssea/p/12651008.html
Copyright © 2011-2022 走看看