在列结束处find第一个空行并在其中粘贴数据,Excel VB
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中发生未处理的types为“System.InvalidCastException”的exception
- 其他信息:从string“Excel.XlDirection.XlUp”转换为types“整数”无效。
从Xlup参数中删除引号,并使用&
连接string而不是+
。
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"
要find使用的行的数量,你应该能够使用这个:
Dim lastRow as Integer = oSheet.UsedRange.Rows.Count
要做一个单一的值或单元格,你可以这样做:
oSheet.Cells(lastRow, 1).value = "Test" oSheet.Cells(lastRow, 2).value = "Test"
这里是在excel表单中findLASTROW的简单方法,并且通过在这个LASTROW中添加1,我们可以findEMPTYROW
“节目从这里开始
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