Macro help

marissalynch

New Member
Joined
Aug 3, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi there - I am trying to write a macro for the following criteria.

The name of the worksheet will always be different. The number of columns will always be the same, however, the number of rows will vary.
Column AV is sorted Z-A so the word Main is first, followed by the word Comparison. I want to cut and paste columns AE through AU into columns N through AD, but only up until Main.

Thank you in advance!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Please provide a copy of a sample Excel sheet that this macro will operate on.
 
Upvote 0
1691164666852.png
 

Attachments

  • 1691164646801.png
    1691164646801.png
    60.4 KB · Views: 10
Upvote 0
Try this Sub (Macro) to Copy AE - AU (when AV = "Main") to N - AD

VBA Code:
Option Explicit
Public Sub Copy_AEtoAU_To_NtoAD()
  Dim lr As Long
  Dim r As Long
  lr = Range("AV" & Rows.Count).End(xlUp).Row
  
  For r = 2 To lr
    If UCase(Range("AV" & r)) = "MAIN" Then
      Range("AE" & r & ":AU" & r).Copy Destination:=ActiveSheet.Range("N" & r & ":AD" & r)
    Else
      Exit For
    End If
  Next r
End Sub
 
Upvote 0
Assuming at least one "Main" row, what about doing them all at once?

VBA Code:
Sub Move_Main()
  Range("AE2", Columns("AV").Find(What:="Main", LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious).Offset(, -1)).Cut Destination:=Range("N2")
End Sub
 
Upvote 0
I'm adding it onto the already existing macro below and not sure where to add it within.

Sub walmartbeacon_cleanup()
'
' walmartbeacon_cleanup Macro
'
' Keyboard Shortcut: Ctrl+Shift+W
'
Cells.Replace What:="Walmart Grocery", Replacement:="Pickup", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Cells.Replace What:="walmart.com", Replacement:="Delivery", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Columns("A:AE").Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
"AE2:AE755"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A1:AE755")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("N:AD").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("T11").Select

End Sub
 
Upvote 0
Try replacing the last 4 lines of your VBA code with the one-line solution
 
Upvote 0
I got a Compile error: Expected End Sub
Sub walmartbeacon_cleanup()
'
' walmartbeacon_cleanup Macro
'
' Keyboard Shortcut: Ctrl+Shift+W
'
Cells.Replace What:="Walmart Grocery", Replacement:="Pickup", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Cells.Replace What:="walmart.com", Replacement:="Delivery", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Columns("A:AE").Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
"AE2:AE755"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A1:AE755")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
Sub Move_Main()
Range("AE2", Columns("AV").Find(What:="Main", LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious).Offset(, -1)).Cut Destination:=Range("N2")
End Sub
 
Upvote 0
I got a Compile error: Expected End Sub
Sub walmartbeacon_cleanup()
'
' walmartbeacon_cleanup Macro
'
' Keyboard Shortcut: Ctrl+Shift+W
'
Cells.Replace What:="Walmart Grocery", Replacement:="Pickup", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Cells.Replace What:="walmart.com", Replacement:="Delivery", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Columns("A:AE").Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
"AE2:AE755"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A1:AE755")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
Sub Move_Main()
Range("AE2", Columns("AV").Find(What:="Main", LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious).Offset(, -1)).Cut Destination:=Range("N2")
End Sub
Remove the Sub Move_Main line
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,957
Members
449,200
Latest member
indiansth

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