Is there any way to change range's values more efficiently than looping?

vonguyenphu

New Member
Joined
May 26, 2019
Messages
29
I have a range of cells with values inside. The values have pattern such as '123-abc'. Now I need to change the value of selection to just 123.
I currently use left function to loop over the selection. But it's very slow because I have 1000 cells.
 
Could also do with the Replace method :
Code:
[A:A].Replace what:="-*", replacement:="", lookat:=xlPart
Or :
Code:
[A:A].Replace "-*", "", xlPart
 
Last edited:
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Thank you Mr Rick,

I often work with such problems and find your usage of evaluate function fantastic so I really want to learn how it works.
I have the same problem with another issue. I want to split cell into 2 cells by seperator "-". Can you help me with the same evaluate function?
eg:
$A1="abcd-1" => $A1="abcd" and $B1="1"
$A2="xyz-123" => $A2="xyz" and $B2="123"
 
Upvote 0
First off, if you have not seen it yet, I would direct you to footoo's solution (Message #11 ) for your original problem as it is much better than the Evaluate method Jaafar gave you even with the addition I provided.

As to the question you raised in Message #12 ... I would not use Evaluate for that, rather, I would use the TextToColumns method instead.
Code:
Sub SplitOnDash()
  Columns("A").TextToColumns Range("A1"), xlDelimited, , , False, False, False, False, True, "-"
End Sub
 
Upvote 0
Hi Rick, Well spotted !

I didn't think about possible empty entries.

@vonguyenphu
Do you know any guide or tutorial for Evaluate function, I already search online but didn't find a good one.
I always search Rick Rothstein's posts when I have vba formulaes\string manipulations issues. I would resommend that you look at his valuable contributions on the subject in this forum. I am sure you will learn alot.

As for footoo's soultion, it is obviously simpler and most likely quicker. Thank you.

Regards.
 
Upvote 0
I always search Rick Rothstein's posts when I have vba formulas\string manipulations issues.
You do? :eek: I am honored... and speechless at the thought! (y)



I would recommend that you look at his valuable contributions on the subject in this forum. I am sure you will learn a lot.
Thank you so much for your kind words above and for this recommendation... you are far too kind. :oops:
 
Upvote 0
String functions do not natively process arrays, so it must be "induced". If there are no blanks within your range, these (from Jaafar's code in Message #4 ...
Code:
Range("A1:A10") = Evaluate("[B][COLOR="#FF0000"]IF({1},[/COLOR][/B]LEFT(" & Range("A1:A10").Address & ",3)[B][COLOR="#FF0000"])[/COLOR][/B]")

Code:
Sub LeftWithoutLoop(ByVal R As Range, ByVal Length As Long)
    R = Evaluate("[B][COLOR="#FF0000"]IF({1},[/COLOR][/B]LEFT(" & R.Address & "," & Length & ")[B][COLOR="#FF0000"])[/COLOR][/B]")
End Sub

If, on the other hand, there could be blanks within your data, we need to test for them or else they will become zeros...
Code:
Range("A1:A10") = Evaluate("[B][COLOR="#FF0000"]IF(" & Range("A1:A10").Address & "="""","""",[/COLOR][/B]LEFT(" & Range("A1:A10").Address & ",3)[B][COLOR="#FF0000"])[/COLOR][/B]")

Code:
Sub LeftWithoutLoop(ByVal R As Range, ByVal Length As Long)
    R = Evaluate([B][COLOR="#FF0000"]Replace([/COLOR][/B]"[B][COLOR="#FF0000"]IF(@="""","""",[/COLOR][/B]LEFT([B][COLOR="#FF0000"]@[/COLOR][/B]," & Length & "))"[B][COLOR="#FF0000"], "@", R.Address)[/COLOR][/B])
End Sub

Note: The above is untested, but I think I got it correct.

Hi Rick,

I just recently wanted to reuse your code but now it threw syntax error. Can you help me?
VBA Code:
Compile Error: list seperator or )
 
Upvote 0
String functions do not natively process arrays, so it must be "induced". If there are no blanks within your range, these (from Jaafar's code in Message #4 ...
Code:
Range("A1:A10") = Evaluate("[B][COLOR="#FF0000"]IF({1},[/COLOR][/B]LEFT(" & Range("A1:A10").Address & ",3)[B][COLOR="#FF0000"])[/COLOR][/B]")

Code:
Sub LeftWithoutLoop(ByVal R As Range, ByVal Length As Long)
    R = Evaluate("[B][COLOR="#FF0000"]IF({1},[/COLOR][/B]LEFT(" & R.Address & "," & Length & ")[B][COLOR="#FF0000"])[/COLOR][/B]")
End Sub

If, on the other hand, there could be blanks within your data, we need to test for them or else they will become zeros...
Code:
Range("A1:A10") = Evaluate("[B][COLOR="#FF0000"]IF(" & Range("A1:A10").Address & "="""","""",[/COLOR][/B]LEFT(" & Range("A1:A10").Address & ",3)[B][COLOR="#FF0000"])[/COLOR][/B]")

Code:
Sub LeftWithoutLoop(ByVal R As Range, ByVal Length As Long)
    R = Evaluate([B][COLOR="#FF0000"]Replace([/COLOR][/B]"[B][COLOR="#FF0000"]IF(@="""","""",[/COLOR][/B]LEFT([B][COLOR="#FF0000"]@[/COLOR][/B]," & Length & "))"[B][COLOR="#FF0000"], "@", R.Address)[/COLOR][/B])
End Sub

Note: The above is untested, but I think I got it correct.

Hi Rick,

I just recently wanted to reuse your code but now it threw syntax error. Can you help me?
VBA Code:
Compile Error: list seperator or )
That code was posted before the forum changed to its new format. I notice that the color coding I used in those code module is there in HTML format, so if you copied what you have quoted above, it won't work. Here are the code modules with the color coding implemented so that you can copy/paste directly from the code shown below.
Rich (BB code):
Range("A1:A10") = Evaluate("IF({1},LEFT(" & Range("A1:A10").Address & ",3))")

Rich (BB code):
Sub LeftWithoutLoop(ByVal R As Range, ByVal Length As Long)
    R = Evaluate("IF({1},LEFT(" & R.Address & "," & Length & "))")
End Sub

If, on the other hand, there could be blanks within your data, we need to test for them or else they will become zeros...
Rich (BB code):
Range("A1:A10") = Evaluate("IF(" & Range("A1:A10").Address & "="""","""",LEFT(" & Range("A1:A10").Address & ",3))")

Rich (BB code):
Sub LeftWithoutLoop(ByVal R As Range, ByVal Length As Long)
    R = Evaluate(Replace("IF(@="""","""",LEFT(@," & Length & "))", "@", R.Address))
End Sub
 
Upvote 0
That code was posted before the forum changed to its new format. I notice that the color coding I used in those code module is there in HTML format, so if you copied what you have quoted above, it won't work. Here are the code modules with the color coding implemented so that you can copy/paste directly from the code shown below.
Rich (BB code):
Range("A1:A10") = Evaluate("IF({1},LEFT(" & Range("A1:A10").Address & ",3))")

Rich (BB code):
Sub LeftWithoutLoop(ByVal R As Range, ByVal Length As Long)
    R = Evaluate("IF({1},LEFT(" & R.Address & "," & Length & "))")
End Sub

If, on the other hand, there could be blanks within your data, we need to test for them or else they will become zeros...
Rich (BB code):
Range("A1:A10") = Evaluate("IF(" & Range("A1:A10").Address & "="""","""",LEFT(" & Range("A1:A10").Address & ",3))")

Rich (BB code):
Sub LeftWithoutLoop(ByVal R As Range, ByVal Length As Long)
    R = Evaluate(Replace("IF(@="""","""",LEFT(@," & Length & "))", "@", R.Address))
End Sub

Thank you for your quick reply. Now after looking back your code, I understand a little more.
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,049
Latest member
THMarana

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