是否可以通过VBA查找和选择第二个变量范围?

时间:2019-05-13 02:13:16

标签: excel vba

您好,我有一个自动生成的报告,我想创建一个宏,将报告格式化为可以用作网络驱动器数据库的格式。该报告生成2个表,我想简化为1个表。由于第二张表的行数每天都在变化,因此第二张表永远不在完全相同的位置时,如何找到第二张表的范围。 如果有帮助,则第二个表中有一个唯一的标题为“ Potential Locations”,它是第二个表的最后一行。

我不确定如何从此代码开始。

我基本上想运行宏以找到第二张表并选择它,删除标题,然后将其向上移2行,向右移2列。

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,我终于到了那里,请参阅下面的代码了解如何解决我的问题。

Sub FORMAT_CUSTOMER_ORDER_REPORT()
'
' FORMAT_CUSTOMER_ORDER_REPORT Macro
' RE-ORGANISES CUSTOMER ORDER CLEANUP REPORT AND SAVES TO SHARED DRIVE IN FORRECT FILE FORMAT
'

    'Code below adjusts column widths and deleted first row

    Columns("A:A").ColumnWidth = 5.14
    Columns("C:C").ColumnWidth = 10
    Columns("D:D").ColumnWidth = 10.86
    Columns("E:E").ColumnWidth = 4.14
    Columns("G:G").ColumnWidth = 43.43
    Columns("H:H").ColumnWidth = 5.14
    Columns("I:I").ColumnWidth = 3.43


    'Deletes top row.

    Rows("1:1").Select
    Selection.Delete Shift:=xlUp


    'Code below finds and selects the cell with "POTENTIAL" in it.

    Cells.Find(What:="POTENTIAL", _
               After:=ActiveCell, _
               LookIn:=xlFormulas, _
               LookAt:=xlPart, _
               SearchOrder:=xlByRows, _
               SearchDirection:=xlNext, _
               MatchCase:=False, _
               SearchFormat:=False).Activate


    'Code below 'cuts' the 'current region'.
        ActiveCell.CurrentRegion.Cut


    'Code below pastes clipboard to the first blank cell in Column C.
        Range("C1").End(xlDown).Offset(1, 0).Select
        ActiveSheet.Paste


    'Code below selects "current region" which in this case is the entire sheet
        ActiveCell.CurrentRegion.Select

    'Code below finds and selects the cell with "POTENTIAL" in it.
        Cells.Find(What:="POTENTIAL", _
          After:=ActiveCell, _
          LookIn:=xlFormulas, _
          LookAt:=xlPart, _
          SearchOrder:=xlByRows, _
          SearchDirection:=xlNext, _
          MatchCase:=False, _
          SearchFormat:=False).Activate

    'Code below select entire row of currently selected cell.
        ActiveCell.EntireRow.Select

    'Deletes selection
        Selection.Delete Shift:=xlUp

    'Selects 'current region'.
        ActiveCell.CurrentRegion.Select

        'Code below freezes first row
        Rows("1:1").Select
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
    Cells.Select
    Sheet1.Sort.SortFields.Clear
    Sheet1.Sort.SortFields.Add2 Key:= _
        Range("B1:B2931"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With Sheet1.Sort
        .SetRange Range("A1:N10000")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveWindow.SmallScroll Down:=-6


    'Code below Sorts entire sheet by Column B, A - Z.
        Sheet1.Sort.SortFields.Clear
        Sheet1.Sort.SortFields.Add2 Key:= _
        Range("B1:B10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With Sheet1.Sort
        .SetRange Range("A1:N10000")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


    'Code Below Saves File to network shared drive with the correct name
        ActiveWorkbook.SaveAs Filename:= _
        "R:\6024 Onsite\ONSITE CUSTOMER ORDERS\6024 CUSTOMER REPORT.xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False


End Sub