VBA Error 1004: PasteSpecial method of range class failed - vba

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)

Related

Difficulty with the worksheetfunction.match property in VBA

I keep getting the error "Run-time error '91': "Object Variable or With block variable not set" when my macro gets to the line
matchrange = Workbooks("tracker test").Sheets(start_sheet).Range("F" & h).Value
where I am trying to define the PLnumber that will be compared to the PL_compare_list named range. If I try to not define that variable and instead just put the reference directly into my match function on the line below I instead get the error "Run-time error '1004': Unable to get the Match property of the WorksheetFunction class"
What I am trying to do is have this code look at column H on start_sheet to see if it has data yet. then, if it does not, compare the PL numbers on start_sheet in column F to the PL numbers on "Calculation Sheet" in column B to find a row and then open the corresponding file name that is in column A in that row. Thoughts?
Here is my code in its entirety but I think the most relevant bits will be close to the bottom:
Option Explicit
Sub GetFileNames()
Range("A1").Select
ActiveCell.FormulaR1C1 = _
"=REPLACE(CELL(""filename""),FIND(""["",CELL(""filename"")),LEN(CELL(""filename"")),MID(CELL(""filename""),FIND(""]"",CELL(""filename""),1)+1,255))&""_samples shipment PO_PL_Invoice_ attachment\""&TRIM(MID(CELL(""filename""),FIND(""]"",CELL(""filename""),1)+1,255))&""_PL\"""
Range("B1").Select
ActiveCell.FormulaR1C1 = _
"=left(RC[-1],len(RC[-1])-10)"
Range("A1:B1").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Dim directory As String
directory = Range("B1").Value
Dim start_sheet As String
start_sheet = ActiveSheet.Name
Sheets("Calculation Sheet").Activate
Range("D1") = Sheets(start_sheet).Range("A1").Value
Columns("B:B").Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveSheet.Cells(1, 1).Select
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$
InitialFoldr$ = directory
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With
Dim i As Integer
Dim j As Integer
Dim filenumber As Integer
filenumber = Evaluate("CountA(A:A)")
Columns("A:A").Select
Selection.NumberFormat = "#"
j = 1
For i = 1 To filenumber
If InStr(1, Range("A" & i), "xlsx") Then
ActiveSheet.Range("B" & j).Value = ActiveSheet.Range("D1").Value & ActiveSheet.Range("A" & i).Value
j = j + 1
End If
Next i
Columns("B:B").Select
Selection.Copy
Columns("A:A").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("B:E").Select
Application.CutCopyMode = False
Selection.ClearContents
Dim xlfilenumber As Integer
Dim PL_list_length As Integer
xlfilenumber = Evaluate("CountA(A:A)")
ActiveSheet.Range("A1:A" & xlfilenumber).Select
Selection.Name = "list_of_files"
For i = 1 To xlfilenumber
Range("B" & i).Select
ActiveCell.FormulaR1C1 = _
"=MID(RC[-1],FIND(""_PL"",RC[-1],FIND(""_PL\"",RC[-1],1)+4)+1,7)"
Next i
xlfilenumber = Evaluate("CountA(B:B)")
ActiveSheet.Range("A1:A" & xlfilenumber).Select
Selection.Name = "PL_compare_list"
Sheets(start_sheet).Activate
PL_list_length = Evaluate("CountA(F:F)") - 1
Dim h As Integer
Dim g As Integer
Dim filerownum As Integer
Dim matchrange As Range
Dim comparerange As Range
Dim filename As String
For h = 6 To 9
If IsEmpty(Range("J" & h)) Then
matchrange = Workbooks("tracker test").Sheets(start_sheet).Range("F" & h).Value
filerownum = Application.WorksheetFunction.Match(matchrange, Worksheets("Calculation Sheet").Range("PL_compare_list"), 0)
filename = Range("A" & filerownum).Value
Workbooks.Open filename
End If
Next h
Workbooks("tracker test").Sheets(start_sheet).Activate
If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
ActiveSheet.Cells(1, 1).Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveSheet.Cells(1, 2).Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveSheet.Cells(1, 3).Select
Application.CutCopyMode = False
Selection.ClearContents
End Sub
Ideally there's a bunch of other changes you should consider, but to address the question of how to handle a failed Match:
Dim filerownum As Variant
Dim rngSrch As Range
Set rngSrch = Worksheets("Calculation Sheet").Range("PL_compare_list")
For h = 6 To 9
If IsEmpty(Range("J" & h)) Then
matchrange = Workbooks("tracker test").Sheets(start_sheet).Range("F" & h).Value
'drop the WorksheetFunction
filerownum = Application.Match(matchrange, rngSrch, 0)
'test for error return value
If Not IsError(filerownum) Then
filename = Range("A" & filerownum).Value
Workbooks.Open filename
End If
End If
Next h

VBA- Copy specific columns to a sheet from a workbook

i need some help how to fix my syntax. Whenever i try to run it there is an error saying "subscript out of range"
I need to copy columns ("B:F"),("J"),(N:Q), (S:V) from Sheet("Filtered Data") to a workbook Sheet("February 2018 Tracker (Raw)")
When i deleted the selected columns ("J"),(N:Q), (S:V) the code is working and copying the data from columns B2:F2.
I know there is something wrong with my syntax but i can't figure it out how to correct it. Please help.
Thanks
Sub L4toMetrics()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim MainWorkfile As String
Dim OtherWorkfile As String
MainWorkfile = ActiveWorkbook.Name
lRow = Range("C1048576").End(xlUp).Row
Sheets("February 2018 Tracker (Raw)").Select
Range("B2:Q2" & lRow).ClearContents
Range("C1").Select
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open Filename:=Application.GetOpenFilename
OtherWorkfile = ActiveWorkbook.Name
Sheets("Filtered Data").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or _
ActiveWorkbook.ActiveSheet.AutoFilterMode Then _
ActiveWorkbook.ActiveSheet.AutoFilterMode = False
lRw = Range("C1048576").End(xlUp).Row
Range("B2:F2" & lRw).Select
Selection.Copy
Windows(MainWorkfile).Activate
Sheets("February 2018 Tracker (Raw)").Select
lstrw = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Range("B" & lstrw).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(OtherWorkfile).Activate
Sheets("Filtered Data").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or _
ActiveWorkbook.ActiveSheet.AutoFilterMode Then _
ActiveWorkbook.ActiveSheet.AutoFilterMode = False
lRw = Range("C1048576").End(xlUp).Row
Range("J2" & lRw).Select
Selection.Copy
Windows(MainWorkfile).Activate
Sheets("February 2018 Tracker (Raw)").Select
lstrw = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Range("C" & lstrw).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(OtherWorkfile).Activate
Sheets("Filtered Data").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or _
ActiveWorkbook.ActiveSheet.AutoFilterMode Then _
ActiveWorkbook.ActiveSheet.AutoFilterMode = False
lRw = Range("C1048576").End(xlUp).Row
Range("N2:Q2" & lRw).Select
Selection.Copy
Windows(MainWorkfile).Activate
Sheets("February 2018 Tracker (Raw)").Select
lstrw = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row + 1
Range("D" & lstrw).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(OtherWorkfile).Activate
Sheets("Filtered Data").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or _
ActiveWorkbook.ActiveSheet.AutoFilterMode Then _
ActiveWorkbook.ActiveSheet.AutoFilterMode = False
lRw = Range("C1048576").End(xlUp).Row
Range("S2:O2" & lRw).Select
Selection.Copy
Windows(MainWorkfile).Activate
Sheets("February 2018 Tracker (Raw)").Select
lstrw = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row + 1
Range("D" & lstrw).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
You are relying too much on the MACRO-Recorder, try the code below to copy>>paste for the first section (columns "B:F").
You be able to implement it for the rest of the columns.
Option Explicit
Sub L4toMetrics()
Dim MainWorkfile As Workbook
Dim OtherWorkfile As Workbook
Dim TrackerSht As Worksheet
Dim FilterSht As Worksheet
Dim lRow As Long, lRw As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
' set workbook object
Set MainWorkfile = ActiveWorkbook
' set the worksheet object
Set TrackerSht = MainWorkfile.Sheets("February 2018 Tracker (Raw)")
With TrackerSht
lRow = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("B2:Q2" & lRow).ClearContents
End With
Application.AskToUpdateLinks = False
' set the 2nd workbook object
Set OtherWorkfile = Workbooks.Open(Filename:=Application.GetOpenFilename)
' set the 2nd worksheet object
Set FilterSht = OtherWorkfile.Sheets("Filtered Data")
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("B2:F" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("B" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' implement it for the rest of your columns...
End Sub

Issue in Vba code in copying and Union ranges based on particular condition

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

Macro that creates new workbooks by the values in Column M

I need to build a Macro that creates new workbooks based on the values in Column M (distributors). So I would have a new workbook for each distributor. I've tried modifying others on here that were attempting something similar with no success. Thanks in advance.
Here is the macro that I'm trying to get similar results from. The differences are that I need mine based off of column M instead of B. Also, my sheet's name is "taxes_20150619-145507", not Sheet1. I've tried to change these in the code but keep getting errors!
Sub details()
Dim thisWB As String
Dim newWB As String
thisWB = ActiveWorkbook.Name
On Error Resume Next
Sheets("tempsheet").Delete
On Error GoTo 0
Sheets.Add
ActiveSheet.Name = "tempsheet"
Sheets("Sheet1").Select
If ActiveSheet.AutoFilterMode Then
Cells.Select
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
End If
Columns("B:B").Select
Selection.Copy
Sheets("tempsheet").Select
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
If (Cells(1, 1) = "") Then
lastrow = Cells(1, 1).End(xlDown).Row
If lastrow <> Rows.Count Then
Range("A1:A" & lastrow - 1).Select
Selection.Delete Shift:=xlUp
End If
End If
Columns("A:A").Select
Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Range("B1"), Unique:=True
Columns("A:A").Delete
Cells.Select
Selection.Sort _
Key1:=Range("A2"), Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
lMaxSupp = Cells(Rows.Count, 1).End(xlUp).Row
For suppno = 2 To lMaxSupp
Windows(thisWB).Activate
supName = Sheets("tempsheet").Range("A" & suppno)
If supName <> "" Then
Workbooks.Add
ActiveWorkbook.SaveAs supName
newWB = ActiveWorkbook.Name
Windows(thisWB).Activate
Sheets("Sheet1").Select
Cells.Select
If ActiveSheet.AutoFilterMode = False Then
Selection.AutoFilter
End If
Selection.AutoFilter Field:=2, Criteria1:="=" & supName, _
Operator:=xlAnd, Criteria2:="<>"
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
Rows("1:" & lastrow).Copy
Windows(newWB).Activate
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
End If
Next
Sheets("tempsheet").Delete
Sheets("Sheet1").Select
If ActiveSheet.AutoFilterMode Then
Cells.Select
ActiveSheet.ShowAllData
End If
End Sub
Try this.
Sub AddNew()
Set NewBook = Workbooks.Add
With NewBook
.SaveAs fileName:="Allsales.xls" 'Replace with the column M's value
End With
End Sub

Excel VBA code, one macro works when ran by itself, but debugs when ran in a group

My program works by calling a number of macros as such:
Sub Start()
Call ClearAll
Call Sales_Download
Call Copy_Sales
Call Receipt_Download
Call Copy_Receipt
Call Copy1
Call Sales_Summary
Call Copy2
Call Receipt_Summary
End Sub
My program breaks at the copy2, which is essentially an exact replica of copy1 wich works fine. When copy2 is ran by itself it works perfectly, but when I attempt to run the entire program it debugs. The bolded line is where the debug happens.
Sub Copy2()
' Copies all data from Receipt Download tab for each location, and saves in a seperate folder
Dim i As Long
Dim lngLastRow As Long, lngPasteRow As Long
'Find the last row to search through
lngLastRow = Sheets("Receipt_Download").Range("J65535").End(xlUp).Row
'Initialize the Paste Row
lngPasteRow = 2
Dim rng As Range
Dim c As Range
Dim endrow
Dim strName As String
Dim ws As Worksheet
Dim j As Long
endrow = Sheets("names").Range("A65000").End(xlUp).Row
Set rng = Sheets("names").Range("A2:A" & endrow)
j = 1
FBO = strName
For Each c In rng
For i = 2 To lngLastRow
strName = c.Value
If Sheets("Receipt_Download").Range("J" & i).Value = strName Then
Sheets("Receipt_Download").Select
Range("A" & i & ":IV" & i).Copy
Sheets("Summary").Select
Range("A" & lngPasteRow & ":IV" & lngPasteRow).Select
ActiveSheet.Paste
lngPasteRow = lngPasteRow + 1
End If
Next i
j = j + 1
Sheets("Receipt_Download").Select
Rows("1:1").Select
Selection.Copy
Sheets("Summary").Select
Rows("1:1").Select
ActiveSheet.Paste
Columns("D:E").Select
Selection.NumberFormat = "m/d/yyyy"
Sheets("Summary").Select
Range("B25000").Select
ActiveCell.FormulaR1C1 = "Grand Total"
Range("B25000").Select
Selection.Font.Bold = True
Columns("G:G").Select
Selection.Insert Shift:=xlToRight
Range("G1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]=0,""0"",RC[-1])"
Range("G1").Select
Selection.AutoFill Destination:=Range("G1:G24950")
Range("G25000").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-24950]C:R[-1]C)"
Range("G25000").Select
Selection.Copy
Range("F25000").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("G:G").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Sheets("Summary").Select
Range("F25000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Names").Select
With Columns("B")
.Find(what:="", after:=.Cells(1, 1), LookIn:=xlValues).Activate
End With
ActiveSheet.Paste
Sheets("Summary").Select
Range("b1:b30000").Select
For Each Cell In Selection
If Cell.Value = "" Then
Cell.ClearContents
End If
Next Cell
Range("b1:b30000").Select
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Sheets("Summary").Select
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Names").Select
***With Columns("C")
.Find(what:="", after:=.Cells(1, 1), LookIn:=xlValues).Activate***
End With
ActiveSheet.Paste
Sheets("Summary").Select
Range("A1:Z5000").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
File = "C:\Documents and Settings\user\Desktop\New FBO\" & strName & "\" & strName & " Receipts.xls"
ActiveWorkbook.SaveAs Filename:=File, _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Close
IngPasteRow = IngPasteRow + 1
Sheets("Summary").Select
Selection.ClearContents
Next c
End Sub
I would really appreciate any help, I am certainly no VBA master and this has been quite troublesome.
Replace this part of your code
Sheets("Summary").Select
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Names").Select
With Columns("C")
.Find(what:="", after:=.Cells(1, 1), LookIn:=xlValues).Activate
End With
ActiveSheet.Paste
with
Dim lRow As Long
With Sheets("Names")
lRow = .Range("C" & .Rows.Count).End(xlUp).Row + 1
Sheets("Summary").Range("D2").Copy .Range("C" & lRow)
End With
Now try it.
Also few tips
Avoid .Select and .Activate They are a major cause of errors
Indent and appropriately comment your code. Your code is very difficult to read. If you don't indent/comment your code, you will realize that you will not recognize your OWN code if you visit it say after a week :)
In support of Siddharth's answer above, I have take a portion of your code (up to where your break happens) and have indented and avoided the .Select and .Activate that he mentions. Hopefully this gives you a good start on how to make your code more readable for debugging and understanding.
For Each c In rng
For i = 2 To lngLastRow
strName = c.Value
If Sheets("Receipt_Download").Range("J" & i).Value = strName Then
Sheets("Receipt_Download").Range("A" & i & ":IV" & i).Copy _
Destination:=Sheets("Summary").Range("A" & lngPasteRow & ":IV" & lngPasteRow)
lngPasteRow = lngPasteRow + 1
End If
Next i
j = j + 1
Sheets("Receipt_Download").Rows("1:1").Copy Destination:=Sheets("Summary").Rows("1:1")
With Sheets("Summary")
.Columns("D:E").NumberFormat = "m/d/yyyy"
With .Range("B25000")
.Formula = "Grand Total"
.Font.Bold = True
End With
.Columns("G:G").Insert Shift:=xlToRight
With Range("G1")
.FormulaR1C1 = "=IF(RC[-2]=0,""0"",RC[-1])"
.AutoFill Destination:=Range("G1:G24950")
End With
With ("G25000")
.FormulaR1C1 = "=SUM(R[-24950]C:R[-1]C)"
.Copy
End With
.Range("F25000").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Columns("G:G").Delete Shift:=xlToLeft
.Range("F25000").Copy Destination:=Sheets("Names").Columns("B").Find(what:="", after:=Sheets("Names").Cells(1, 1), LookIn:=xlValues)
End With