Dim excel As Object
excel = CreateObject("excel.application")
Dim book As Object
Dim sheet As Object
excel.visible = True
book = excel.workbooks.add
sheet = book.worksheets(1)
sheet.Cells(1, 1) = 1
sheet.Cells(1, 2) = 2
sheet.Columns(2).select()
excel.Selection.Cut()
sheet.Columns(1).select()
excel.selection.insert()
Dim excel As Object
excel = CreateObject("excel.application")
Dim book As Object
Dim sheet As Object
excel.visible = True
book = excel.workbooks.open("d:\ssss.xlsx") '打开一个文件
sheet = book.worksheets(1)
Dim excel As Object
excel = CreateObject("excel.application")
Dim book As Object
Dim sheet As Object
excel.visible = False
book = excel.workbooks.open("d:\abc.xlsx")
sheet = book.worksheets(1)
Dim str1 As String
Dim str2 As String
str1 = sheet.Cells(1, 1).value
str2 = sheet.Cells(1, 2).value
sheet.Columns(2).select()
excel.Selection.Cut()
sheet.Columns(1).select()
excel.selection.insert()
sheet.Cells(1, 1).value = str1
sheet.Cells(1, 2).value = str2
book.save()
excel.quit()
Dim excel As Object
excel = CreateObject("excel.application")
Dim book As Object
Dim sheet As Object
excel.visible = False
book = excel.workbooks.open("d:\abc.xlsx")
sheet = book.worksheets(1)
Dim str1 As String
Dim str2 As String
Dim i As Integer
i = 2
Do While True
str1 = CStr(sheet.Cells(i, 1).value)
str2 = CStr(sheet.Cells(i, 2).value)
sheet.Cells(i, 1).value = str2
sheet.Cells(i, 2).value = str1
i = i + 1
If str1 = "" And str2 = "" Then Exit Do
Loop
book.save()
excel.quit()
我用这样的方法解决的,说穿了,就是把第一列复制到第三列,然后删除第一列,就完成了第一列和第二列的位置交换:
Private Sub Command1_Click()
Dim z As Integer
Dim i As Integer
Dim s As String
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
For z = 0 To File1.ListCount - 1
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(Dir1.Path & "\" & File1.List(z)) '打开你的EXCEL文件
xlApp.DisplayAlerts = False
xlApp.Visible = False
Set xlSheet = xlBook.Worksheets(1) '第一个表格
xlSheet.Application.Visible = False '设置Excel 不可见
xlSheet.Columns(1).Copy xlSheet.Columns(3)
xlSheet.Columns(1).Delete
xlBook.Close True '先保存修改再关闭工作簿
xlApp.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
prcERR:
Debug.Print Err.Number & ":" & Err.Description
Next z
MsgBox "OK"
End Sub