Deleting Values in a Cell Based on the Above Cell Value

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello Folks
I am a rookie in excel macro. I need a macro to sort out my issue. If any cell contains text "Name" the letters or numbers before the 1st space should be deleted in the below cell. If anyone able to write code please help me. Thank you.


Before After[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Name
[/TD]
[TD]Name
[/TD]
[/TR]
[TR]
[TD]Peter Siddle
[/TD]
[TD]Siddle
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hello Folks
I am a rookie in excel macro. I need a macro to sort out my issue. If any cell contains text "Name" the letters or numbers before the 1st space should be deleted in the below cell. If anyone able to write code please help me. Thank you.


Before After[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Name[/TD]
[/TR]
[TR]
[TD]Peter Siddle[/TD]
[TD]Siddle[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
For example, your original data is colum A, your result data is colum B
Code:
Sub MrExcel()    Dim sArr(), dArr(), Tmp
    Dim I As Long, J As Long, K As Long
    
    sArr = Range("A2", Range("A2").End(xlDown)).Value
    ReDim dArr(1 To UBound(sArr), 1 To 1)
    For I = 1 To UBound(sArr)
        K = K + 1
        Tmp = Split(sArr(I, 1), " ")
        For J = 1 To UBound(Tmp)
            dArr(K, 1) = Trim(dArr(K, 1) & " " & Tmp(J))
        Next J
    Next I
    Range("B2").Resize(K) = dArr
End Sub
 
Upvote 0
Hi vanthinh3101, thanks for your reply. But i want the result in A2 itself.
Your code
Code:
Sub MrExcel()
    Dim sArr(), dArr(), Tmp
    Dim I As Long, J As Long, K As Long
    
    sArr = Range("A2", Range("A2").End(xlDown)).Value
    ReDim dArr(1 To UBound(sArr), 1 To 1)
    For I = 1 To UBound(sArr)
        K = K + 1
        Tmp = Split(sArr(I, 1), " ")
        For J = 1 To UBound(Tmp)
            dArr(K, 1) = Trim(dArr(K, 1) & " " & Tmp(J))
        Next J
    Next I
    Range("A2").Resize(K) = dArr
End Sub
 
Last edited:
Upvote 0
Your code
Code:
Sub MrExcel()
    Dim sArr(), dArr(), Tmp
    Dim I As Long, J As Long, K As Long
    
    sArr = Range("A2", Range("A2").End(xlDown)).Value
    ReDim dArr(1 To UBound(sArr), 1 To 1)
    For I = 1 To UBound(sArr)
        K = K + 1
        Tmp = Split(sArr(I, 1), " ")
        For J = 1 To UBound(Tmp)
            dArr(K, 1) = Trim(dArr(K, 1) & " " & Tmp(J))
        Next J
    Next I
    Range("A2").Resize(K) = dArr
End Sub

Thanks Its is very useful for me. I have a another scenario similar to this please solve this too. It will be more helpful for me. Now i want to search the whole C column for the text "Name" and delete the letters before the 1st space below that Name cell. Name text could be in different cells in C column



[TABLE="width: 82"]
<tbody>[TR]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[/TR]
[TR]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Thanks Its is very useful for me. I have a another scenario similar to this please solve this too. It will be more helpful for me. Now i want to search the whole C column for the text "Name" and delete the letters before the 1st space below that Name cell. Name text could be in different cells in C column
For your request, I rewrote the code.
You need to choose input data and type output cell.
Have a nice day.
Code:
Sub MrExcel()
    Dim Arr, dArr(), Tmp, Rng As String
    Dim I As Long, J As Long, K As Long
    
    Arr = Application.InputBox("Select input data", "Mr. Excel", Type:=8)
    ReDim dArr(1 To UBound(Arr), 1 To 1)
    For I = 1 To UBound(Arr)
        K = K + 1
        Tmp = Split(Arr(I, 1), " ")
        For J = 1 To UBound(Tmp)
            dArr(K, 1) = Trim(dArr(K, 1) & " " & Tmp(J))
        Next J
    Next I
    Rng = Application.InputBox("Type output cell", "Mr. Excel")
    Range(Rng).Resize(K) = dArr
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top