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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
7,090
Office Version
  1. 365
Platform
  1. Windows
try this:

Code:
Sub foo()
Range("B1:B10").Formula2R1C1 = "=LEFT(RC[-1],2)"
End Sub
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,935
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,020
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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:

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
17,085
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Maybe...

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

M.
 

Forum statistics

Threads
1,170,960
Messages
5,872,997
Members
432,955
Latest member
minhnhat2504

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
Top