left() in vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I wanted to do the code below without using for-loop, but it did not work. Is it possible to do it without For loop?
Thank you so much.
Code:
Sub myleft()
    'For x = 1 To 10
    'Cells(x, 2) = Left(Cells(x, 1), 2)
    'Next x
    '''''''
    Range("b1:b10") = Left(Range("a1:a10"), 2)
End Sub
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
try this:

Code:
Sub foo()
Range("B1:B10").Formula2R1C1 = "=LEFT(RC[-1],2)"
End Sub
 
Upvote 0
Another way:
Code:
Sub WithoutLoop()
  With Range("A1:A10")
    .Offset(0, 1).Value = Evaluate(Replace("if(@="""","""",trim(replace(trim(@),3,len(@),"""")))", "@", .Address))
  End With

End Sub
 
Upvote 0
Another way (this puts the values in the cells directly rather than formulas referring back to the previous column)...
Code:
[table="width: 500"]
[tr]
	[td]Sub GetLeftTwoCharacters()
  Range("A1:A10").TextToColumns Range("B1"), xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(2, 9))
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Maybe...

Code:
Sub myleft()
    With Range("B1:B10")
        .Formula = "=Left(A1,2)"
        .Value = .Value
    End With
End Sub

M.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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