宏开放旧工作簿

时间:2017-05-10 19:07:06

标签: excel-vba vba excel

我正在使用的工作簿是旧工作簿的更新副本。 当我在新工作簿中运行宏时,它似乎运行得很好但是它也会打开旧工作簿。我希望这不会发生。

  • 新工作簿名为 LOG.xlsm
  • 旧工作簿称为 Application Log-Macro.xlsm 。它与新工作簿位于同一文件夹中。
  • 我正在运行Excel 2016。

请帮帮我 - 我猜我需要更改代码以确保活动工作簿是LOG.xlsm ??

以下是代码:

Sub SortApps()
'
' SortApps Macro
Application.Goto Reference:="AppsTable"
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields. _
    Clear
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add _
    Key:=Range("AppsTable[NOTES]"), SortOn:=xlSortOnCellColor, Order:= _
    xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields _
    (1).SortOnValue
    .Pattern = xlPatternLinearGradient
    .Gradient.Degree = 0
    .Gradient.ColorStops.Clear
End With
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields _
    (1).SortOnValue.Gradient.ColorStops.Add(0)
    .Color = 16763391
    .TintAndShade = 0
End With
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields _
    (1).SortOnValue.Gradient.ColorStops.Add(1)
    .Color = 16738047
    .TintAndShade = 0
End With
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add _
    Key:=Range("AppsTable[NOTES]"), SortOn:=xlSortOnCellColor, Order:= _
    xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields _
    (2).SortOnValue
    .Pattern = xlPatternLinearGradient
    .Gradient.Degree = 180
    .Gradient.ColorStops.Clear
End With
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields _
    (2).SortOnValue.Gradient.ColorStops.Add(0)
    .Color = 3394611
    .TintAndShade = 0
End With
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields _
    (2).SortOnValue.Gradient.ColorStops.Add(1)
    .Color = 3407718
    .TintAndShade = 0
End With
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add( _
    Range("AppsTable[STAT]"), xlSortOnCellColor, xlAscending, , xlSortNormal). _
    SortOnValue.Color = RGB(204, 0, 102)
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add( _
    Range("AppsTable[STAT]"), xlSortOnCellColor, xlAscending, , xlSortNormal). _
    SortOnValue.Color = RGB(204, 153, 255)
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add _
    Key:=Range("AppsTable[T2]"), SortOn:=xlSortOnCellColor, Order:= _
    xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields _
    (5).SortOnValue
    .Pattern = xlPatternLinearGradient
    .Gradient.Degree = 270
    .Gradient.ColorStops.Clear
End With
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields _
    (5).SortOnValue.Gradient.ColorStops.Add(0)
    .Color = 14202006
    .TintAndShade = 0
End With
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields _
    (5).SortOnValue.Gradient.ColorStops.Add(1)
    .Color = 9592886
    .TintAndShade = 0
End With
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add( _
    Range("AppsTable[NOTES]"), xlSortOnCellColor, xlDescending, , xlSortNormal). _
    SortOnValue.Color = RGB(218, 238, 243)
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add( _
    Range("AppsTable[STAT]"), xlSortOnCellColor, xlAscending, , xlSortNormal). _
    SortOnValue.Color = RGB(242, 220, 219)
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add( _
    Range("AppsTable[RI DUE]"), xlSortOnFontColor, xlAscending, , xlSortNormal). _
    SortOnValue.Color = RGB(192, 0, 0)
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add( _
    Range("AppsTable[STAT]"), xlSortOnCellColor, xlAscending, , xlSortNormal). _
    SortOnValue.Color = RGB(247, 150, 70)
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add( _
    Range("AppsTable[STAT]"), xlSortOnCellColor, xlAscending, , xlSortNormal). _
    SortOnValue.Color = RGB(255, 235, 156)
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add _
    Key:=Range("AppsTable[APP DT]"), SortOn:=xlSortOnValues, Order:= _
    xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort.SortFields.Add _
    Key:=Range("AppsTable[SSN]"), SortOn:=xlSortOnValues, Order:=xlAscending _
    , DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Apps").ListObjects("AppsTable").Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Range("K9").Select
ActiveWindow.SmallScroll Down:=21
Range("AppsTable[NOTES]").Select
ActiveWindow.SmallScroll Down:=-39
Selection.Rows.AutoFit
Range("A9").Select
End Sub

提前感谢您的建议!我确定这很容易,但是它让我陷入了一个循环,因为(惊讶)我实际上并不知道我在做什么。 :“)

1 个答案:

答案 0 :(得分:0)

如果在创建新工作簿时从旧工作簿中复制了工作表,则可能会在新工作簿中引用旧工作簿。首先按照此处的说明排除这种可能性。 https://support.office.com/en-us/article/Find-links-external-references-in-a-workbook-fcbf4576-3aab-4029-ba25-54313a532ff1