使用 Find 和 Offset 创建动态范围

使用 Find 和 Offset 创建动态范围

我正在尝试构建一些代码,用于对不断变化的数据集中的数据进行排序、搜索和复制。我认为我需要定义一个变量,然后让代码找到该变量的坐标,然后继续,但这可能完全错误。我已经在代码中包含了我需要代码执行的操作的基本概念,但我不知道使它工作的语法。有什么想法吗?

子组织数据()

Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim X As Integer
Dim Function_Name As String

Replace_Blank

'******Next line could use some "robusting" by replacing hard coded value with a search for EE status**************
    A = ActiveWorkbook.Worksheets("Raw Data").Range("F2", Worksheets("Raw Data").Range("F2").End(xlDown)).Rows.Count
Worksheets.Add().Name = "Calculations"
Find_Unit
Find_Locations
    B = ActiveWorkbook.Worksheets("Calculations").Range("B3", Worksheets("Calculations").Range("B3").End(xlDown)).Rows.Count

    C = ActiveWorkbook.Worksheets("Calculations").Range("C3", Worksheets("Calculations").Range("C3").End(xlDown)).Rows.Count

For X = 1 To B
Worksheets.Add().Name = Sheets("Calculations").Range("B2").Offset(X, 0).Value
Next X

Delete_Temp_Sheets





 ActiveWorkbook.Worksheets("Raw Data").Sort.SortFields.Add Key:=Range( _
    "F2:F376"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
With ActiveWorkbook.Worksheets("Raw Data").Sort
    .SetRange Range("B2:V376")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
'*********Working code******************
Sheets("Raw Data").Select
Cells.Find(What:="EE status", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Calculations").Select
Range("B2").Select
ActiveSheet.Paste

答案1

您想在哪里找到“EE 状态”?只需将搜索范围限制在该位置,即可.Find返回范围

例如(未经测试)

Dim findIt as Range
Set findIt = Sheets("Raw Data").Cells.Find (...)
Dim lastRow as Long
lastRow = Cells(Rows.Count,findIt.Column).end(xlup).row
dim myRange as Range
Set myRange = Range(findIt, Cells(lastRow, findIt.Column))

相关内容