Solved

VBA to check cell value for NULL then replace with another value

Posted on 2012-03-14
8
384 Views
Last Modified: 2012-03-16
Hi Experts,

I have a macro that someone else wrote but I have to make adjustments to.  I need some help to figure out the best way to acheive the following:

If Last Name = null look in the first name column.  Find the space in the first name column then cut the text after the space then past to the last name column.  

Below is current code.  When it runs and the last name = Null the merged field is empty.

'Name************************************************************
'first
Cells(1, 1).Select
k = 1

Do While First_name <> "RAFirst"
If Cells(1, k).Value <> "RAFirst" Then
k = k + 1
Else
First_name = "RAFirst"
End If

Loop
First_name = k



Columns(First_name).Select
Selection.Copy
Columns("N:N").Select
Selection.Insert Shift:=xlToRight

'last
Cells(1, 1).Select
k = 1

Do While Last_name <> "RALast"
If Cells(1, k).Value <> "RALast" Then
k = k + 1
Else
Last_name = "RALast"
End If

Loop
Last_name = k



Columns(Last_name).Select
Selection.Copy
Columns("O:O").Select
Selection.Insert Shift:=xlToRight

'merge 1st and LastNames
Columns("N:N").Select
Selection.Insert Shift:=xlToRight

For i = 2 To j

Cells(i, 14) = Cells(i, 15) & " " & Cells(i, 16)
Next i

Columns("N:N").Select
Selection.Copy

Cells(1, 14).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
0
Comment
Question by:rhadash
  • 4
  • 4
8 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37721602
From what I understood, what you need is simply

Sub splitfirstlast()
Dim cel As Range
For Each cel In Range("B:B")
    If cel = "" And cel.Offset(0, -1) <> "" Then
        cel = Right(cel.Offset(0, -1), Len(cel.Offset(0, -1)) - InStr(cel.Offset(0, -1), " "))
        cel.Offset(0, -1) = Left(cel.Offset(0, -1), InStr(cel.Offset(0, -1), " ") - 1)
    End If
Next cel
End Sub
0
 

Author Comment

by:rhadash
ID: 37721720
I put the code before the name merge and then tried at the begining but did not work.  Code runs no errors but does not provide the desired result.  I attached the macro.
respmacro.txt
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37721991
I cannot test your macro without your data.

I have setup some data which the macro works on. See attached.
Splitnames.xls
0
 

Author Comment

by:rhadash
ID: 37725982
heres the data file that will run with the marco i posted.
Thank you
DATA-031412.xlsx
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:rhadash
ID: 37730037
Are you dropping this question?
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 37730317
Sorry I had lost track.

Try this modification
Replace

For i = 2 To j

Cells(i, 14) = Cells(i, 15) & " " & Cells(i, 16)
Next i

Open in new window


with

For i = 2 To Range("O2").End(xlDown).Row

If Cells(i, 15) = "NULL" Then
Cells(i, 14) = Right(Cells(i, 16), Len(Cells(i, 16)) - InStr(Cells(i, 16), " ")) & " " & Left(Cells(i, 16), InStr(Cells(i, 16), " ") - 1)
Else
Cells(i, 14) = Cells(i, 15) & " " & Cells(i, 16)
End If
Next i

Open in new window

0
 

Author Closing Comment

by:rhadash
ID: 37730711
Thank you!  When I look at the solution you provided it makes sence to me but I cant figure for self.  Any suggestions for me to help me improve /gain skills?
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37730735
Just follow this site. There is enough here to teach you. Just keep an eye on the questions and have a go at the simpler ones and see how others respond to more tricky ones. After some time you would be after the more tricky ones. The key is "Spend time".

Good luck and thanks for the grade.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now