【问题标题】:Finding The First Empty Row At The End Of The Column And Pasting Data In It, Excel VB查找列末尾的第一个空行并将数据粘贴到其中,Excel VB
【发布时间】:2015-05-26 15:34:00
【问题描述】:
    Dim LastRow As Long

    LastRow = oSheet.Range("A4000").End("Excel.XlDirection.XlUp").Row + 1
    oSheet.Range("A" + LastRow).Value = "TEST"
    oSheet.Range("B" + LastRow).Value = "TEST"
  • Microsoft.VisualBasic.dll 中出现“System.InvalidCastException”类型的未处理异常
  • 附加信息:从字符串“Excel.XlDirection.XlUp”到类型“Integer”的转换无效。

【问题讨论】:

    标签: vb.net excel


    【解决方案1】:

    从您的 Xlup 参数中删除引号,并使用 & 而不是 + 来连接字符串。

    Dim LastRow As Long
    
    LastRow = oSheet.Range("A4000").End(Excel.XlDirection.xlUp).Row + 1
    oSheet.Range("A" & LastRow).Value = "TEST"
    oSheet.Range("B" & LastRow).Value = "TEST"
    

    【讨论】:

      【解决方案2】:

      要查找已使用的行数,您应该可以使用:

      Dim lastRow as Integer = oSheet.UsedRange.Rows.Count
      

      要执行单个值或单元格,您可以这样做:

      oSheet.Cells(lastRow, 1).value = "Test"
      oSheet.Cells(lastRow, 2).value = "Test"
      

      【讨论】:

      • 在 2DArray 之后应该有一个 end of statement + 我还有转换问题?
      • @HudsonWY 这只是一个例子,你没有任何东西被声明为 2DArray。查看我的编辑(以清除它)
      【解决方案3】:

      这是在 Excel 工作表中找到 LASTROW 的简单方法,通过在这个 LASTROW 上加 1,我们可以找到 EMPTYROW

      '程序从这里开始

      Private Sub f_ExcelWorksheet()
              Dim oExcel As Object
              Dim oBook As Object
              Dim oSheet As Object
      
              'Start a new workbook in Excel
              oExcel = CreateObject("Excel.Application")
              oBook = oExcel.Workbooks.Add
      
      
              'Add data to cells of the first worksheet in the new workbook
              oSheet = oBook.Worksheets(1)
              oSheet.Range("A1").Value = "Last Name"
              oSheet.Range("B1").Value = "First Name"
              oSheet.Range("A1:B1").Font.Bold = True
              oSheet.Range("A2").Value = "Doe"
              oSheet.Range("B2").Value = "John"
              'This will find the lastRow in the sheet
              Dim lastRow As Integer = oSheet.UsedRange.Rows.Count
              'This is next emptyRow in the sheet
              Dim emptyRow As Integer = lastRow + 1
              MessageBox.Show(lastRow)
              oSheet.Cells(emptyRow, 1).value = "Test"
              oSheet.Cells(emptyRow, 2).value = "Test"
              'Now again find the lastRow in the excel sheet
              lastRow = oSheet.UsedRange.Rows.Count
              'This is next emptyRow in the sheet
              emptyRow = lastRow + 1
      
              'Save the Workbook and Quit Excel
              oBook.saveas("C:\Users\adimadud\Desktop\Test.xlsx")
              oExcel.Quit()
      
          End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多