Board logo

標題: [發問] VB.net 如何計算Excel中A欄位xlDown? [打印本頁]

作者: faye59    時間: 2018-6-22 05:38     標題: VB.net 如何計算Excel中A欄位xlDown?

請問VB.net要如何算Excel內For迴圈的結束值?
我用VBA的方法都顯示xlDown跟xlUp都是無效

Wo                                  Op             Time
M123456789        OAG        01 10:00:00
M123456789        HAG        01 10:00:00
M123456789        SAG        01 10:00:00
M123456788        OAG        02 10:00:00
M123456788        SAG        02 10:00:00
  1.     Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  2.         Dim WB, XL, oSheet As Object
  3.         XL = CreateObject("Excel.Application")
  4.         XL.Visible = False
  5.         WB = XL.Workbooks.Open("\\Test\test.xlsx", True)
  6.         oSheet = XL.Workbooks("Test.xlsx").Sheets("Text1")
  7.         'For i As Long = 1 To oSheet.Range("A100").End(xlUp).Row
  8.         'Dim aa As String = Wo.Text
  9.         'Dim bb As String = oSheet.Range("A2").End(xlUp)
  10.         'Dim rng As Range
  11.         rng = XL.range(XL.cells(1, 1), XL.cells(1, 1).End(xlDown))
  12.         For i As Integer = 1 To rng'要如何計算Excel內會迴圈總數?
  13.             oSheet.cells(4, i).value = "Test"
  14.         Next
  15.         XL.Workbooks("Test.xlsx").Save()
  16.         XL.DisplayAlerts = True
  17.         MsgBox("OK")
  18.         XL.Workbooks("Test.xlsx").Close()
  19.         XL.Application.Quit()
  20.         XL = Nothing
  21.         WB = Nothing
  22.         oSheet = Nothing

  23.     End Sub
複製代碼

作者: faye59    時間: 2018-7-5 01:39

已找到解答了,分享給各位!

Constant        Value
xlDown        -4121
xlToLeft        -4159
xlToRight        -4161
xlUp        -4162
  1. Const xlUp = -4162
  2. Const xlDown = -4121
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)