对于不同范围内的循环

时间:2014-07-29 15:11:10

标签: excel-vba vba excel

我希望从excel中的信息流生成数据库(从PDF文件转换)。在这里得到一些帮助之后,已经定义了需要搜索的各个范围,现在我正在尝试扫描这些单独的范围以获取特定的触发器。然后,目的是提取数据,这取决于数据中存在哪些触发因素。

我目前正在尝试创建一个For循环来扫描所有不同的范围并提取所寻求的信息,但我想与For Loop一起解开。

我的数据采用以下格式(excel总共约900行)

Biodiesel  
Loss Number:  
88840  
Three people were killed and five injured in a fire that broke out in a biodiesel plant. People living eight km away reported hearing an explosion. A building collapsed on the victims of the incident.  
Event Date:  
22/11/2008  
Country:  
Turkey  
Location:  
Antakya, southern province of Hatay  
Event:  
Explosion, fire  
Cause:  
Explosion  
Plant Status:  
Operating  
Fatalities:  
3  
Injuries:    
5

问题是数据不遵循相同的格式,因此我想根据“丢失编号:”拆分成范围,然后扫描每个范围大约10个可能的标题。但是,我不确定如何扫描各个范围?

任何帮助都将不胜感激。

我目前的代码是;

Sub RangePrintTest()


Dim pd(100000, 100)
Dim rpd(10000, 100)
Dim VirtMat(300, 100)
Dim c As Integer
Dim npd As Variant


With ThisWorkbook.Sheets("PDF Input Sheet")


Dim Rng As Range
Dim Dn As Range
Dim Temp As String
Dim k
Dim cc As Long


Set Rng = Range(Range("A2"), Range("A" & Rows.count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare

For Each Dn In Rng
If Not .Exists(Dn.Value & Dn.Offset(1).Value) And Dn.Value = "Loss Number:" Then
.Add Dn.Value & Dn.Offset(1).Value, Dn
Temp = Dn.Value & Dn.Offset(1).Value
Else
Set .Item(Temp) = Union(.Item(Temp), Dn)
End If
Next

Dim p
For Each k In .keys
    cc = cc + 1
    VirtMat(cc, 1) = .Item(k).Address
Next k
End With


End With


Dim i As Integer
i = 1

For i = 1 To 41
X = VirtMat(i, 1).Value
i = i + 1
Set TestRange = Range(X)
Do Until R = 21
If TestRange(R, 1) = "Loss Number:" Then
If TestRange(R + 1, 1) <> "" Then LossNumber = TestRange(R + 1, 1)

If TestRange(R, 1) <> "Loss Number:" Then LossNumber = NoInfo

cr = cr + 1

rpd(cr, 1) = LossNumber


End If

R = R + 1

Loop
Next

End Sub

1 个答案:

答案 0 :(得分:0)

你有:

For i = 1 To 41
X = VirtMat(i, 1).Value
i = i + 1

// other stuff

Next

这种For ... Next循环在这种情况下自动递增循环变量 - i - 在每次循环之后。您还在代码中手动递增i,因此会错过VirtMat中一半的条目。您将阅读条目1,错过条目2,阅读条目3,依此类推。

尝试删除i = i + 1行,看看是否有帮助。 For ... Next循环的更多信息为here