我的代码主要工作,但它需要一段时间的调试,所以我开始认为我的架构可能有缺陷XD 那么,我该如何设计这更好?抓取范围的架构
我有一组空白行分隔的数据。除了空行以外,还可以通过列C中的ID区分每个组。对于每个ID,我需要捕获B列中的各种数字。有时候这些号码仅以5
开头,有时以7
开头。我需要分别捕获5和7。
With projWS
With .Range("C1:C6000")
Set f = .Find(cc, LookIn:=xlValues, lookat:=xlPart)
End With
If Not f Is Nothing Then 'first occurence found
counter = 0
i = f.Row
Do
acct = .Cells(i, 2)
If (Len(projWS.Cells(i, 3)) < 1 Or Left(acct, 1) = "7") And done = False Then
acctStart = f.Row
acctRows = i - acctStart
Set acctRng = .Range(.Cells(acctStart, 2), .Cells(i - 1, 5))
Set amountRng = .Range(.Cells(acctStart, 7), .Cells(i - 1, 8))
done = True 'set flag to show range has been filled
End If
counter = counter + 1 'increment counter
i = i + 1 'move to next row
Loop Until Len(.Cells(i, 3)) < 1 'keep looping until blank row
End If
If counter - 1 > acctRows Then 'how we determine if there's a "7"
flag = True 'so we set flag to true
Set depreRng = Range(.Cells(acctStart + acctRows, 2), .Cells(i - 1, 8))
dep = depreRng.Value2 'store range into array
End If
End With
捕获后,我需要将它拖放到另一个工作表。这个工作表已经有一个7
内置块。因此,这是我用来放下7
范围的循环。 5
没有内置的块。
For r = 112 To 120
For k = 1 To UBound(dep())
If .Cells(r, 1).Value2 = Trim(dep(k, 1)) Then
Debug.Print .Cells(r, 1).Value2
.Cells(r, 6) = dep(k, 6)
.Cells(r, 7) = dep(k, 7)
Exit For
Else
.Cells(r, 6) = 0
.Cells(r, 7) = 0
End If
Next k
Next r
我已经调试过几个错误。目前的情况是,depreRng
正在打破,因为我的数学不好。我绊倒了它,而不是调试每个错误,我该如何构建这个更好的?
我设计它假设总是会有'7'。 if语句是有缺陷的......我想我需要一个'else' ... – findwindow
嗯,其实这个工作就是你想找到col B中的行,数字以5开头,从7开始分别为一个块中的保存块和另一个工作表的另一个块中的其他块,对吧?你打算在第二张工作表中复制什么,整个行是它的一部分?那么不同区块中的相同数字(col C中的不同值)呢? col B中的数字始终是4位数字,或者也可能是51或7834924之类的数字? – EttoreP
我听起来太复杂了。我需要捕捉一个范围。有时我需要将这个范围划分为以5开头和7开头的范围。编辑:范围从B列到H的范围。编辑2:实际范围无关紧要。我正在寻找建筑。 Edit3:基本上,我该如何确定'5'的开始,'5'的结束,以及如果适用,'7'的开始和'7'的结束? – findwindow