rhadash
asked on
VBA to check cell value for NULL then replace with another value
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
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
ASKER
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
respmacro.txt
I cannot test your macro without your data.
I have setup some data which the macro works on. See attached.
Splitnames.xls
I have setup some data which the macro works on. See attached.
Splitnames.xls
ASKER
ASKER
Are you dropping this question?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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.
Good luck and thanks for the grade.
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