[英]VBA change a activecell offset on multiple criteria
首先,多年來我一直在這個網站上學到很多東西。 話雖這么說,但對於更復雜的代碼行,我還是很環保的。 我會盡我所能描述我要做什么,也許有人可以指出我正確的方向。
我在兩個系列中有兩個專欄:
Column A Column B
1 0
1 1
1 2
1 0
1 1
1 2
2 0
2 1
2 2
4 0
4 1
我想做的是搜索B列,直到找到A列序列的最后一個數字,然后將A列的數字增加一個。 所以我的最終結果應該是:
Column A Column B
1 0
1 1
1 2
2* 0
2* 1
2* 2
3 0
3 1
3 2
4 0
4 1
我試圖做類似的事情
Sub pdiddy()`
Do Until ActiveCell.Value = ""`
If ActiveCell.Value < 15 Then 'the column b sequence is only going to be between the numbers 0-15
ActiveCell.offest(1, 0).Value 1 'the column A sequence can increase until 99
End If
Loop
End Sub
如果數字序列在更改之前沒有超過15,我想將數字保留在A列中。 我希望我已經解釋清楚了。 我要確保沒有相同的A列和B列組合。 在此先感謝大家幫助新手了解和改進代碼序列。 我也嘗試對代碼進行標記,但是我不對不起,我很抱歉。 BSOV
這里的代碼可以滿足您的要求,而不是使上一個答案變得無法識別。 作為參考,我產生了輸出(與原始文件並排)。
編碼:
Sub pdiddy()
Dim bank, cue
Dim r As Range
Set r = Range("b2") ' point to the first cell in column C: cueWrap
bank = Range("A2").Value - 1 ' starting value for bank ... code adds one in the first pass
cue = r.Value ' first value for cue
Do Until r.Value = ""
r.Select
If r.Value < r.Offset(-1, 0) Or r.Value >= 15 Then ' must wrap
If r.Value > 15 Then cue = (r.Value Mod 15) Else cue = 0
If r.Offset(0, -1) = r.Offset(-1, -1) Or cue = 0 Then
bank = bank + 1
End If
Else
cue = r.Value
End If
If bank > 127 Then Exit Do ' 127 banks available
If r.Offset(0, -1).Value > bank Then bank = r.Offset(0, -1).Value
r.Offset(0, -1).Value = bank ' overwrite bank
r.Value = cue ' overwrite cue
Set r = r.Offset(1, 0) ' next cell down
Loop
End Sub
輸出:
---after--- --- before ----
Bank Cue Bank Cue
10 1 10 1
10 2 10 2
10 3 10 3
10 4 10 4
10 5 10 5
10 6 10 6
10 7 10 7
10 8 10 8
10 9 10 9
10 10 10 10
10 11 10 11
10 12 10 12
10 13 10 13
10 14 10 14
10 15 10 15
11 1 10 16
11 2 10 17
11 3 10 18
11 4 10 19
11 5 10 20
11 6 10 21
11 7 10 22
11 8 10 23
11 9 10 24
11 10 10 25
11 11 10 26
11 12 10 27
11 13 10 28
11 14 10 29
11 15 10 30
12 1 10 31
12 2 10 32
13 1 11 1
13 2 11 2
13 3 11 3
13 4 11 4
13 5 11 5
13 6 11 6
13 7 11 7
13 8 11 8
13 9 11 9
13 10 11 10
13 11 11 11
13 12 11 12
13 13 11 13
13 14 11 14
13 15 11 15
14 1 11 16
14 2 11 17
15 1 12 1
15 2 12 2
15 3 12 3
15 4 12 4
15 5 12 5
15 6 12 6
15 7 12 7
15 8 12 8
15 9 12 9
15 10 12 10
15 11 12 11
15 12 12 12
15 13 12 13
15 14 12 14
15 15 12 15
16 1 12 16
16 2 12 17
16 3 12 18
16 4 12 19
16 5 12 20
16 6 12 21
16 7 12 22
16 8 12 23
16 9 12 24
16 10 12 25
16 11 12 26
16 12 12 27
16 13 12 28
16 14 12 29
16 15 12 30
17 1 12 31
17 2 12 32
21 1 21 1
21 2 21 2
21 3 21 3
21 4 21 4
21 5 21 5
21 6 21 6
21 7 21 7
21 8 21 8
21 9 21 9
這個問題還不清楚。 我要回答一個我認為您想問的問題:“每當B中的序列“重置”時,我都希望將A列中的相應數字加1”。
Sub pdiddy()
Dim Aval, Bval
Dim r as Range
Set r = Range("B1") ' point to the first cell in column B
Aval = 1 ' starting value for A
Bval = r.Value
Do Until r.Value = "" ' <<<< edited
If r.Value < Bval Then ' sequence wraps
Aval = Aval + 1 ' increment A
if Aval > 99 Then Exit Do ' "column A sequence can increase until 99"
End If
r.offset(0, -1).Value = Aval ' set the value in the column to the left
Bval = r.Value ' <<<< added
set r = r.Offset(1,0) ' next cell down
Loop
End Sub
如果這樣做沒有達到您的預期目的,請在評論中進行說明。 特別是從您的問題尚不清楚,當某物(A,B?)達到15
的值時應采取什么措施。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.