![]() |
#1
|
|||
|
|||
![]()
HI
I have column a1 has firstname.lastname@ttc.com in column b1 should be lastname, firstname could you please help me this is the code i have written it comes with lastname@ttc.com firstname as two sepearate columns and i want only lastname,firstname in one column ( between lastname and firstname include comma Code:
Dim firstName As String Dim lastName As String Dim n As Integer Dim rowNum As Integer Dim colNum As Integer rowNum = 1 colNum = 1 While Cells(rowNum, colNum).Value <> "" n = InStr(1, Cells(rowNum, colNum).Value, ".") lastName = Left(Cells(rowNum, colNum).Value, n - 1) MsgBox (lastName) firstName = Right(Cells(rowNum, colNum).Value, Len(Cells(rowNum, colNum).Value) - n) MsgBox (firstName) Cells(rowNum, colNum + 1).Value = firstName Cells(rowNum, colNum + 2).Value = lastName Cells(1, 3).Value = Cells(1, 3).Value & ", " & Cells(1, 1).Value rowNum = rowNum + 1 Wend |
#2
|
||||
|
||||
![]()
This isn't a bad start, kamakshi. Let's analyze what the program is actually doing, and maybe that'll help you understand how to change it:
Code:
n = InStr(1, Cells(rowNum, colNum).Value, ".") lastName = Left(Cells(rowNum, colNum).Value, n - 1) Code:
firstName = Right(Cells(rowNum, colNum).Value, Len(Cells(rowNum, colNum).Value) - n) Code:
Cells(rowNum, colNum + 1).Value = firstName Cells(rowNum, colNum + 2).Value = lastName This is exactly what you said is happening, and maybe you already understood why it's happening that way. Code:
Cells(1, 3).Value = Cells(1, 3).Value & ", " & Cells(1, 1).Value Code:
rowNum = rowNum + 1 What you need, here, is a) to change the name of that first variable to "firstName" (because that's what you're putting in it), and b) to figure out how to extract lastName from "lastname@ttc.com". Hint: You already did something very like it in the first step, figuring out where the '.' was and taking everything to the left of it. Now you want everything to the left of the '@'..... |
#3
|
|||
|
|||
![]()
Thanks i want everyhting in the left of the @
|
#4
|
||||
|
||||
![]()
A non VBA solution:
Select the column 1st step
There most certainly is a way to translate this to VBA without the necessity of loops ![]()
__________________
Using O365 v2503 - Did you know you can thank someone who helped you? Click on the tiny scale in the right upper hand corner of your helper's post |
#5
|
|||
|
|||
![]()
Hi
Thanks and i want lastname, firstname it shows like firstname,lastname |
#6
|
||||
|
||||
![]()
Kamakshi, I won't speak for anyone else here, but if it's left up to me the only kind of help you'll get is the kind that helps you think about this until you understand it and can solve it yourself. So far as I can tell from your last two posts, you're not thinking; you're just half-reading and not trying very hard to understand.
Others may disagree with me and just give you a formula. But I think you should read what Pecoflyer and I have written, and ask questions about it, and/or explain a) what you understand about it and b) what you don't understand. Actually you don't have to read both our posts. Peco's right, you don't need a VBA program to do this. I just answered in terms of VBA because that's how you phrased the question; but you can do it with worksheet functions instead. |
![]() |
|