在Excel VBA代码上,一个宏单独运行时有效,但是在成组运行时调试

时间:2018-12-20 22:18:17

标签: excel vba excel-vba

我的程序通过调用许多宏来工作,例如:

Sub RunAll()
Call BenNaimGal
Call NunnariMaureen
Call FarberovDimitry

End Sub

我的程序在macro3处中断调用FarberovDimitry,它本质上是macro1和macro2的精确副本,但工作正常。单独运行macro3时,它可以完美运行,但是当我尝试运行整个程序时,缺少某些行,

Rng4.Copy Destination:=WS.Range("A5:AJ50")

A5:AJ50为空

下面是这3个宏的代码

Sub BenNaimGal()
Dim WS As Worksheet
Dim Rng As Range
Set Rng = Range("A1:AJ14")
Application.Workbooks.Add
Set WS = Application.ActiveSheet
Rng.Copy Destination:=WS.Range("A1:AJ20")
Rows(3).RowHeight = 36
Rows(4).RowHeight = 64.5
Rows("6:50").RowHeight = 15
Columns(11).ColumnWidth = 11.57
Columns(12).ColumnWidth = 30.43
Columns(14).ColumnWidth = 15.57
Columns(22).ColumnWidth = 14
Columns(24).ColumnWidth = 13.71
Columns(25).ColumnWidth = 13.71

Set column1 = Columns("A:J")
column1.Hidden = True
Set column2 = Columns("R:T")
column2.Hidden = True
Set column3 = Columns("M:M")
column3.Hidden = True
Set column4 = Columns("U:U")
column4.Hidden = True
Set column5 = Columns("W:W")
column5.Hidden = True
Set column6 = Columns("Z:AJ")
column6.Hidden = True
[N1].Value = "Gal"
[O1].Value = "Ben-Naim"
[N2].Value = "CA . PBI. 3410"

WS.SaveAs "L:\17_Year_End_KPIs\2018\Master KIP 2018 v1 - Distribution\Ben- 
Naim, Gal"

End Sub

Sub NunnariMaureen()
Dim WS As Worksheet
Dim Rng As Range
Set Rng1 = Range("A1:AJ4")
Set Rng2 = Range("A15:AJ25")
Application.Workbooks.Add
Set WS = Application.ActiveSheet
Rng1.Copy Destination:=WS.Range("A1:AJ4")
Rng2.Copy Destination:=WS.Range("A5:AJ50")
Rows(3).RowHeight = 36
Rows(4).RowHeight = 64.5
Rows("6:50").RowHeight = 15
Columns(11).ColumnWidth = 11.57
Columns(12).ColumnWidth = 30.43
Columns(14).ColumnWidth = 15.57
Columns(22).ColumnWidth = 14
Columns(24).ColumnWidth = 13.71
Columns(25).ColumnWidth = 13.71


Set column1 = Columns("A:J")
column1.Hidden = True
Set column2 = Columns("R:T")
column2.Hidden = True
Set column3 = Columns("M:M")
column3.Hidden = True
Set column4 = Columns("U:U")
column4.Hidden = True
Set column5 = Columns("W:W")
column5.Hidden = True
Set column6 = Columns("Z:AJ")
column6.Hidden = True
[N1].Value = "Nunnari"
[O1].Value = "Maureen"
[N2].Value = "CA . C&I. 3420"

WS.SaveAs "L:\17_Year_End_KPIs\2018\Master KIP 2018 v1 - 
Distribution\Nunnari, Maureen"

End Sub

Sub FarberovDimitry()
Dim WS As Worksheet
Dim Rng As Range
Set Rng3 = Range("A1:AJ4")
Set Rng4 = Range("A26:AJ33")
Application.Workbooks.Add
Set WS = Application.ActiveSheet
Rng3.Copy Destination:=WS.Range("A1:AJ4")
Rng4.Copy Destination:=WS.Range("A5:AJ50")
Rows(3).RowHeight = 36
Rows(4).RowHeight = 64.5
Rows("6:50").RowHeight = 15
Columns(11).ColumnWidth = 11.57
Columns(12).ColumnWidth = 30.43
Columns(14).ColumnWidth = 15.57
Columns(22).ColumnWidth = 14
Columns(24).ColumnWidth = 13.71
Columns(25).ColumnWidth = 13.71


Set column1 = Columns("A:J")
column1.Hidden = True
Set column2 = Columns("R:T")
column2.Hidden = True
Set column3 = Columns("M:M")
column3.Hidden = True
Set column4 = Columns("U:U")
column4.Hidden = True
Set column5 = Columns("W:W")
column5.Hidden = True
Set column6 = Columns("Z:AJ")
column6.Hidden = True
[N1].Value = "Dimitry"
[O1].Value = "Farberov"
[N2].Value = "CA . PBI. 3409"

WS.SaveAs "L:\17_Year_End_KPIs\2018\Master KIP 2018 v1 - 
Distribution\Farberov, Dimitry"

End Sub

1 个答案:

答案 0 :(得分:0)

最好指定一块ng。

Dim WS As Worksheet
Dim Rng As Range
Dim myWs As Worksheet

Set myWs = ThisWorkbook.Sheets(1)
Set Rng = myWs.Range("A1:AJ14")