Left function for entire column

GirishDhruva

Active Member
Joined
Mar 26, 2019
Messages
308
Hi Everyone ,
Here i am trying to find the left-function of a particular column i,e 'A', and i did it for destination column, but i need that at last column were i would not be knowing the last column.

Sub Leftfunc()


Dim Sourcecolumn As Range
Dim Destinationcolumn As Range
Dim i As Integer


Set Sourcecolumn = Sheet1.Range("A4:A100")' i would not be knowing the range also, just i have mentioned 100 from A4
Set Destinationcolumn = Sheet1.Range("H4:H100") ' if i need at last column means how can i do that
For i = 1 To Sourcecolumn.Count
Destinationcolumn(i, 1).Value = Left(Sourcecolumn(i, 1).Value, 2)
Next i


End Sub

Thanks in prior
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
So you want the destination column ( last column) to have the first 2 charatcers of Col "A"

Code:
Sub Leftfunc()
Dim lr As Long, lc As Integer, r As Long
lc = Cells(1, Columns.Count).End(xlToLeft).Column
lr = Cells(Rows.Count, lc).End(xlUp).Row
    For r = 1 To lr
        Cells(r, lc).Value = Left(Cells(r, 1).Value, 2)
    Next r
End Sub
 
Upvote 0
So you want the destination column ( last column) to have the first 2 charatcers of Col "A"

Code:
Sub Leftfunc()
Dim lr As Long, lc As Integer, r As Long
lc = Cells(1, Columns.Count).End(xlToLeft).Column
lr = Cells(Rows.Count, lc).End(xlUp).Row
    For r = 1 To lr
        Cells(r, lc).Value = Left(Cells(r, 1).Value, 2)
    Next r
End Sub

Here is another way to write your function assuming the OP really meant he wanted to overwrite the existing values in the last column with the first two characters from Column A...
Code:
[table="width: 500"]
[tr]
	[td]Sub Leftfunc()
  Dim LastRow As Long, LastCol As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  LastCol = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
  Range("A4").Resize(LastRow - 3).Offset(, LastCol [B][COLOR="#FF0000"]- 1[/COLOR][/B]) = Evaluate("IF({1},LEFT(A4:A" & LastRow & ",2))")
End Sub[/td]
[/tr]
[/table]
However, my gut tells me the OP probably wanted the output to go to the empty column immediately after the last column of data. If so, then he would need to remove the -1 (highlighted in red) from the code above.
 
Last edited:
Upvote 0
So you want the destination column ( last column) to have the first 2 charatcers of Col "A"

Code:
Sub Leftfunc()
Dim lr As Long, lc As Integer, r As Long
lc = Cells(1, Columns.Count).End(xlToLeft).Column
lr = Cells(Rows.Count, lc).End(xlUp).Row
    For r = 1 To lr
        Cells(r, lc).Value = Left(Cells(r, 1).Value, 2)
    Next r
End Sub

This is overlapping the last column, but i need the values in last empty column and can u please explain the code, so that i can learn

Thanks in prior
 
Last edited:
Upvote 0
Here is another way to write your function assuming the OP really meant he wanted to overwrite the existing values in the last column with the first two characters from Column A...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub Leftfunc()
  Dim LastRow As Long, LastCol As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  LastCol = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
  Range("A4").Resize(LastRow - 3).Offset(, LastCol [B][COLOR=#FF0000]- 1[/COLOR][/B]) = Evaluate("IF({1},LEFT(A4:A" & LastRow & ",2))")
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
However, my gut tells me the OP probably wanted the output to go to the empty column immediately after the last column of data. If so, then he would need to remove the -1 (highlighted in red) from the code above.

Thank you sir , it worked exactly how i needed
 
Upvote 0
Rick
I agree !!
However, my gut tells me the OP probably wanted the output to go to the empty column immediately after the last column of data
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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