在列的末尾找到第一个空行并在其中粘贴数据,Excel VB

时间:2015-05-26 15:34:00

标签: vb.net excel

    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"
  • 未处理的类型' System.InvalidCastException'发生在Microsoft.VisualBasic.dll
  • 附加信息:从字符串转换" Excel.XlDirection.XlUp"输入'整数'无效。

3 个答案:

答案 0 :(得分: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"

答案 1 :(得分:0)

要查找已使用的行数,您应该能够使用:

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

要执行单个值或单元格,您可以执行以下操作:

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

答案 2 :(得分:0)

以下是在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