找到第一行数据集

时间:2018-03-21 21:10:16

标签: excel-vba vba excel

我试图找到要过滤的数据集。范围由输入到InputBox的用户确定。我想搜索专栏" C"对于ww_from和ww_to。专栏" C"都是整数。当我使用下面的代码时,我无法获得" First"和"最后"加载(x,y)位置的变量。提前致谢。斯科特

Public Sub Create_Report()

   'Define range of report
    On Error Resume Next
    ww_from = Application.InputBox _
    ("From what is the workweek do you want to report?")

    ww_to = Application.InputBox _
    (prompt:="To what workweek do you want to report?")


'Select first row of data set locations
With ThisWorkbook.Worksheets("Sheet1")
ActiveWorkbook.Worksheets("Sheet1").Activate
Set ws = Worksheets("Sheet1")
Set First = .Column("C").Find(What:="ww_from")

      'Locate data set for report by row number
       k = First.Row
   End With

 'Select last row of data set locations
 With ThisWorkbook.Worksheets("Sheet1")
 Set ws = Worksheets("Sheet1")
  Set Last = .Column("C").Cells.Find(What:="ww_to")

End With
         'Case where only one ww is requested in report meaning ww_from = ww_to.
         If l = k Then l = k
         l = Last.Row

1 个答案:

答案 0 :(得分:0)

Public Sub Create_Report()


'Define range of report
Dim ww_from As Long
ww_from = Application.InputBox _
    (prompt:="From what is the workweek do you want to report?", Type:=1)

Dim ww_to As Long
ww_to = Application.InputBox _
    (prompt:="To what workweek do you want to report?", Type:=1)


'Select first row of data set locations
With ThisWorkbook.Worksheets("Sheet1").Range("C:C")
    Dim First As Range
    Set First = .Find(ww_from)
      'Locate data set for report by row number

    Dim k As Long
    If Not First Is Nothing Then k = First.Row

    Dim Last As Range
    Set Last = .Find(ww_to)
    Dim l As Long
    If Not Last Is Nothing Then
        l = Last.Row
    Else
        l = k
    End If
End With