2017-10-09 42 views
0

我寫了一個vbscript來將xlsx文件的特定範圍保存到csv文件。 我想選擇不相鄰的不同列。每列的範圍應包含其內容的最後一行(範圍("F6").End(xlToRight))。
我的代碼:vbscript在範圍命令的xlsx文件中選擇不同的行

Public Sub xlsToCsv()  
    Const WorkingDir = "C:\" 
    Const xlCSV = 6 

    Dim fso, SaveName, myFile 
    Dim objExcel, objWorkbook, sheet 

    myFile = "test.xlsx" 
    SaveName = "test.csv" 

    With CreateObject("Scripting.FilesystemObject") 
     If Not .FileExists(WorkingDir & myFile) Then 
      MsgBox "File not found:" & vbCrLf & WorkingDir & myFile, vbInformation, "Script Cancelled" 
      WScript.Quit 
     End If 
    End With 
    Set objExcel = CreateObject("Excel.Application") 

    objExcel.Visible = False 
    objExcel.DisplayAlerts = False 

    Set objWorkbook = objExcel.Workbooks.Open(WorkingDir & myFile) 

    With objWorkbook.Sheets(1) 
      .Range("D87", .Range("D87").End(-4121)).Copy 
      objWorkbook.Sheets.Add().paste 
      .Range("E87", .Range("E87").End(-4121)).Copy 
    End With 

    set sheet = objWorkbook.Sheets.Add() 
    sheet.paste 
    objWorkbook.SaveAs WorkingDir & SaveName, 23 
    objWorkbook.Saved = true 
    objWorkbook.Close 

    Set objWorkbook = Nothing 
    Set objExcel = Nothing 
    Set fso = Nothing 
    Set myFolder = Nothing 
End Sub 

call xlsToCsv() 

我得到第18行代碼800A000D與 '範圍' 一類型的衝突。
我該如何做這項工作?

回答

1

認爲您的一些代碼可能會丟失,如WorkingDir的聲明。 我已經明確地通過添加引用的範圍:隨着objWorkbook.Sheets(1)

Dim WorkingDir As String 

    WorkingDir = "C:\test.xlsx" 

    Dim fso, FileName, SaveName, myFile 
    Dim objExcel, objWorkbook 

    Set fso = CreateObject("Scripting.FilesystemObject") 
    Set myFile = fso.GetFile(WorkingDir) 

    Set objExcel = CreateObject("Excel.Application") 

    objExcel.Visible = False 
    objExcel.DisplayAlerts = False 

    'main operation 
    FileName = Left(myFile, InStrRev(myFile, ".")) 
    Set objWorkbook = objExcel.Workbooks.Open(myFile) 

    With objWorkbook.Sheets(1) 
     .Range("F6", .Range("F6").End(xlToRight)).Copy 
     .Range("C6", .Range("C6").End(xlToRight)).Copy 
    End With 

    Dim sheet: Set sheet = objWorkbook.Sheets.Add() 

    sheet.Paste 
    SaveName = FileName & "csv" 
    objWorkbook.SaveAs SaveName, 23 
    objWorkbook.Saved = True 
    objWorkbook.Close 

    Set objWorkbook = Nothing 
    Set objExcel = Nothing 
    Set fso = Nothing 
    ' Set myFolder = Nothing 
+0

當我保存它作爲.vbs錯誤在l ine 3發生:指示結束expectet – nolags

+0

您聲明您保存爲.csv,上面代碼中的第3行也只是一個聲明而不會失敗。在哪一行失敗的代碼是什麼? – QHarr

+0

Dim WorkingDir As String – nolags

2

2這邊的事情:

  • 每當你使用Range對象Range("F6").End(xlToRight),你需要提及它的類型/父對象。所以,在你的情況下,這應該被替換爲objWorkbook.Sheets(1).Range("F6").End(xlToRight)

  • VBScript不知道xlToRight的含義。所以,你必須使用xlToRight的值,它是-4161如下圖所示:

替換下面的代碼:

objWorkbook.Sheets(1).Range("F6", Range("F6").End(xlToRight)).Copy 
objWorkbook.Sheets(1).Range("C6", Range("C6").End(xlToRight)).Copy 

With objWorkbook.Sheets(1) 
    .Range("F6", .Range("F6").End(-4161)).Copy 
    .Range("C6", .Range("C6").End(-4161)).Copy 
End With 
+0

這給我水平F6這是線。我需要專欄! – nolags

+1

@nolags使用代碼-4121來獲取列。 – Gurman

+0

這個工程,但我沒有得到兩列,只有一個。 – nolags