我有一个由unix程序导出的大型CSV文件(这是一个与我在5月29日询问的文件不同的文件)。它包含几个垂直堆叠的大数据组,并由一行间隙分开(即A1:J4098是Sensor1,A4100:J8197是Sensor2等)。每组的第一行是传感器的名称。我正在尝试编写一个宏来搜索CSV以查找特定的传感器名称,创建指向该组的命名范围,然后自动过滤它(注意:传感器,参数和Parm在我们的办公室中是同义词)。
以下是我正在使用的代码:
Set sData = Activeworkbook.Sheets(1)
Set Generator = ThisWorkbook
Set Dashboard = Generator.Sheets("Dashboard")
'Set range for .Find
Set rSensor = sData.Range("J1000000")
Set rSensor = Range("A18", rSensor.End(xlUp))
'Check if tab already exists and create it if not
For parm = 3 To 23
If IsEmpty(Dashboard.Cells(parm, 1)) Then Exit For
Sensor = Dashboard.Cells(parm, 1)
Exist = False
For Each ws In Generator.Worksheets
If Sensor = ws.Name Then
Exist = True
Set NewWS = Generator.Sheets(ws.Name)
Exit For
End If
Next ws
If Exist = False Then
Generator.Worksheets("Fband").Copy Before:=Generator.Sheets("Scaled Data")
Set NewWS = Generator.Worksheets("Fband (2)")
NewWS.Name = Sensor
End If
'Find sensor name in data; set range if found, msgbox if not
' ! Always goes to first sensor in raw data
With rSensor
Set c = .Find(Sensor, , xlValues)
If c Is Nothing Then
MsgBox ("Parameter Not Found:" & Sensor)
End
Else
Set rData = sData.Cells(c.Row + 1, 1)
Set rData = Range(rData, rData.End(xlDown))
Set rData = Range(rData, rData.End(xlToRight))
'Pull peak amplitude for each frequency band
For Band = 1 To 136
rData.AutoFilter Field:=1, Criteria1:=">" & NewWS.Range("A1").Offset(Band, 1), _
Criteria2:="<" & NewWS.Range("A1").Offset(Band, 3)
sData.AutoFilter.Sort.SortFields.Clear
sData.AutoFilter.Sort.SortFields.Add Key:=Range("B:B"), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
With sData.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Replace existing data if greater
Set rNew = sData.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2)
If NewWS.Range("A1").Offset(Band, 4).value < rNew.value Then
rNew.Copy
NewWS.Range("A1").Offset(Band, 4).PasteSpecial xlPasteValues
End If
rData.AutoFilter Field:=1
Next Band
End If
End With
问题是宏返回列表中每个传感器的同一组数据(恰好是第一组)[在Dashboard.Range(“A3:A23”)]中找到。我假设Find函数正在工作,因为我从未得到“参数未找到”消息框,但它每次都将Autofilter放在同一组数据上,而不是紧跟传感器名称后的数据组。