I need you help. I really do not understand what is the wrong with my code. I always get an error message at this point:
Sheets.Add(After:=Sheets(Sheets.Count)).Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
The error message is that: Pastespecial method of range class failed
What I want is to filter the raw data and copy the result to a new sheet in the workbook.
Do you have any suggestion what is the problem with my code?
Thank you for your help in advance!
Sub copypaste()
Dim i, j, v As Long
Dim vSearchCols As Variant
Dim vCols As Variant
Dim FilterFor As String
FilterFor = "=AF*"
Set s1 = ThisWorkbook.Worksheets("RAW DATA")
Set s2 = ThisWorkbook.Worksheets("AF SITE TYPE")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
With s1
vSearchCols = Array("Prefix+short name", "Site type", "Probe Id", "Owner", "SLA Target", "Avg RTT (ms)", "Completion (ms)")
ReDim vCols(0 To UBound(vSearchCols))
For v = LBound(vSearchCols) To UBound(vSearchCols)
vCols(v) = .rows(2).Cells.Find(What:=vSearchCols(v), LookIn:=xlFormulas, LookAt:=xlWhole).Column
Next v
End With
With s1
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells.Resize(.rows.Count - 1, .Columns.Count).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
.AutoFilter Field:=vCols(0), Criteria1:=FilterFor
.Copy
Sheets.Add(After:=Sheets(Sheets.Count)).Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ActiveSheet.Name = "TEMP"
End If
End With
End With
End Sub
This should work:
Dim ws As Worksheet
Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
ws.Name = "TEMP"
With s1
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
.AutoFilter Field:=vCols(0), Criteria1:=FilterFor
.Copy
ws.Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
End With
End With
Or some completely revised code:
Sub copypaste()
Dim i, j, v As Long
Dim vSearchCols As Variant
Dim vCols As Variant
Dim FilterFor As String
Dim ws As Worksheet
Dim s1 As Worksheet
Dim s2 As Worksheet
FilterFor = "=AF*"
With ThisWorkbook
Set s1 = .Worksheets("RAW DATA")
Set s2 = .Worksheets("AF SITE TYPE")
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With
ws.Name = "TEMP"
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
vSearchCols = Array("Prefix+short name", "Site type", "Probe Id", "Owner", "SLA Target", "Avg RTT (ms)", "Completion (ms)")
ReDim vCols(0 To UBound(vSearchCols))
For v = LBound(vSearchCols) To UBound(vSearchCols)
vCols(v) = s1.Rows(2).Cells.Find(What:=vSearchCols(v), LookIn:=xlFormulas, LookAt:=xlWhole).Column
Next v
With s1
.AutoFilterMode = False
With .Range("A1").CurrentRegion
.AutoFilter Field:=vCols(0), Criteria1:=FilterFor
If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
ws.Range("A1").PasteSpecial Paste:=xlPasteValues
End If
End With
End With
With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Related
So I have looked at the same question and answer but it does not help with my problem.
here is the code
Private Sub Update_To_Search_Click()
Dim itmx As ListItem
Set itmx = ListView1.FindItem(Number_Selected.Text, lvwText) ', , lvwPartial)
If itmx Is Nothing Then
MsgBox "No Record", vbCritical
Else
ListView1.ListItems(itmx.Index).Selected = True
ListView1.SetFocus
End If
Dim myindex As Integer
Number_Selected.Text = Me.ListView1.SelectedItem
myindex = Me.ListView1.SelectedItem.Index
TextBox2.Text = Me.ListView1.ListItems.Item(myindex).SubItems(1)
TextBox3.Text = Me.ListView1.ListItems.Item(myindex).SubItems(2)
TextBox4.Text = Me.ListView1.ListItems.Item(myindex).SubItems(3)
TextBox5.Text = Me.ListView1.ListItems.Item(myindex).SubItems(4)
TextBox6.Text = Me.ListView1.ListItems.Item(myindex).SubItems(5)
TextBox7.Text = Me.ListView1.ListItems.Item(myindex).SubItems(6)
TextBox8.Text = Me.ListView1.ListItems.Item(myindex).SubItems(7)
TextBox9.Text = Me.ListView1.ListItems.Item(myindex).SubItems(8)
TextBox10.Text = Me.ListView1.ListItems.Item(myindex).SubItems(9)
'Go get the selected line
Dim Base As Worksheet, GoodData As Worksheet
Dim Rng As Range
Set GoodData = Sheets("GoodDBData")
Set Base = Sheets("Data")
Set wb = Workbooks("Staffing LogV1.7.xlsm")
Set listview = wb.Sheets("ListView")
Set fromsearch = wb.Sheets("FromDB")
Set Rng = Base.Range("A20:A28")
FilePath = CStr(wb.Sheets("Data").Cells(2, "A"))
filename = "DB.xlsx"
Application.ScreenUpdating = False
Set DB = Workbooks.Open(FilePath & "\" & filename)
Application.ScreenUpdating = True
Rng.Copy
DB.Sheets("Search Criteria").Range("A2").PasteSpecial _
Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
With DB.Sheets("DB")
With .Rows(1)
Selection.AutoFilter
Selection.AutoFilter
End With
End With
Dim rCrit1 As Range, rCrit2 As Range, rCrit3 As Range, rCrit4 As Range, rCrit5 As Range, rCrit6 As Range, rCrit7 As Range, rCrit8 As Range
Dim rRng1 As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rCrit1 = Sheets("Search Criteria").Range("A2")
Set rCrit2 = Sheets("Search Criteria").Range("B2")
Set rCrit3 = Sheets("Search Criteria").Range("C2")
Set rCrit4 = Sheets("Search Criteria").Range("D2")
Set rCrit5 = Sheets("Search Criteria").Range("E2")
Set rCrit6 = Sheets("Search Criteria").Range("F2")
Set rCrit7 = Sheets("Search Criteria").Range("G2")
Set rCrit8 = Sheets("Search Criteria").Range("H2")
Set rRng1 = Sheets("DB").Range("A1").CurrentRegion
With rRng1
If rCrit1.Value <> "" Then
.AutoFilter field:=11, Criteria1:=rCrit1.Value, Operator:=xlOr
End If
If rCrit2.Value <> "" Then
.AutoFilter field:=7, Criteria1:=rCrit2.Value, Operator:=xlOr
End If
If rCrit3.Value <> "" Then
.AutoFilter field:=13, Criteria1:=rCrit3.Value, Operator:=xlOr
End If
If rCrit4.Value <> "" Then
.AutoFilter field:=14, Criteria1:=rCrit4.Value, Operator:=xlOr
End If
If rCrit5.Value <> "" Then
.AutoFilter field:=16, Criteria1:=rCrit5.Value, Operator:=xlOr
End If
If rCrit6.Value <> "" Then
.AutoFilter field:=30, Criteria1:=rCrit6.Value, Operator:=xlOr
End If
If rCrit7.Value <> "" Then
.AutoFilter field:=32, Criteria1:=rCrit7.Value, Operator:=xlOr
End If
If rCrit8.Value <> "" Then
.AutoFilter field:=37, Criteria1:=rCrit8.Value, Operator:=xlOr
End If
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End sub
The following still does not copy and paste the criteria to look for. For some reason it only copies blanks no data is entered in Searcriteria. rangeA2.
Rng.Copy
DB.Sheets("Search Criteria").Range("A2").PasteSpecial _
Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
I'm at a lost and I'm looking for any help I could get.
Thank you very much
Check this for me.
Replace your code
Base.Select
Base.Range("A7:A15").Select
Selection.Copy
FilePath = CStr(wb.Sheets("Data").Cells(2, "A"))
FileName = "DB.xlsx"
Application.ScreenUpdating = False
Set Db = Workbooks.Open(FilePath & "\" & FileName)
Application.ScreenUpdating = True
Sheets("Search Criteria").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("DB").Select
Rows("1:1").Select
Selection.AutoFilter
Selection.AutoFilter
With
Dim Rng As Range
Set Rng = Base.Range("A7:A15")
FilePath = CStr(wb.Sheets("Data").Cells(2, "A"))
FileName = "DB.xlsx"
Application.ScreenUpdating = False
Set Db = Workbooks.Open(FilePath & "\" & FileName)
Application.ScreenUpdating = True
Rng.Copy
Db.Sheets("Search Criteria").Range("A2").PasteSpecial _
Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
With Db.Sheets("Search Criteria")
With .Rows(1)
'~~> REST OF THE CODE
End With
End With
Now Try it?
I am attempting to create a macro that will pull data from several sheets and display them in an 'OVERVIEW' sheet.
At the moment I have the following:
Sheets("Sheet1).Select
ActiveCell.Range("A1:G7").Select
SELECTION.Copy
Sheets("OVERVIEW").Select
ActiveCell.Select
ActiveSheet.Paste
Sheets("Sheet2").Select
ActiveCell.Range("A1:G7").Select
Application.CutCopyMode = False
SELECTION.Copy
Sheets("OVERVIEW").Select
ActiveCell.Offset(7, 0).Range("A1").Select
ActiveSheet.Paste
Sheets("Sheet3").Select
ActiveCell.Range("A1:G2").Select
Application.CutCopyMode = False
SELECTION.Copy
Sheets("OVERVIEW").Select
ActiveCell.Offset(7, 0).Range("A1").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=-12
ActiveCell.Columns("A:A").EntireColumn.EntireColumn.AutoFit
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Application.CutCopyMode = False
Unfortunately, this currently only copies the data from the first sheet. I would much rather have something along the lines of the following pseudo code
sub COPY1()
Selection = []
curentRow = 1
while(notEmpty(cell(AcurentRow)))
Selection.add(curentRow)
curentRow++
return Selection
End Sub
sub PASTE1(selection)
curentRow=1
while(notEmpty(cell(AcurentRow)))
curentRow++
paste(selection)
End Sub
You can loop through the sheets, and it will skip over "OVERVIEW"
Sub Button1_Click()
Dim ws As Worksheet, sh As Worksheet, LstRw As Long
Set ws = Sheets("OVERVIEW")
For Each sh In Sheets
If sh.Name <> ws.Name Then
With sh
LstRw = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A1:G" & LstRw).Copy
ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With
End If
Next sh
Application.CutCopyMode = False
End Sub
sub copy_to_overview()
currentRow = 1
while (notempty(cell(currentrow))
currentrow.copy
sheet("overwiev").currentrow.paste
currentrow = currentrow + 1
wend
end sub
My code is giving me runtime error 424 in the highlighted line. What could be the possible reason? My rows are not getting copied. CopyRng12 creates some sort of issue.
sub grouping()
Set ws6 = Workbooks("A.xlsx").Worksheets("X1")
Set ws7 = Workbooks("B.xlsx").Worksheets("X2")
LastRowu = ws6.Cells(Rows.Count, "B").End(xlUp).Row
LastRowb = ws7.Cells(Rows.Count, "K").End(xlUp).Row
LastRowb1 = ws7.Cells(Rows.Count, "L").End(xlUp).Row
Application.Calculation = xlAutomatic
ws6.Columns("E:E").Insert Shift:=xlToRight,
CopyOrigin:=xlFormatFromLeftOrAbove
ws6.Range("E2").FormulaR1C1 = _
"=VLOOKUP(RC[-1],'[B.xlsx]X2'!C11:C12,2,0)"
ws6.Range("E2").AutoFill Destination:=ws6.Range("E2:E" & LastRowu),
Type:=xlFillDefault
With ws6.UsedRange
.Copy
.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End With
ws6.Cells.Replace "#N/A", "Company Code Not Found", xlWhole
Workbooks("A.xlsx").Worksheets("X1").Activate
ws6.Columns("D:D").Select
Selection.Copy
ws6.Columns("A:A").Select
Selection.Insert Shift:=xlToRight
For q = LastRowu - 1 To 1 Step -1
If ws6.Cells(q, "F").Value = "G1" Then
**If Not CopyRng12 Is Nothing Then
Set CopyRng12 = Application.Union(CopyRng12, ws6.Rows(q))**
Else
Set CopyRng12 = ws6.Rows(q)
End If
End If
Next q
Set wbmm = Workbooks("G1.xlsx")
Workbooks("G1.xlsx").Activate
Dim wsmm As Worksheet
Set wsmm = wbmm.Worksheets("X1")
Workbooks("G1.xlsx").Worksheets("X1").Activate
CopyRng12.Copy
Worksheets("X2").ClearContents
ActiveSheet.Paste
End Sub
I have code below help me to copy filtered value and paste to different worksheet.
It always stop at apple... (Apple result looks fine)and pop up Run-time error'1004' Application-defined or object-defined error..
Sub CoWFTR()
'Filter out Apple
Sheet1.Range("A1:ER1").Select
Selection.AutoFilter Field:=11, Criteria1:=Array( _
"ILOVEApple"), Operator:=xlFilterValues
'Copy and Paste to Apple Tab
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Apple").Select
ActiveSheet.Paste
Sheet1.Range("A1").Select
Application.CutCopyMode = False
'Clear Filter
On Error Resume Next
Sheet1.ShowAllData
On Error GoTo 0
'Filter out Banana
Sheet1.Range("A1:ER1").Select
Selection.AutoFilter Field:=11, Criteria1:=Array( _
"ILOVEBanana"), Operator:=xlFilterValues
'Copy and Paste to Banana Tab
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Banana").Select
ActiveSheet.Paste
Sheet1.Range("A1").Select
Application.CutCopyMode = False
'Clear Filter
On Error Resume Next
Sheet1.ShowAllData
On Error GoTo 0
End Sub
Copy the 2 procedures bellow in the same module, and update FILTER_ITEMS with your criteria:
Option Explicit
Public Sub CoWFTR()
Const FILTER_COL As Long = 11 'K
Const FILTER_ITEMS As String = "ILOVEApple,ILOVEBanana"
Dim wsFrom As Worksheet, wsDest As Worksheet, fi As Variant, i As Long
Set wsFrom = Sheet1 '<--- Update this
fi = Split(FILTER_ITEMS, ",")
Application.ScreenUpdating = False
For i = 0 To UBound(fi)
Set wsDest = CheckNamedSheet(fi(i))
With wsFrom.UsedRange
.AutoFilter Field:=11, Criteria1:="=" & fi(i), Operator:=xlFilterValues
.Copy 'Copy visible data
End With
With wsDest.Cells
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteAll
Application.CutCopyMode = False
wsDest.Activate
.Cells(1, 1).Select
End With
Next
With wsFrom
.Activate
.Cells(1, 1).Copy
.UsedRange.AutoFilter
End With
Application.ScreenUpdating = True
End Sub
This manages the new sheets
Private Function CheckNamedSheet(ByVal sheetName As String) As Worksheet
Dim ws As Worksheet, result As Boolean, activeWS As Worksheet
Set activeWS = IIf(ActiveSheet.Name = sheetName, Worksheets(1), ActiveSheet)
For Each ws In Worksheets
If ws.Name = sheetName Then
Application.DisplayAlerts = False
ws.Delete 'delete sheet if it already exists
Application.DisplayAlerts = True
Exit For
End If
Next
Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count)) 'create a new one
ws.Name = sheetName
activeWS.Activate
Set CheckNamedSheet = ws
End Function
For your code, the error you are getting is at this line:
Sheet1.Range("A1").Select
It repeats for Bananas as well, and is triggered by the fact that it tries to select Range("A1") on Sheet1, but the active sheet is Apple (or Banana), so to fix the issues you need to add this line:
Sheet1.Activate
Here is your code, fixed:
Sub CoWFTR()
'Filter out Apple
Sheet1.Range("A1:ER1").Select
Selection.AutoFilter Field:=11, Criteria1:=Array( _
"ILOVEApple"), Operator:=xlFilterValues
'Copy and Paste to Apple Tab
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Apple").Select
ActiveSheet.Paste
Sheet1.Activate 'Fix to error 1004
Sheet1.Range("A1").Select
Application.CutCopyMode = False
'Clear Filter
On Error Resume Next
Sheet1.ShowAllData
On Error GoTo 0
'Filter out Banana
Sheet1.Range("A1:ER1").Select
Selection.AutoFilter Field:=11, Criteria1:=Array( _
"ILOVEBanana"), Operator:=xlFilterValues
'Copy and Paste to Banana Tab
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Banana").Select
ActiveSheet.Paste
Sheet1.Activate 'Fix to error 1004
Sheet1.Range("A1").Select
Application.CutCopyMode = False
'Clear Filter
On Error Resume Next
Sheet1.ShowAllData
On Error GoTo 0
End Sub
I think it is useful using xlCellTypeVisible. And Use Array.
Sub CoWFTR()
Dim WS As Worksheet, toWs As Worksheet
Dim rngDB As Range, rngTo As Range
Dim vCriteria, vName, i As Integer
Set WS = Sheet1
Set toWs = Sheets("Apple")
Set rngDB = WS.Range("a1").CurrentRegion
vCriteria = Array("ILOVEApple", "ILOVEBanana")
vName = Array("Apple", "Banana")
For i = 0 To UBound(vCriteria)
If WS.FilterMode Then
WS.ShowAllData
End If
Set toWs = Sheets(vName(i))
Set rngTo = toWs.Range("a" & Rows.Count).End(xlUp)(2)
rngDB.AutoFilter Field:=11, Criteria1:=Array( _
vCriteria(i)), Operator:=xlFilterValues
rngDB.SpecialCells(xlCellTypeVisible).Offset(1).Copy rngTo
Next i
If WS.FilterMode Then
WS.ShowAllData
End If
End Sub
I'm having a bit of trouble with any kind of paste method I use at the moment.
Data from one sheet must be cut and pasted to another, but I'm not sure what I'm missing.
The error occurs here, shortly after the commented "HERE" :
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Full code can be found below, thanks for any replies.
Option Explicit
Public Sub Workbook_Open()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wb As Variant
Dim wsName As Variant
Dim blastrow As Variant
Dim flastrow As Variant
Dim lastrow As Variant
ActiveWorkbook.Sheets("combined").Select
Range("A1:U9999").ClearContents
Dim MyObj As Object, MySource As Object, file As Variant
file = Dir("G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\")
'file level loop
While (file <> "")
If InStr(file, ".xlsx") > 0 Then
Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\" & file
wb = ActiveWorkbook.Name
'ws = ActiveSheet.Name
Dim ws As Worksheet
'worksheet/tab level loop
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
wsName = ws.Name
'andrew code (09/12/2015)
blastrow = Workbooks("Copy of merge.xlsb").Worksheets("Combined").Range("A" & Rows.Count).End(xlUp).Row + 1
If blastrow = 2 Then blastrow = 1
Workbooks("Copy of merge.xlsb").Worksheets("Combined").Range("A" & blastrow & ":XFD" & blastrow).Value = _
Workbooks(wb).Worksheets(wsName).Range("A1:XFD1").Value
lastrow = Range("A" & Rows.Count).End(xlUp).Row
'finding status column
Range("M1").Select
Do Until ActiveCell.Value = "Status" Or ActiveCell.Column > 100
If Range("A2") = "" Then
GoTo there
End If
ActiveCell.Offset(0, 1).Select
Loop
'looping through
Do Until ActiveCell.Row > lastrow
If ActiveCell.Value = "Solved" Then 'HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
wb = ActiveWorkbook.Name
wb = Replace(wb, ".xlsx", "")
ActiveCell.EntireRow.Cut
Workbooks("copy of merge.xlsb").Activate
'find matching company
Range("E1").Select
While ActiveCell.Value <> "CoName"
ActiveCell.Offset(0, 1).Select
Wend
Do Until ActiveCell.Value = wb
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then
ActiveCell.EntireRow.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Loop
'first cell in row select
ActiveSheet.Cells(ActiveCell.Row, 1).Select
'find matching ws
If ws = "Be Wiser" Then
Do Until ActiveCell.Value = "BW"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Insure Wiser" Then
Do Until ActiveCell.Value = "IW"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Call Wiser" Then
Do Until ActiveCell.Value = "CW"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Quote Wiser" Then
Do Until ActiveCell.Value = "QW"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Be Wiser Business" Then
Do Until ActiveCell.Value = "BWB"
ActiveCell.Offset(1, 0).Select
Loop
ElseIf ws = "Younger But Wiser" Then
Do Until ActiveCell.Value = "YBW"
ActiveCell.Offset(1, 0).Select
Loop
End If
'insert row and paste
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'lastrow = Range("A" & Rows.Count).End(xlUp).Row + 1
'Range("A" & lastrow).Select
'ActiveSheet.Paste
ws.Activate
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Cells.Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("A2:A19" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A1:U" & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("M1").Select
Do Until ActiveCell.Value = "Status" Or ActiveCell.Column > 100
ActiveCell.Offset(0, 1).Select
Loop
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
there:
'here
flastrow = Workbooks("Copy of merge.xlsb").Worksheets("Combined").Range("A" & Rows.Count).End(xlUp).Row
If blastrow = flastrow Then
Workbooks("Copy of merge.xlsb").Worksheets("Combined").Activate
Range("A" & blastrow).Select
ActiveCell.EntireRow.Delete
Workbooks(wb).Worksheets(wsName).Activate
End If
Next ws
Workbooks(wb).Close False
End If
file = Dir
Wend
Call storeFileNames
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
As has been noted, you really ought to rewrite this, but as a quick fix, add a range variable:
Dim rgCut as Excel.Range
then instead of this:
ActiveCell.EntireRow.Cut
use:
set rgCut = ActiveCell.EntireRow
and then replace this:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
with this:
rgCut.Cut Destination:=Selection.Cells(1)