?
Solved

excel macro to copy line above it and remove a character

Posted on 2011-05-11
5
Medium Priority
?
311 Views
Last Modified: 2012-05-11
Hey folks,

Is it possible to create a macro to make a macro to copy the row of cells immediately above the blank row you're highlighting, and strip a few characters from one of the cells? For example, see attached sheet - basically I just want to copy the top row into a new row and strip the trailing # up to the hyphen on the right from the highlighted cell.

Any advice appreciated!

Thanks!

example-sheet.xls
0
Comment
Question by:merv109
5 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35735675
>>>strip the trailing # up to the hyphen on the right from the highlighted cell.

I don't see the "#"?

Sid
0
 
LVL 17

Expert Comment

by:aflockhart
ID: 35735720
Sub CopyAndEdit()

    ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Copy
    ActiveSheet.Paste
    Application.CutCopyMode = False
   
    'ch
    ActiveCell.Offset(0, 4).Select
    'Selection.FormulaR1C1 = "ttt"
   
    'now strip the
   
    Do While Right(Selection.Value, 1) <> "-" And Len(Selection) > 0
   
     Selection.FormulaR1C1 = Left(Selection.Value, Len(Selection.Value) - 1)
   
    Loop
   
    'if you want to keep the trailing hyphen, remove the following line
    If Len(Selection) > 0 Then Selection.FormulaR1C1 = Left(Selection.Value, Len(Selection.Value) - 1)
   
   
End Sub
0
 
LVL 17

Accepted Solution

by:
aflockhart earned 2000 total points
ID: 35735734
Sorry, some shouldhave deleted some test code and finished the comments :

You need to place the cursor at the start of the blank row below the data thatyou want to copy.

Tidier version here:
Sub CopyAndEdit()

    ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Copy
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
    'choose the right cell to work on
    ActiveCell.Offset(0, 4).Select
    
    
    'now strip the trailing numbers
    
    Do While Right(Selection.Value, 1) <> "-" And Len(Selection) > 0
         Selection.FormulaR1C1 = Left(Selection.Value, Len(Selection.Value) - 1)
    Loop
    
    'if you want to keep the trailing hyphen, remove the following line
    If Len(Selection) > 0 Then Selection.FormulaR1C1 = Left(Selection.Value, Len(Selection.Value) - 1)
    
    
End Sub

Open in new window

0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 35735967
You can try this formula

=LEFT(E2,FIND("##",SUBSTITUTE(E2,"-","##",LEN(E2)-LEN(SUBSTITUTE(E2,"-",""))))-1)
0
 

Author Closing Comment

by:merv109
ID: 35737197
Spot on m8, thanks for the detailed response, and for commenting the code. :)
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

New style of hardware planning for Microsoft Exchange server.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

840 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