VBA Moving Matching Data after finding 2 columns with the same heading

Alroj

Board Regular
Joined
Jan 12, 2016
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Hi Team, Hoping you can assist me with the VBA below.

I have this sheet with many columns divided in 2 sections each section contains 13 events which are number from 1 to 13. So I am intending to bring across the events of the second section and put it to the right of the column that contains the matching heading. As the macro start bringing columns across, the original ranges should also change and I am moving it using the offset function. However I am getting a "run-time error 424: object required". Not sure why it is getting stuck there. Your assistance would be greatly appreciated

VBA Code:
Sub MovingMatchingData()
  
 Dim rngb, rngp As Range
 
 Set rngb = Range("v5:ah5") 'range of the second set of events
 Set rngp = Range("d5:u5")  'range of the first set of events
  
      
For i = 1 To 34 'lastcolumn
For j = 1 To 13 'count of events

'Finds the event and cut the whole column and brings it across and put it to the right of the matching heading
rngb.Select
Selection.Find(What:=j, LookIn:=xlValues).Select
  Range(ActiveCell, ActiveCell.End(xlDown)).Select
  Selection.Cut
    
rngp.Select
Selection.Find(What:=j, LookIn:=xlValues).Offset(0, 1).Select
    Selection.Insert shift:=xlToRight
 
 'As the columns move across the original ranges need to chance to the right so it can capture the later events otherwise the later events fall outside the range
 rngb = rngb.Offset(0, 1)
 rngp = rngp.Offset(0, 1)

 Next j
 
Next i
  
End Sub
 
You will catch this sort of error if you put Option Explicit at the top of the module and then declare your variables.
In this case you would have had
Dim rngb as Range, rngp as Range

The immediate fix is to put Set in front of these 2 lines.
Rich (BB code):
Set rngb = rngb.Offset(0, 1)
Set rngp = rngp.Offset(0, 1)
 
Upvote 0
Thank you for the response Alex,
The macro worked after the adjustment but was not covering the lenght of the range. So I have modified a bit and added the following line and excluded your suggestion

Range(ActiveCell, ActiveCell.Offset(0, lastcolumn)).Select

Now this works as the ranges are now dynamic but it goes in an endless loop. How do I stop it after it finds the last event? In this case the column under the label "13"

Much appreciated for your assistance



VBA Code:
Sub MovingMatchingData()

Dim rngb, rngp As Range
 
 Set rngb = Range("v5:ah5") 'range of the second set of events
 Set rngp = Range("d5:u5")  'range of the first set of events
  
      
For i = 1 To 34 'last column
For j = 1 To 13 'count of events

'Finds the event and cut the whole column and brings it across and put it to the right of the matching heading
rngb.Select
Selection.Find(What:=j, LookIn:=xlValues).Select
Range(ActiveCell, ActiveCell.Offset(0, lastcolumn)).Select
Selection.Find(What:=j, LookIn:=xlValues).Select
  Range(ActiveCell, ActiveCell.End(xlDown)).Select
  Selection.Cut
    
rngp.Select
Selection.Find(What:=j, LookIn:=xlValues).Offset(0, 1).Select
    Selection.Insert shift:=xlToRight
 

 Next j
 
Next i

End Sub
 
Upvote 0
Please show us what it looks like before the macro and what you want it to look like after it has run.
 
Upvote 0

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