为什么这个程序的 Grid1 总是显示变量未定义?求高手指教
Private Sub CmdPrint_Click()Dim ex As Excel.Application
Dim a As String
Dim i As Integer, j As Integer
On Error Resume Next
If Grid1.Rows < 3 Then
Exit Sub
End If
If MsgBox("是否进行打印?", vbYesNo, "报表打印") = vbNo Then
Exit Sub
End If
CmdPrint.Enabled = False
cmdQuery.Enabled = False
cmdClear.Enabled = False
SetTipInfo ("正在生成报表,请等待")
Set ex = New Excel.Application
ex.Workbooks.Add
ex.Range("A1:" & Chr(Asc("A") + Grid1.Cols - 1) & "1").Select
With ex.Selection
.MergeCells = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.Font.Bold = True
.Font.Name = "宋体"
.Font.Size = 14
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
ex.Range("A1").Value = CStr(Lu) & "#炉" & TabStrip1.SelectedItem.Caption & "报表"
ex.Range("A2:" & Chr(Asc("A") + Grid1.Cols - 1) & CStr(Grid1.Rows)).Select
With ex.Selection
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlInsideVertical).LineStyle = xlContinuous
.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.Font.Size = 10
End With
With ex
For i = 0 To Grid1.Rows - 2
For j = 65 To Grid1.Cols + 65
.Range(Chr(j) & CStr(i + 2)).Value = Grid1.TextMatrix(i, j - 65)
DoEvents
Next
Next
End With
If TabStrip1.SelectedItem.Key = "高压历史参数" Then
ex.Columns("A:A").ColumnWidth = 15 '日期
ex.Columns("B:B").ColumnWidth = 15 '时间
ex.Columns("C:C").ColumnWidth = 15 '除尘器名称
ex.Columns("D:D").ColumnWidth = 15 '室
ex.Columns("E:E").ColumnWidth = 15 '电场
ex.Columns("C:C").ColumnWidth = 15 '运行状态
ex.Columns("K:K").ColumnWidth = 4.5 '油温
ex.Columns("L:L").ColumnWidth = 4.5 'IGBT温度
ex.Columns("M:M").ColumnWidth = 4.5 '闪频
Else
ex.Columns("A:A").ColumnWidth = 15 '日期
ex.Columns("B:B").ColumnWidth = 20 '设备名称
ex.Columns("G:G").ColumnWidth = 30 '故障描述
End If
'设置页边距
With ex.ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.196850393700787)
.RightMargin = Application.InchesToPoints(0.196850393700787)
.TopMargin = Application.InchesToPoints(0.196850393700787)
.BottomMargin = Application.InchesToPoints(0.196850393700787)
.CenterHorizontally = True
End With
ex.Visible = True
ex.ActiveWorkbook.Saved = True
ex.Quit
Set ex = Nothing
SetTipInfo ("打印完毕")
CmdPrint.Enabled = True
cmdQuery.Enabled = True
cmdClear.Enabled = True
End Sub --------------------编程问答-------------------- 不是自己写的代码吧? 这Grid1明显就是个表格控件, 可能是MSFLEXHGRID或者是MSFLEXGRID,也有可能是第三方表格控件. --------------------编程问答-------------------- 除
补充:VB , 数据库(包含打印,安装,报表)