关于vb控制excel问题
excelXLS = CreateObject("Excel.Application")
excelWK = excelXLS.Workbooks.Open(strFieldName)
// j = 0
excelWS = excelWK.Worksheets(j + 1) //选中sheet1 excelWK.Sheets.Add()
excelWK.Sheets("Sheet1").Select()
excelWS.Range("A1:Y10").Select()
excelWS.Range("A1:Y10").Copy()
excelWK.Sheets("Sheet" & (j + 2)).Select()
excelWK.ActiveSheet.Paste()
excelWS = excelWK.Worksheets(j + 2)
excelWS.Cells.Select()
'excelWS.Selection.ColumnWidth = 5
excelWS.Cells.ColumnWidth = 5
excelWS = excelWK.Worksheets(j + 1)
流程是 在sheet1后面新加一个sheet 然后复制sheet1的 A1:Y10 粘贴到sheet2 然后全选sheet2 把列宽改成5 我在excelWS.Cells.Select()
这里报错 说类range 的select无效,求各位帮忙看看 Excel VB
[解决办法]
试了一下,没问题,实在不行自己调试看看
Dim excelXLS As Excel.Application
Dim excelWK As Workbook
Dim excelWS As Worksheet
Dim j As Integer = 0
excelXLS = CreateObject("Excel.Application")
excelXLS.Visible = True
excelWK = excelXLS.Workbooks.Open("C:\test.xlsx")
excelWS = excelWK.Worksheets(j + 1) '//选中sheet1 excelWK.Sheets.Add()
excelWK.Sheets("Sheet1").Select()
excelWS.Range("A1:Y10").Select()
excelWS.Range("A1:Y10").Copy()
excelWK.Sheets("Sheet" & (j + 2)).Select()
excelWK.ActiveSheet.Paste()
excelWS = excelWK.Worksheets(j + 2)
excelWS.Cells.Select()
'excelWS.Selection.ColumnWidth = 5
excelWS.Cells.ColumnWidth = 2
excelWS = excelWK.Worksheets(j + 1)
excelWK.Save()
excelWK.Close()