需要 运行 基于列中值的宏
Need to run a macro based on values in a column
更具体地说,我需要查找列中的所有唯一值(这些值是一年中月份的数字表示)并使用返回的值创建一个弹出框,允许用户选择要使用哪个宏运行。例如,我将报表数据导入到包含 100 条记录的工作表中。在 B 列中,我们将看到代表 11 月和 12 月的 11 和 12。我想要做的是捕获这两个数字并使用它来允许用户 运行 十一月或十二月的代码。我添加了我在 11 月使用的代码作为示例。
Sub Extract_Sort_1511_November()
'
'
' This line renames the worksheet to "Extract"
Application.ScreenUpdating = False
ActiveSheet.Name = "Extract"
' This line autofits the columns C, D, O, and P
Range("C:C,D:D,O:O,P:P").Select
Range("P1").Activate
Selection.Columns.AutoFit
Range("A2").Select
' This unhides any hidden rows
Cells.Select
Selection.EntireRow.Hidden = False
Range("A2").Select
' Want to alter the code below to perform a query in column B to determine
' which months are listed from the import, then use the results in a dialog box to
' allow the user to choose which month to view
Dim LR As Long
For LR = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
If Range("B" & LR).Value <> "11" Then
Rows(LR).EntireRow.Hidden = True
End If
Next LR
With ActiveWorkbook.Worksheets("Extract").Sort
With .SortFields
.Clear
.Add Key:=Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
End With
.SetRange Range("A2:Z2000")
.Apply
End With
Cells.WrapText = False
Sheets("Extract").Range("A2").Select
Application.ScreenUpdating = True
End Sub
找到了一个更快的方法来实现这个问题的真正目标。更改了排序和隐藏部分的顺序。这现在产生了预期的结果。
Sub Extract_Sort_1511_November()
'
'
' This line renames the worksheet to "Extract"
Application.ScreenUpdating = False
ActiveSheet.Name = "Extract"
' This line autofits the columns C, D, O, and P
Range("C:C,D:D,O:O,P:P").Select
Range("P1").Activate
Selection.Columns.AutoFit
Range("A2").Select
' This unhides any hidden rows
Cells.Select
Selection.EntireRow.Hidden = False
Range("A2").Select
' Want to alter the code below to perform a query in column B to determine
' which months are listed from the import, then use the results in a dialog box to
' allow the user to choose which month to view
Dim LR As Long
With ActiveWorkbook.Worksheets("Extract").Sort
With .SortFields
.Clear
.Add Key:=Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
End With
.SetRange Range("A2:Z2000")
.Apply
End With
For LR = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
If Range("B" & LR).Value <> "11" Then
Rows(LR).EntireRow.Hidden = True
End If
Next LR
Cells.WrapText = False
Sheets("Extract").Range("A2").Select
Application.ScreenUpdating = True
End Sub
更具体地说,我需要查找列中的所有唯一值(这些值是一年中月份的数字表示)并使用返回的值创建一个弹出框,允许用户选择要使用哪个宏运行。例如,我将报表数据导入到包含 100 条记录的工作表中。在 B 列中,我们将看到代表 11 月和 12 月的 11 和 12。我想要做的是捕获这两个数字并使用它来允许用户 运行 十一月或十二月的代码。我添加了我在 11 月使用的代码作为示例。
Sub Extract_Sort_1511_November()
'
'
' This line renames the worksheet to "Extract"
Application.ScreenUpdating = False
ActiveSheet.Name = "Extract"
' This line autofits the columns C, D, O, and P
Range("C:C,D:D,O:O,P:P").Select
Range("P1").Activate
Selection.Columns.AutoFit
Range("A2").Select
' This unhides any hidden rows
Cells.Select
Selection.EntireRow.Hidden = False
Range("A2").Select
' Want to alter the code below to perform a query in column B to determine
' which months are listed from the import, then use the results in a dialog box to
' allow the user to choose which month to view
Dim LR As Long
For LR = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
If Range("B" & LR).Value <> "11" Then
Rows(LR).EntireRow.Hidden = True
End If
Next LR
With ActiveWorkbook.Worksheets("Extract").Sort
With .SortFields
.Clear
.Add Key:=Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
End With
.SetRange Range("A2:Z2000")
.Apply
End With
Cells.WrapText = False
Sheets("Extract").Range("A2").Select
Application.ScreenUpdating = True
End Sub
找到了一个更快的方法来实现这个问题的真正目标。更改了排序和隐藏部分的顺序。这现在产生了预期的结果。
Sub Extract_Sort_1511_November()
'
'
' This line renames the worksheet to "Extract"
Application.ScreenUpdating = False
ActiveSheet.Name = "Extract"
' This line autofits the columns C, D, O, and P
Range("C:C,D:D,O:O,P:P").Select
Range("P1").Activate
Selection.Columns.AutoFit
Range("A2").Select
' This unhides any hidden rows
Cells.Select
Selection.EntireRow.Hidden = False
Range("A2").Select
' Want to alter the code below to perform a query in column B to determine
' which months are listed from the import, then use the results in a dialog box to
' allow the user to choose which month to view
Dim LR As Long
With ActiveWorkbook.Worksheets("Extract").Sort
With .SortFields
.Clear
.Add Key:=Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
End With
.SetRange Range("A2:Z2000")
.Apply
End With
For LR = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
If Range("B" & LR).Value <> "11" Then
Rows(LR).EntireRow.Hidden = True
End If
Next LR
Cells.WrapText = False
Sheets("Extract").Range("A2").Select
Application.ScreenUpdating = True
End Sub