用值计算行数(最大值)

时间:2017-06-08 14:18:20

标签: excel vb.net count max

我想计算具有值的行。我试过了oSheet.Rows.Count,但这不起作用。对此有何想法?

我的代码如下:

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add

oSheet = oBook.Worksheets("Sheet")

oSheet.Range("A" & max).Value = "0000111"
oSheet.Range("B1").Value ="Name"

oBook.SaveAs("C:\New folder\excel\" & datenw & ".xlsx")
oExcel.Quit()

2 个答案:

答案 0 :(得分:1)

正如评论中所述,以下代码应该为您提供具有基于$newName= $_POST["changeT"]; $userId = $_POST["userId"]; $db = mysqli_connect("trolö", "trolö", "trolö123", "trolö") $sql = "UPDATE user SET username = '$newName' WHERE user_id = '$userId'"; $query = mysqli_query($db, $sql); $response["successU"] = true; 的值的行数:

Range

但我相信您的代码存在轻微问题。这可能不会起作用:

Dim rowCount As Integer = oSheet.UsedRange.Rows.Count()

它赢得的原因是因为" Sheet"新oSheet = oBook.Worksheets("Sheet") 上不存在。 "工作表Sheet"是的,所以这需要改为:

Workbook

最后,我会查看您关闭oSheet = oBook.Worksheets("Sheet1") 'or oSheet = oBook.Worksheets(1) 'remember Excel collections are one based not zero based 的方式,因为Excel可能正在运行Excel实例。请查看与enter image description hereanswer位相关联的Siddharth Rout's

oExcel.Quit()

您还要确保以正确的顺序发布并释放所有内容。这通常是向后的顺序:

Private Sub ReleaseObject(ByVal obj As Object)
    Try
        Dim intRel As Integer = 0
        Do
            intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        Loop While intRel > 0
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

然而,尽管如此,我会直接使用code而不是声明对象:

ReleaseObject(oSheet)
oBook.Close()
ReleaseObject(oBook)
oExcel.Quit()
ReleaseObject(oExcel)

然后我会考虑转向Microsoft.Office.Interop.Excel namespace

  

将隐式数据类型转换限制为仅扩展转换,禁止后期绑定,并禁止导致Object类型的隐式类型。

答案 1 :(得分:0)

将行变量定义为Long,然后启动一个循环,当它在A列中找到空白值时结束:

Dim lRow as Long = 1

Do until oSheet.Range("A" & lRow).Value=""
  ' increment the loop variable
  lRow+=1
Loop

' display the result in a message block
MsgBox(lRow-1)