Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-29-2018, 09:37 AM
rsrasc rsrasc is offline Need Macro to Move Text in a Row Windows 10 Need Macro to Move Text in a Row Office 2013
Competent Performer
Need Macro to Move Text in a Row
 
Join Date: Mar 2014
Location: Germany
Posts: 148
rsrasc is on a distinguished road
Default 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!
Attached Files
File Type: xlsx LeaveBalanceQuestion.xlsx (8.4 KB, 13 views)
Reply With Quote
  #2  
Old 03-29-2018, 02:27 PM
NoSparks NoSparks is offline Need Macro to Move Text in a Row Windows 7 64bit Need Macro to Move Text in a Row Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

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.
Reply With Quote
  #3  
Old 03-30-2018, 09:11 AM
rsrasc rsrasc is offline Need Macro to Move Text in a Row Windows 10 Need Macro to Move Text in a Row Office 2013
Competent Performer
Need Macro to Move Text in a Row
 
Join Date: Mar 2014
Location: Germany
Posts: 148
rsrasc is on a distinguished road
Default

Quote:
Originally Posted by NoSparks View Post
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.

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
Hope this will help clarify and help find a solution.

Regards,
rsrasc
Reply With Quote
  #4  
Old 03-30-2018, 12:20 PM
p45cal's Avatar
p45cal p45cal is offline Need Macro to Move Text in a Row Windows 10 Need Macro to Move Text in a Row Office 2010 32bit
Expert
 
Join Date: Apr 2014
Posts: 866
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

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
Reply With Quote
  #5  
Old 03-30-2018, 03:10 PM
NoSparks NoSparks is offline Need Macro to Move Text in a Row Windows 7 64bit Need Macro to Move Text in a Row Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

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
Reply With Quote
  #6  
Old 03-30-2018, 03:32 PM
p45cal's Avatar
p45cal p45cal is offline Need Macro to Move Text in a Row Windows 10 Need Macro to Move Text in a Row Office 2010 32bit
Expert
 
Join Date: Apr 2014
Posts: 866
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

Quote:
Originally Posted by NoSparks View Post
a more simplistic option...
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!).
Reply With Quote
  #7  
Old 03-30-2018, 09:31 PM
NoSparks NoSparks is offline Need Macro to Move Text in a Row Windows 7 64bit Need Macro to Move Text in a Row Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

@ p45cal
Quote:
and no, I didn't spend 3 hours + on it!
Me neither, although I did extend the OP's two lines of data down to row 25,000 with the intent of doing a speed comparison of the codes thinking maybe I'd learn something I could tuck away for future reference.

That didn't go so well. One of the codes never processes what would be the last John Doe's data.
Reply With Quote
  #8  
Old 03-31-2018, 03:53 AM
p45cal's Avatar
p45cal p45cal is offline Need Macro to Move Text in a Row Windows 10 Need Macro to Move Text in a Row Office 2010 32bit
Expert
 
Join Date: Apr 2014
Posts: 866
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

Quote:
Originally Posted by NoSparks View Post
That didn't go so well. One of the codes never processes what would be the last John Doe's data.
Which code was that?!
Yes, poorly tested. Solved (hopefully) by the addition of:
Code:
Set SicHrs = Union(SicHrs, DataBody.Columns(3).Cells(DataBody.Rows.Count).Offset(1))
directly after the existing Set SicHrs = line.

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.
Reply With Quote
  #9  
Old 03-31-2018, 05:32 AM
NoSparks NoSparks is offline Need Macro to Move Text in a Row Windows 7 64bit Need Macro to Move Text in a Row Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Thanks p45cal, the additional line did it.
Reply With Quote
Reply

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
Need Macro to Move Text in a Row 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
Need Macro to Move Text in a Row 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft