#1
|
|||
|
|||
Need Macro to Move Text in a Row
Hi all. The attached file is short sample of a file containing more than 500 pages. I would like to know if someone can help me with a macro so I can move the information "ANNUAL LEAVE REGULAR HOURS", which is from column D to column H, and move it to Column M through Column R. If possible, if the information can be put it in the same line or row where you see the text "SICK LEAVE REGULAR HOURS, I will appreciate it. Thank you in advance for your support and cooperation. Cheers! |
#2
|
|||
|
|||
Can you elaborate a little more please:
Do all of the more than 500 worksheets in the workbook require this ? What separates or distinguishes John Doe1 from John Doe2 ? Why the blank row 3 ? From column D to column H (5 columns) <> Column M through Column R (6 columns) Post the macro you now have and are wanting help with. |
#3
|
|||
|
|||
Quote:
Thank you NoSparks for asking. Let me rephrase what I said because I got this wrong from the beginning. The file with over 500 pages is a word document. I saved the file as plain text and then open it with Excel. The excel file has over 15,000 rows of information. Within this 15,000 rows of information, the word "ANNUAL LEAVE REGULAR HOURS" is listed over 550 times (this for number of employees in the file), and to see how is presented in the file, I suggest you open the attachment to see how they are listed in the file. My intention is to find a macro that will move the text "ANNUAL LEAVE REGULAR HOURS", and everything that is right after the text and move it seven columns (column M through column S) to the right and two rows up. I found the below macro (not mine) but it is only moving the text information but not the information from column D to column I. HTML Code:
Sub Findandcut4() 'This macro will move the word "ANNUAL LEAVE REGULAR HOURS" from Column C to Column M Dim row As Long For row = 2 To 50000 ' Check if "save" appears in the value anywhere. If Range("C" & row).Value Like "ANNUAL LEAVE REGULAR HOURS*" Then ' Copy the value and then blank the source. Range("M" & row).Value = Range("C" & row).Value Range("C" & row).Value = "" End If Next End Sub Regards, rsrasc |
#4
|
||||
|
||||
a guess:
Code:
Sub blah() ActiveSheet.Range("A:H").AutoFilter Field:=3, Criteria1:="ANNUAL LEAVE REGULAR HOURS" Set DataBody = Intersect(ActiveSheet.AutoFilter.Range, ActiveSheet.AutoFilter.Range.Offset(1)) Set AnnHrs = DataBody.Columns(3).SpecialCells(xlCellTypeVisible) ActiveSheet.Range("A:H").AutoFilter Field:=3, Criteria1:="SICK LEAVE REGULAR HOURS" Set SicHrs = DataBody.Columns(3).SpecialCells(xlCellTypeVisible) DataBody.AutoFilter Set CellFirst = SicHrs.Cells(1) For Each cll In SicHrs.Cells Set CellsToCopy = Nothing Set CellsToCopy = Intersect(AnnHrs, Range(CellFirst.Offset(1), cll.Offset(-1))) If Not CellsToCopy Is Nothing Then CellsToCopy.Cells(1).Resize(, 6).Copy CellFirst.Offset(, 10) CellsToCopy.Cells(1).Resize(, 6).ClearContents If CellsToCopy.Cells.Count > 1 Then CellsToCopy.Select MsgBox "More than one row to copy! Only the first of the selected cells' rows has been moved." End If End If Set CellFirst = cll Next cll End Sub |
#5
|
|||
|
|||
a more simplistic option...
Column M through column S is 7 columns so columns C through I will be moved (that's the 7 in the resize part) Code:
Sub Move_Stuff() Dim cl As Range Application.ScreenUpdating = False With ActiveSheet For Each cl In .Range("C2", .Range("C" & Rows.Count).End(xlUp)) If cl.Value = "ANNUAL LEAVE REGULAR HOURS" Then cl.Offset(-2, 10).Resize(, 7).Value = cl.Resize(, 7).Value cl.Resize(, 7).ClearContents End If Next cl End With Application.ScreenUpdating = True End Sub |
#6
|
||||
|
||||
Yes, that should work; I missed the 'and two rows up', probably because I started putting something together before that messsage was posted and I hadn't noticed new posts when I did eventually submit my offering (and no, I didn't spend 3 hours + on it!).
|
#7
|
|||
|
|||
@ p45cal
Quote:
That didn't go so well. One of the codes never processes what would be the last John Doe's data. |
#8
|
||||
|
||||
Quote:
Yes, poorly tested. Solved (hopefully) by the addition of: Code:
Set SicHrs = Union(SicHrs, DataBody.Columns(3).Cells(DataBody.Rows.Count).Offset(1)) As to timing, well, I'd expect my offering to be hundreds of times slower - the two codes were designed to handle different scenarios. In general, I would expect the time taken iterating and testing every cell in a contiguous range as opposed to iterating and not testing every cell in a non-contiguous range such as AnnHrs to be slower however, how much slower would be dependent on the proportion of cells testing positive for action; If only 3 cells in 30k cells needed action I suspect the non-contiguous range method would be faster than if 10k cells needed action. I haven't tested. |
#9
|
|||
|
|||
Thanks p45cal, the additional line did it.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
macro to move cells | ewso | Excel Programming | 1 | 10-11-2017 12:23 AM |
A macro to find a symbol, delete it, and move the cursor to that location | JayBird24 | Word VBA | 1 | 08-19-2016 04:26 PM |
How to move data - Macro not working | ecarter312 | Excel Programming | 1 | 08-10-2016 11:26 PM |
Macro to move focus after entry in a cell | Phil H | Excel Programming | 3 | 06-18-2015 01:20 PM |
Macro - Can I move information from a website and excel to word? | redzan | Word VBA | 1 | 03-13-2013 07:39 AM |