Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-20-2013, 04:48 PM
kamakshi kamakshi is offline lastname and firstname Windows XP lastname and firstname Office 2003
Novice
lastname and firstname
 
Join Date: Apr 2013
Posts: 3
kamakshi is on a distinguished road
Default lastname and firstname

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
Reply With Quote
  #2  
Old 09-20-2013, 07:51 PM
BobBridges's Avatar
BobBridges BobBridges is offline lastname and firstname Windows 7 64bit lastname and firstname Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

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)
Perfect. Well, almost perfect; you now have the first name in a variable named "lastName". I won't insult your intelligence by explaining how to fix that part.

Code:
firstName = Right(Cells(rowNum, colNum).Value, Len(Cells(rowNum, colNum).Value) - n)
Not bad. Now you have the remainder of that email address in a value called firstName. The remainder, unfortunately, isn't the first name, nor even the last name; it's the rest of the email address, "lastname@ttc.com" in your example. Do you see why?

Code:
Cells(rowNum, colNum + 1).Value = firstName
Cells(rowNum, colNum + 2).Value = lastName
Now you've put those two values in two cells to the right, the firstName variable (which contains the remainder of the email address) in the first cell to the right, and lastName (which contains the first name) in the cell after that.

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
I don't know why this statement is in there. It seems to concatenate A1 to the end of C1 (with a comma between them), and to do that over and over for every row you're trying to handle. So not only will most of your rows end up as "lastname@ttc.com" and "firstname", but the first row (only) will end up as "lastname@ttc.com" and "addr,firstname,firstname,firstname....". I'm guessing you put that in there while you were experimenting, and forgot to take it out.

Code:
rowNum = rowNum + 1
This is, again, perfect; you're getting ready to look at the next row.

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 '@'.....
Reply With Quote
  #3  
Old 09-20-2013, 08:41 PM
kamakshi kamakshi is offline lastname and firstname Windows XP lastname and firstname Office 2003
Novice
lastname and firstname
 
Join Date: Apr 2013
Posts: 3
kamakshi is on a distinguished road
Default

Thanks i want everyhting in the left of the @
Reply With Quote
  #4  
Old 09-21-2013, 04:08 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is offline lastname and firstname Windows 7 64bit lastname and firstname Office 2010 64bit
Expert
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,779
Pecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant future
Default

A non VBA solution:

Select the column
1st step
  • Ctrl+H
  • Replace what @*
  • With ( leave blank)
  • Replace all
2nd step
  • Ctrl+H
  • replace what .
  • With ,
  • Replace all

There most certainly is a way to translate this to VBA without the necessity of loops
__________________
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
Reply With Quote
  #5  
Old 09-21-2013, 11:01 AM
kamakshi kamakshi is offline lastname and firstname Windows XP lastname and firstname Office 2003
Novice
lastname and firstname
 
Join Date: Apr 2013
Posts: 3
kamakshi is on a distinguished road
Default

Hi
Thanks and i want lastname, firstname
it shows like firstname,lastname
Reply With Quote
  #6  
Old 09-21-2013, 11:14 AM
BobBridges's Avatar
BobBridges BobBridges is offline lastname and firstname Windows 7 64bit lastname and firstname Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

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.
Reply With Quote
Reply



Other Forums: Access Forums

All times are GMT -7. The time now is 09:42 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft