Macro to take data from string and put it in another cell

3thr3e

New Member
Joined
Mar 12, 2011
Messages
15
Hi,

I have a column of data (column D) which contains a 3 digit string I want to extract from it. I've come up with the code below which replaces each cell in D with that 3 digit string. However, this is not what I want to do. I want to keep each cell in column D the same, but put that 3 digit string in column A (same row number).

Is it just a simple modification to the code below? Also I would like the loop to start from D2 and go down (as D1 is a column heading). And obviously A2 when putting the sxtracted data in.

Any help would be much appreciated :)

Sub HotelName()
Dim LR As Long, i As Long, myCell As String
LR = Range("D" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With Range("D" & i)
If Left(.Value, 3) <> "999" Then
.Value = " " & .Value
Else
.Value = Mid(.Value, 14, 3)
End If
End With
Next i
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try

Code:
Sub HotelName()
Dim LR As Long, i As Long, myCell As String
LR = Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    With Range("D" & i)
        If Left(.Value, 3) <> "999" Then
            .Offset(, -3).Value = " " & .Value
        Else
            .Offset(, -3).Value = Mid(.Value, 14, 3)
        End If
    End With
Next i
End Sub
 
Upvote 0
Worked a treat - Thanks!

Try

Code:
Sub HotelName()
Dim LR As Long, i As Long, myCell As String
LR = Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    With Range("D" & i)
        If Left(.Value, 3) <> "999" Then
            .Offset(, -3).Value = " " & .Value
        Else
            .Offset(, -3).Value = Mid(.Value, 14, 3)
        End If
    End With
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,575
Messages
6,179,637
Members
452,934
Latest member
Jdsonne31

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