VB问题删除指定excel行

http://zhidao.baidu.com/question/580792598.html?quesup2&oldq=1里你的回答确实有效,非常感谢!不过还有两个问题,一是没有找到最新的xls文件,二是在删除指定行后并没有自动保存并退出excel,能否解决此问题?再追加50分,谢谢

'菜单“工程/引用”,勾选Microsoft Excel 11库,必须的

Private Sub Command1_Click()

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then Set xlApp = CreateObject("Excel.Application")
On Error GoTo prcERR
Set xlBook = xlApp.Workbooks.Open(App.Path & "\test.xls")  '打开你的EXCEL文件
Set xlSheet = xlBook.Worksheets(1)'第一个表格
xlSheet.Application.Visible = True '设置Excel 可见
  xlSheet.Rows("1:1").Delete Shift:=xlUp'假如要删除第1行。删除第2行就是"2:2",删除1-3行就是"1:3"
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Exit Sub
prcERR:
 Debug.Print Err.Number & ":" & Err.Description
End Sub

追问

那怎么自动保存修改后的excel呢?

追答Private Sub Command1_Click()
 
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then Set xlApp = CreateObject("Excel.Application")
On Error GoTo prcERR
Set xlBook = xlApp.Workbooks.Open(App.Path & "\test.xls")  '打开你的EXCEL文件
Set xlSheet = xlBook.Worksheets(1)'第一个表格
xlSheet.Application.Visible = True '设置Excel 可见
  xlSheet.Rows("1:1").Delete Shift:=xlUp'假如要删除第1行。删除第2行就是"2:2",删除1-3行就是"1:3"
 
xlBook.DisplayAlerts = False     '不显示警告信息
xlBook.Close True  '先保存修改再关闭工作簿
xlBook.Quit   '关闭Excel 

Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Exit Sub
prcERR:
 Debug.Print Err.Number & ":" & Err.Description
End Sub

温馨提示:答案为网友推荐,仅供参考
相似回答