View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
AltaEgo AltaEgo is offline
external usenet poster
 
Posts: 245
Default After checked condition, add the data to adjacent cell automatically

List your columns in an array. Call the other (slightly modified code) for
each element of the array.

Sub Test()
Dim i, ArrCols

ArrCols = Array("D", "F", "H", "J") 'etc
For i = LBound(ArrCols) To UBound(ArrCols)

Call LenCheck(ArrCols(i))

Next i

End Sub

Sub LenCheck(Col)
dim c,x
Range(Col & "2").Select
Range(Selection, Selection.End(xlDown)).Select

For Each c In Selection

x = Len(c)

Select Case x

Case Is < 10
rtn = 5
Case Is < 20
rtn = 4
Case Is < 30
rtn = 3
Case Is < 40
rtn = 2
Case Is < 50
rtn = 1
Case Else
rtn = 0

End Select

c.Offset(, -1) = rtn

Next c
End Sub


Depending how many you need to do, selecting and looping each cell may be
slow.

--
Steve

"tlee" wrote in message
...
Hi AltaEgo,

Thanks for your help. You give me the idea how to implement the macro.

Besides, anyone know how to selected the specified column pattern for
checking?

Such that, check column (B, D, F, H, J, L,........) in the worksheet.

I tried to find information about it. It is more likely to use
"range.Offset( )" to implement.

Am I correct? how can I loop it until checked all specified columns.

Thanks for your in advance.

Best regards,
Tom Lee



Sorry. I just noticed that you want to automate the process with code
rather than use a function.

Sub Test()
' Select column D cells
' Results show in Column C
For Each c In Selection

x = Len(c)

Select Case x

Case Is < 10
rtn = 5
Case Is < 20
rtn = 4
Case Is < 30
rtn = 3
Case Is < 40
rtn = 2
Case Is < 50
rtn = 1
Case Else
rtn = 0

End Select

c.Offset(, -1) = rtn

Next c
End Sub

--
Steve

"tlee" wrote in message
...
Hi all,

Could anyone help me how to check condition, and fill the data to the
adjacent cell automatically?

Such that, there are several columns at the worksheet.

And I specified to use 2 columns (C and D) only.

Column D contains characters in each cell.
Column C is blank Column without data in all cell.

It checks the number of the characters in each cell of column D.
If the number of characters is 10 in D2, then fill "5" into C2
If the number of characters is 20 in D3, then fill "4" into C3

How can I implement that operation?

Thanks for your in advance.

TLee