我的代碼主要工作,但它需要一段時間的調試,所以我開始認爲我的架構可能有缺陷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