如何使用VBA获取数据的最后一列

时间:2019-08-02 10:37:27

标签: excel vba

我无法获取代码来正确识别数据的最后一列。最后一栏应为<div id="field"> <label>Add List Items</label><br/> <input autocomplete="off" class="inputlist" id="listid01" name="list[0][]" type="text" placeholder="Type List Item Name" data-items="8"/><input autocomplete="off" class="inputlist" id="listid02" name="list[0][]" type="text" placeholder="Type List Item Name" data-items="8"/><button id="removelist0" class="btn add-more-list" type="button">+</button> </div> <script> $(document).ready(function(){ var next = 0; $(".add-more-list").click(function(e){ e.preventDefault(); var addto = "#listid" + next+"2"; var addRemove = "#listid" + next+"2"; next = next + 1; var newIn = '<input autocomplete="off" class="inputlist form-control" id="listid' + next + '1" name="list['+next+'][]" type="text"><input autocomplete="off" class="inputlist form-control" id="listid' + next + '2" name="list['+next+'][]" type="text">'; var newInput = $(newIn); var removeBtn = '<button id="removelist' + (next - 1) + '" class="btn btn-danger remove-list" >-</button></div><div id="listid">'; var removeButton = $(removeBtn); $(addto).after(newInput); $(addRemove).after(removeButton); $("#listid" + next).attr('data-source',$(addto).attr('data-source')); $("#countlist").val(next); $('.remove-list').click(function(e){ e.preventDefault(); var fieldNum = this.id.charAt(this.id.length-1); var fieldID1 = "#listid" + fieldNum + "1"; var fieldID2 = "#listid" + fieldNum + "2"; $(this).remove(); $(fieldID1).remove(); $(fieldID2).remove(); }); }); }); </script> ,但是运行该函数时,我将$Q$9作为我的最后一栏。我在做什么错了?

代码

$M$9

范围的屏幕截图

enter image description here

2 个答案:

答案 0 :(得分:0)

替换

lastrow = ws.Range("M" & ws.Rows.Count).End(xlUp).Row
lastcol = ws.Cells(9, Columns.Count).End(xlToLeft).Column

使用

With ws
    lastrow = .Cells(.Rows.Count, "M").End(xlUp).Row
    lastcol = .Cells(9, .Columns.Count).End(xlToLeft).Column
End With

我还建议定义您在函数中似乎没有完成的变量

Function Q3calls()
   Dim wb as Workbook
   Dim LastRow as Long, LastCol as Long
   Dim rng as Range

   set wb = ActiveWorkbook

    With wb.Sheets("clientmenu")
        LastRow = .Cells(.Rows.Count, "M").End(xlUp).Row
        LastCol = .Cells(9, .Columns.Count).End(xlToLeft).Column

        Set rng = .Range(.Cells(8, 13), .Cells(LastRow, LastCol))
    End With

    With wb.Sheets("Sheet1")
        Q3calls = Application.WorksheetFunction.CountIfs(rng, ">=" & .Range("A67"), rng, "<=" & .Range("B67")) 'q3
    End With

End Function

答案 1 :(得分:0)

您还可以使用此功能:

Function LastCol(Sh As Worksheet)
    On Error Resume Next
    LastCol = Sh.Cells.Find(What:="*", _
                            After:=Sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
    On Error GoTo 0
End Function

那么LastColumn将是

lastcol = LastCol(wb.Sheets("clientmenu"))