vba code to appear names beside the total


I would like to ask for an help.

I am trying a code in vba to give the unit name(column B) beside the unit total(column A), where ever the total names are there in column A the unit name should appear beside in column B, the data is not constant it changes every time when i run it. At the start of unit the unit name is available but the end where the total of unit is there the unit name is required.

Could you please assist on this, i have attached the example file of this scenario.
rdy123 rdyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It seems like the file didn't get attached...
rdy123 rdyAuthor Commented:
Sorry. PFA
Here you go:
Sub Demo()
    Dim rng As Range
    Dim cell As Range
    Dim strUnitName As String
    Set rng = Selection
    For Each cell In rng.Cells
        If (cell.Value & "" <> "") And (cell.Offset(, 1) & "" <> "") Then
            strUnitName = cell.Offset(, 1)
        ElseIf (cell.Value & "" <> "") And (cell.Offset(, 1) = "") Then
            cell.Offset(, 1) = strUnitName
        End If
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rdy123 rdyAuthor Commented:
thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.