Enter Data Macro

  • Thread starter Thread starter MCheru
  • Start date Start date
M

MCheru

I need help with a macro. Here is what I am trying to do. Search every cell
in Column B. When the first two numbers are 20 then 2ZZP will be entered to
the left of that cell in Column A. When the first two numbers are 30 then
3ZZP will be entered to the left of that cell in Column A. When the first
two numbers are 40 then 4ZZP will be entered to the left of that cell in
Column A. Can you help me with this?
 
Sub codeif()
lr = Cells(Rows.Count, "b").End(xlUp).Row
For Each c In Range("b2:b" & lr)
mv = Left(c, 2)
If mv = 20 Or mv = 30 Or mv = 40 Then
c.Offset(, -1) = Left(c, 1) & "ZZP"
End If
Next c
End Sub
 
This is a very helpful code. It does what I asked for help with. Thank you.

I have one more question now that I probably should've asked initally but it
slipped my mind. Sometimes the numbers change for instance.
20 could become ZZP5
30 could become ZZP1
40 could become ZZP3

How can I change the code to accomodate this change when it happens?
 
Thank you. Like the other code this works very well.

I mentioned to Francis just in case you might be able to help as well. I
have one more question now that I probably should've asked initally but it
slipped my mind. Sometimes the numbers change for instance.
20 could become ZZP5
30 could become ZZP1
40 could become ZZP3

How can I change the code to accomodate this change when it happens?
 
Based on what you say:

Sub codeif_1()
lr = Cells(Rows.Count, "b").End(xlUp).Row
For Each c In Range("b2:b" & lr)
mv = Left(c, 2)
If mv = 20 Or mv = 30 Or mv = 40 Then
Select Case mv
Case 20: y = 5
Case 30: y = 1
Case 40: y = 3
Case Else
End Select
c.Offset(, -1) = Left(c, 1) & "ZZP" & y
End If
Next c
End Sub
 
This is very good. Thank you.

Don Guillett said:
Based on what you say:

Sub codeif_1()
lr = Cells(Rows.Count, "b").End(xlUp).Row
For Each c In Range("b2:b" & lr)
mv = Left(c, 2)
If mv = 20 Or mv = 30 Or mv = 40 Then
Select Case mv
Case 20: y = 5
Case 30: y = 1
Case 40: y = 3
Case Else
End Select
c.Offset(, -1) = Left(c, 1) & "ZZP" & y
End If
Next c
End Sub
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top