xin-ge 发表于 2011-12-2 09:54:57

如何判断行数非空?为什么不行

因为我下面是循环,决定从yline那一行开始写数据
如果是个新表,那么就从第二行 yline=2写数据

如果不是新表(就是已经写过一次数据了),我就接着上次的写
想得到yline的行数————就是上一行数据的下面一行的行号!!!
这个表的表头就是 块名称阀名称数量
我的代码不对,调试不成功!


能帮我改一下么?   
Dim Excel As Object
Dim ExcelSheet As Object
Dim ExcelWorkbook As Object
'创建Excel应用程序实例

On Error Resume Next
Set Excel = GetObject(, "Excel.Application")
Set ExcelSheet = Excel.ActiveSheet
Excel.Visible = True
   If ExcelSheet.Cells(1, 1).Value = "" Then
         ExcelSheet.Cells(1, 1).Value = "块名称"
         ExcelSheet.Cells(1, 2).Value = "阀名称"
         ExcelSheet.Cells(1, 3).Value = "数量"
         yline = 2 '写入行位置
    Else
yline = ExcelSheet.sheets(1).UsedRange.Rows.Count
   End If



黄玉宏 发表于 2011-12-2 11:32:56

Private Sub Command1_Click()
Dim Excel As Object
Dim ExcelSheet As Object
Dim ExcelWorkbook As Object
'创建Excel应用程序实例

On Error Resume Next
Set Excel = GetObject(, "Excel.Application")
Set ExcelSheet = Excel.ActiveSheet
Excel.Visible = True
With ExcelSheet
   If Len(.cells(1, 1)) = 0 Then
         .cells(1, 1) = "块名称"
         .cells(1, 2) = "阀名称"
         .cells(1, 3) = "数量"
    End If
   
    N = .cells(65536, 1).End(-4162).Row + 1
    .cells(N, 1) = "你的数据"
End With
End Sub
不建议你Excel申明为后期绑定,正确的应用前期绑定。这也就是你找不到空行原因。

xin-ge 发表于 2011-12-2 15:16:43

End(-4162).Row ?这个不明白

xin-ge 发表于 2011-12-2 15:17:32

我用下面的程序搞定了

If ExcelSheet.Cells(1, 1).Value = "" Then
         ExcelSheet.Cells(1, 1).Value = "块名称"
         ExcelSheet.Cells(1, 2).Value = "阀名称"
         ExcelSheet.Cells(1, 3).Value = "数量"
         yline = 2 '写入行位置
      Else
   yline = ExcelSheet.UsedRange.Rows.Count + 1
    End If
页: [1]
查看完整版本: 如何判断行数非空?为什么不行