Re: [VBA ] 字段COPY

楼主: hsnu920 (0rz)   2013-10-17 03:38:09
※ 引述《LiLiHsien ()》之铭言:
: 'for(int i = 0 ;i < array_A的阵列大小 ; i++)
: For i = 1 To 25
: 'if(i!=0)
: If i <> 0 Then
: 'if(array_A[i] == NULL)
: If Cells(i, 1).Value = "" Then
: 'array_B[i] = Array_A[i-1]
: Cells(i, 2) = Cells(i - 1, 1)
: Else
: 'array_B[i] = Array_A[1]
: Cells(i, 2) = Cells(i, 1)
: End If
: Else
: 'array_B[i] = Array_A[1]
: Cells(i, 2) = Cells(i, 1)
: End If
: Next
: 以上是我更改大大提供的程式码
: 不过他在A列空值的状况只能印出两个
: 且不能达到我有两个同一类需求的时候
: A B
: 1 dog dog
: 2 cat cat1
: 3 cat1
: 4 cat cat2
: 5 cat2
: 6 bird bird
: 7 bird
: 8 bird
: 9 bird
: A的原有的 , 我想产生B列
: 若如果A的类别只有一种(dog) , 则B只会依序印出dog
: 但如果A的类别重复(cat有两个) , 则B会列出cat1,cat1
: 等碰到A列的另一个cat , B在继续印出cat2,cat2.....
: 不知这该如何表示
: 麻烦大大提供一些意见 谢谢 : )
A列空值只能印出两个 应该是因为笔误
改成
Cells(i, 2) = Cells(i - 1, 2)
抓B列上面一个值就可以
至于你的问题要判断A列是否有重复就比较复杂
我自己习惯 [也许有更快的做法]
会先弄出一个阵列
把有重复的项目存起来
然后填B列的时候 跟存在内存中的阵列比对 决定后面要不要加数字
另外还要开一个变量 用来决定后面要加的值是1 or 2 or 3
要达到你所有要求 整个写下来程式码并不短
以上拙见
======附录========
'**********读取A列重复项目为y()阵列************
m = 0
For i = 1 To 35
If Cells(i, 1).Value <> "" Then
If m > 0 Then
If y(m - 1) <> Cells(i, 1).Value Then
y(m - 1) = Cells(i, 1).Value
Else
y(m) = Cells(i, 1).Value
m = m + 1
End If
Else
y(m) = Cells(i, 1).Value
m = m + 1
End If
End If
Next i
'***********暂存变量归零**********
j = 0
r = 0
x = ""
'***********判断B列该填写的内容*********
For i = 1 To 35
If i = 1 Then '***B1格要拉出来另外写 因为如果A1空的 并不存在B0这个值***
If Cells(1, 1) <> "" Then
For n = 0 To m - 2
If Cells(1, 1).Value = y(n) Then
Cells(1, 2).Value = Cells(1, 1).Value + Str(1)
j = 1
x = Cells(1, 1).Value + Str(j)
ElseIf r = 0 Then
Cells(1, 2).Value = Cells(1, 1).Value
x = Cells(1, 1).Value
End If
r = 1
Next n
End If
Else '***处理B2之后***
r = 0
If Cells(i, 1).Value = "" Then '***A(i)格空白***
Cells(i, 2).Value = Cells(i-1, 2).Value
Else '***A(i)格有值***
For n = 0 To m - 2 '***比对A(i)与内存的y阵列***
If Cells(i, 1).Value = y(n) Then r = 1
Next n
If r = 1 Then '***A(i)存在于阵列中***
If Cells(i, 1).Value + Str(j) <> x Then j = 0
j = j + 1
Cells(i, 2).Value = Cells(i, 1).Value + Str(j)
x = Cells(i, 1).Value + Str(j)
Else '***A(i)不存在阵列中***
Cells(i, 2).Value = Cells(i, 1).Value
x = Cells(i, 1).Value
j = 0
End If
End If
End If
Next i
作者: LiLiHsien   0000-00-00 00:00:00
谢谢大大 后来想到解决方法了 感谢你们的热心帮忙
作者: timestoprun (忧郁米虫)   0000-00-00 00:00:00
推推推!!我都忙到忘记来这看了XDD

Links booklink

Contact Us: admin [ a t ] ucptt.com