Replacing one value to multiple values in rotations

Rashidz

New Member
Joined
Feb 8, 2018
Messages
9
Hi I would like to replace one value with multiple values in rotation,

In sheet 1, I have a value "DATA 10" in column A, row 2, 5, 7, 10, 15, 20, 23 and so on.

How can i replace this value (DATA 10) with multiple values (10 LADY, 10 CHILDREN, 10 MAN and 10 HAZMAT) in rotation, so that

row 2 will be replace to 10 LADY
row 5 = 10 CHILDREN
row 7 = 10 MAN
row 10 = 10 HAZMAT
row 15 = 10 LADY
row 20 = 10 CHILDREN
row 23 = 10 MAN
and so on in rotation


Thank you in advance,

Rashidz
 
This will reverse the changes
Code:
Sub ReverseChange()
   Dim Ary10 As Variant
   Dim Cnt As Long
   
   Ary10 = Array("10 LADY", "10 CHILDREN", "10 MAN", "10 HAZMAT")
   For Cnt = 0 To UBound(Ary10)
      Columns(1).Replace Ary10(Cnt), "DATA 10", , , False, , False, False
   Next Cnt

End Sub
The code you supplied in post#8 works for me. Which line gave the error?

Ary10 = Array("30 STORY MATTER", "30 CLIMATE CHANGE", "30 INSPIRATIONAL", "30 PERSPECTIVE")
Qty = WorksheetFunction.CountIf(Columns(1), "DATA 30")
For Cnt = 1 To Qty
Set Fnd = Columns(1).Find("DATA 30", , , xlWhole, , , False, , False)
If Not Fnd Is Nothing Then
Fnd.Value = Ary10(i)
i = i + 1
If i > UBound(Ary10) Then i = 0
End If
Next Cnt



This line
Fnd.Value = Ary10(i)
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try adding the line in red
Code:
Sub replaceData()

   Dim Ary10 As Variant
   Dim Cnt As Long
   Dim Qty As Long
   Dim Fnd As Range
   Dim i As Long
   
   Ary10 = Array("10 LADY", "10 CHILDREN", "10 MAN", "10 HAZMAT")
   Qty = WorksheetFunction.CountIf(Columns(1), "DATA 10")
   For Cnt = 1 To Qty
      Set Fnd = Columns(1).Find("DATA 10", , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Fnd.Value = Ary10(i)
         i = i + 1
         If i > UBound(Ary10) Then i = 0
      End If
   Next Cnt
   
   [COLOR=#ff0000]i = 0[/COLOR]
   Ary10 = Array("30 STORY MATTER", "30 CLIMATE CHANGE", "30 INSPIRATIONAL", "30 PERSPECTIVE")
   Qty = WorksheetFunction.CountIf(Columns(1), "DATA 30")
   For Cnt = 1 To Qty
      Set Fnd = Columns(1).Find("DATA 30", , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Fnd.Value = Ary10(i)
         i = i + 1
         If i > UBound(Ary10) Then i = 0
      End If
   Next Cnt

End Sub
 
Upvote 0
put
Code:
i=0
just before each line starting with
Code:
Ary10=Array
 
Upvote 0
Try adding the line in red
Code:
Sub replaceData()

   Dim Ary10 As Variant
   Dim Cnt As Long
   Dim Qty As Long
   Dim Fnd As Range
   Dim i As Long
   
   Ary10 = Array("10 LADY", "10 CHILDREN", "10 MAN", "10 HAZMAT")
   Qty = WorksheetFunction.CountIf(Columns(1), "DATA 10")
   For Cnt = 1 To Qty
      Set Fnd = Columns(1).Find("DATA 10", , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Fnd.Value = Ary10(i)
         i = i + 1
         If i > UBound(Ary10) Then i = 0
      End If
   Next Cnt
   
   [COLOR=#ff0000]i = 0[/COLOR]
   Ary10 = Array("30 STORY MATTER", "30 CLIMATE CHANGE", "30 INSPIRATIONAL", "30 PERSPECTIVE")
   Qty = WorksheetFunction.CountIf(Columns(1), "DATA 30")
   For Cnt = 1 To Qty
      Set Fnd = Columns(1).Find("DATA 30", , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Fnd.Value = Ary10(i)
         i = i + 1
         If i > UBound(Ary10) Then i = 0
      End If
   Next Cnt

End Sub

Oh great it solve the problem.



Just another question.

How do I reverse this formula, from "10 LADY", "10 CHILDREN", "10 MAN", "10 HAZMAT" to "DATA 10".

Thanks
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
To reverse with formula, you can use this in C2 and copy down:


=IFERROR("DATA_"&CHOOSE(COUNTIF(DATA_10,B2)+COUNTIF(DATA_20,B2)*2+COUNTIF(DATA_30,B2)*3+1,1/0,10,20,30),"")

Markmzz
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top