VB操作Excel动态单元格选择问题
VB操作Excel绘制图表,当N=5时,图表横坐标为5个值,当N=i时,图表横坐标为i个值,横坐标单元格选择怎么写Char.FullSeriesCollection(i).XValues = "sheet(1)!$B$6:$F$6",请各位前辈指教!
Dim EXAPP As Excel.Application Dim WB As Excel.Workbook Dim sht As Excel.Worksheet Dim chartPage As Excel.Chart Dim xlCharts As Excel.ChartObjects Dim myChart As Excel.ChartObject Dim chartRange As Excel.Range Private Sub Command1_Click() Set xlCharts = sht.ChartObjects Set myChart = xlCharts.Add(10, 80, 300, 250) Set chartPage = myChart.Chart Set chartRange = sht.Range("a1", "d5") Call chartPage.SetSourceData(Source:=chartRange) chartPage.ChartType = Excel.XlChartType.xlColumnClustered End Sub Private Sub Form_Load() Set EXAPP = CreateObject("excel.application") EXAPP.Visible = True Set WB = EXAPP.Workbooks.Open("c:\chart.xlsx") Set sht = WB.Worksheets("sheet1") End Sub Private Sub Form_Unload(Cancel As Integer) WB.Close Set myChart = Nothing Set xlCharts = Nothing Set sht = Nothing Set WB = Nothing Set EXAPP = Nothing End Sub
Private Sub Command1_Click() Cls Print 1, 列号转列名(1) Print 26, 列号转列名(26) Print 27, 列号转列名(27) Print 255, 列号转列名(255) End Sub Public Function 列号转列名(cs As Long) As String 'cs 范围 =1-255 ,根据你的需要定义变量类型 Dim i As Long, j As Long Dim s As String If cs < 1 Or cs > 255 Then Exit Function End If i = (cs - 1) Mod 26 'A要余0,Z余25 j = (cs - 1) \ 26 '26得0,27才能得1 If j > 0 Then '有第一位 s = Chr(64 + j) End If s = s & Chr(65 + i) '生成第二位 列号转列名 = s '返回结果 End Function