没有步进时粘贴范围的麻烦

时间:2017-05-26 18:15:49

标签: excel vba range

我想要使用以下VBA代码清理5个客户文件。当我逐步完成它时,一切正常。然而;当我运行代码时,粘贴了错误的范围。我已经花了一些时间完全重写我的范围引用以避免使用。选择任何地方。它似乎没有帮助。有什么想法吗?

Sub convert_Customer()

Dim xl As Excel.Application
Dim wkbks As Workbooks
Dim wksht As Worksheet
Dim Rng As Range
Dim StartCell As Range
Dim Dir As String
Dim LastRow As Long
Dim LastColumn As Long

Application.CutCopyMode = True
Application.DisplayAlerts = False

Dir = "C:\Users\User\Documents\User\Customer\BI Input"

Excel.Workbooks.Open Dir & "\Customer.xlsx"
With Excel.Workbooks("Customer.xlsx").Sheets(1).Activate
    Set StartCell = Range("F15")
    LastRow = StartCell.SpecialCells(xlCellTypeLastCell).Row
    LastColumn = StartCell.SpecialCells(xlCellTypeLastCell).Column
    Set Rng = Range(StartCell, Cells(LastRow, LastColumn))
    Rng.Select
    Rng.Copy
End With

Application.Workbooks.Add

Set wksht = Application.ActiveSheet

wksht.Range("A1").PasteSpecial Paste:=xlPasteValues

wksht.Range("A:A,C:G").NumberFormat = "@"
wksht.Range("B:B,H:CD").NumberFormat = "0.00"

Excel.Application.ActiveWorkbook.SaveAs Dir & "/Customer.csv", xlCSV

Excel.Workbooks("Customer.csv").Close
Excel.Workbooks("Customer.xlsx").Close

Application.CutCopyMode = False
Application.DisplayAlerts = True

End Sub

0 个答案:

没有答案