Sub SetXAxisHeadings(ByVal oRs As ADODB.Recordset, ByVal strValueCol As String)
  Dim iCount As Integer
  Dim iRow, iCol As Integer
  Dim nMaxRows As Integer
  Dim oFill As Excel.ChartFillFormat
  
  '--- 检查参数是否合法
  If (IsNull(oRs) Or IsNull(strValueCol) _
         Or oExcelChart.SeriesCollection.Count <  1) Then
    Err.Raise Number:=1001 + vbObjectError, _
         Description:="Invalid recordset or column name"
    Exit Sub
  End If
  
On Error GoTo hError
  '--- 单个数据系列中最大数据个数
  nMaxRows = 25
  '--- 设置初始值和位置
  oRs.MoveFirst
  iRow = 0: iCol = 1
  '--- 循环,将记录集中的数据写入工作表第一列
  While (Not oRs.EOF And iRow <  nMaxRows)
    iRow = iRow + 1
    '--- 设置单元格的值
    oExcelSheet.Cells(iRow, iCol) = CStr(oRs(strValueCol).Value)
    '--- 下一行
    oRs.MoveNext
  Wend
  
  '--- 检查是否确实写入了数据
  If (iRow  > 0) Then
    '--- 将这些数据设置为X-轴标签
    oExcelChart.SeriesCollection(1).XValues = _
      oExcelSheet.Range(oExcelSheet.Cells(1, iCol), _
      oExcelSheet.Cells(iRow, iCol))
  End If
  
  Exit Sub
  
hError:
  App.LogEvent Err.Description, vbLogEventTypeError
  Err.Raise Err.Number, Err.Source, Err.Description
End Sub
 
  |