VBA:使用变量调用宏

时间:2017-03-09 17:54:15

标签: excel excel-vba vba

我是VBA的初学者,我已经完成了一个脚本,可以根据分配给变量SheetName的工作表名称调用不同的宏。我正在尝试执行下面的代码,我得到一个编译错误。希望你们能帮助我!!

Sub ScrubeCareOutput()

Dim SheetName, Header, PolicyNumber As String
Dim CheckPoint As Integer

StartTime = Now()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Sheets("ConsolidatedData").Select
Range("P:P").Cut
Range("A1").Select
ActiveCell.EntireColumn.Insert
Range("A1").Select

'Deleting old sheet
Application.StatusBar = "Calculating Loop .."
Sheets("Reference").Select
Range("L2").Select

ActiveCell.Offset(1, 0).Select
SheetName = ActiveCell.Value

'Scrubbing Output
Do Until SheetName = ""

Application.StatusBar = "Scrubbing " & SheetName & " Output.."
Sheets(SheetName).Select
Range("a1").Select

If IsEmpty(Range("A2")) = False Then
Range("A2").Select
Header = ActiveCell.Value
End If

'Deleting Headers

Selection.AutoFilter Field:=1, Criteria1:=Header
ActiveSheet.AutoFilter.Range.Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
ActiveSheet.AutoFilterMode = False

Selection.AutoFilter Field:=1, Criteria1:=""
ActiveSheet.AutoFilter.Range.Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
ActiveSheet.AutoFilterMode = False

Selection.AutoFilter Field:=1, Criteria1:="©Copyright Nebo Systems, Inc."
ActiveSheet.AutoFilter.Range.Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
ActiveSheet.AutoFilterMode = False

Selection.AutoFilter Field:=1, Criteria1:="Powered by ECARE?"
ActiveSheet.AutoFilter.Range.Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
ActiveSheet.AutoFilterMode = False

Range("1:1").Delete

'Scrubbing Data
Call SheetName

'Creating fields
For i = 1 To 4
ActiveCell.EntireColumn.Insert
Next

Range("A1").Select
ActiveCell.Value = "Account Number"
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = "Mnemonic"
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = "Begin Date"
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = "End Date"

'Formulating data
ActiveCell.Offset(1, -3).Select
ActiveCell.Value = "=VLOOKUP(E2,ConsolidatedData!$A:$S,3,0)"
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = "=VLOOKUP(E2,ConsolidatedData!$A:$S,16,0)"
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = "=VLOOKUP(E2,ConsolidatedData!$A:$S,17,0)"
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = "=VLOOKUP(E2,ConsolidatedData!$A:$S,18,0)"

ActiveCell.Offset(0, 1).Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, -4).Select
Range(Selection, Selection.End(xlUp)).Select
Range(Selection, Selection.Offset(0, 3)).Select
Selection.FillDown
Selection.Copy
Selection.PasteSpecial xlPasteValues
Range("a1").Select

'Formatting data
Application.StatusBar = "Formatting " & SheetName & " Output.."
With ActiveSheet
.Cells.Font.Name = "Calibri"
.Cells.Font.Size = "10"
End With
Range("1:1").Select
Selection.Font.Bold = True
Range("A1").Select

'Save data
ActiveWorkbook.Saved = True
Sheets("Reference").Select

ActiveCell.Offset(1, 0).Select
SheetName = ActiveCell.Value
Else

Sheets("Reference").Select

ActiveCell.Offset(1, 0).Select
SheetName = ActiveCell.Value

End If
Loop

Sheets("UB92Monitor").Select

'Confirmation message
ActiveWorkbook.Save

EndTime = Format((Now() - StartTime), "HH:MM:SS")
Application.StatusBar = False
MsgBox "Data scrubbed successfully in " & EndTime, vbOKOnly, "Data Scrubbing Status"

End Sub

0 个答案:

没有答案