VBA Hangs On Pasting a Formula Into a Range - vba

I've been at this one a while and I have had it working quickly, executing in a couple of seconds over a couple of thousand lines of data, but for some reason it's now constantly locking up at the point of applying the formula to the range.
I've tried it with Index/Match and with Vlookup and both hang at the same point. I then re-worked the whole thing to read all of the data into a couple of arrays, do the lookup entirely in VBA with Application.Worksheetfunction and return the values to a third array before dumping back into Excel, but I abandoned this as the loop was really messy.
Code is as follows, noted at the point it locks - always at the line .Formula = "***Etc. Apologies if it looks a little messy, it's a work in progress with code still to be tidied up.
Any ideas?
Sub ppmTracking()
On Error GoTo EndHere
Dim trPath
trPath = "C:\Users\hbartles\Desktop\PPM\Tracking Report\[MichPPMTracking3.xls]MichPPMTracking3"
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Sheets(1).Activate
'''''ORDER STATUS
With Range("R2", Range("B2").End(xlDown).Offset(0, 16))
.Formula = "=INDEX('" & trPath & "'!F:F, MATCH($A2&$D2,'" & trPath & "'!A:A,FALSE))" '<<<<LOCKS UP HERE
.Copy
.PasteSpecial Paste:=xlPasteValues
End With
'''''LINE STATUS
With Range("S2", Range("B2").End(xlDown).Offset(0, 17))
.Formula = "=INDEX('" & trPath & "'!G:G, MATCH($A2&$D2,'" & trPath & "'!A:A,FALSE))" '<<<<LOCKS UP HERE
.Copy
.PasteSpecial Paste:=xlPasteValues
End With
''''DESPATCH QUANTITY
With Range("T2", Range("B2").End(xlDown).Offset(0, 18))
.Formula = "=INDEX('" & trPath & "'!H:H, MATCH($A2&$D2,'" & trPath & "'!A:A,FALSE))" '<<<<LOCKS UP HERE
.Copy
.PasteSpecial Paste:=xlPasteValues
.Replace What:="0", Replacement:="", LookAt:=xlWhole
.Interior.ColorIndex = xlNone
End With
i = 2
For Each cell In Range("T2", Range("B2").End(xlDown).Offset(0, 18))
If Not cell.Text = "#N/A" Then
If Not cell.Text = "" Then
If cell.Value < Range("F" & i).Value Then cell.Interior.ColorIndex = 6
End If
End If
i = i + 1
Next cell
'''''DESPATCH DATE
With Range("U2", Range("B2").End(xlDown).Offset(0, 19))
.Formula = "=INDEX('" & trPath & "'!I:I, MATCH($A2&$D2,'" & trPath & "'!A:A,FALSE))" '<<<<LOCKS UP HERE
.Copy
.PasteSpecial Paste:=xlPasteValues
.NumberFormat = "General"
End With
For Each cell In Range("U2", Range("B2").End(xlDown).Offset(0, 19))
cell.Value = cell.Value
Next cell
With Range("U2", Range("B2").End(xlDown).Offset(0, 19))
.Replace What:="0", Replacement:="", LookAt:=xlWhole
.NumberFormat = "m/d/yyyy"
End With
'''''TRACKING NUMBER
With Range("V2", Range("B2").End(xlDown).Offset(0, 20))
.Formula = "=INDEX('" & trPath & "'!J:J, MATCH($A2&$D2,'" & trPath & "'!A:A,FALSE))"
.Copy
.PasteSpecial Paste:=xlPasteValues
.Replace What:="0", Replacement:="", LookAt:=xlWhole
.Replace What:="UPS", Replacement:="", LookAt:=xlPart
End With
'''''FORMAT
Cells.Font.Color = RGB(0, 0, 0)
Rows(1).Font.Color = RGB(256, 256, 256)
For j = 2 To ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
If Cells(j, 19).Text = "Cancelled" Then
ActiveSheet.Range("R" & j).EntireRow.Font.ColorIndex = 3
ActiveSheet.Range("U" & j, "V" & j).ClearContents
End If
Next
Range("T2").Select
Application.CutCopyMode = False
EndHere:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

Typically, been at this for days and as soon as I post it as a question on Stack Exchange I figure out the answer!!
Excel doesn't like doing a lookup to an external file when that file is in XLS format. This code is now snappy when I save the lookup file as an XLSX and change the reference in the variable in VBA.
Now back to the other problem of Access not wanting to export the query data as an XLSX filetype!
Edit: I added a little code to the start of the macro to open the file, save as an xlsx and close it ready for the lookup. Runs smooth as butter now :)
Sub ppmTracking()
On Error GoTo EndHere
Dim chgPath
Dim trPath
chgPath = "C:\Users\hbartles\Desktop\PPM\Tracking Report\MichPPMTracking3.xls"
trPath = "C:\Users\hbartles\Desktop\PPM\Tracking Report\[MichPPMTracking3.xlsx]MichPPMTracking3"
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Workbooks.Open Filename:=chgPath
chgPath = Replace(chgPath, "xls", "xlsx")
ActiveWorkbook.SaveAs Filename:=chgPath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close
Sheets(1).Activate
''And so on....

Related

Stop Excel from jumping to top after filtering/sorting

I had the simple problem that Excel always jumped to the top after a macro ran automatically. Whenever I did a change in any cell the macro runs. However, after finishing, Excel jumps to the top. I want to stay where I edited the cell. I know there are multiple ways to fix this. My solution was one of the following.
Here my code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Selec, LastRow, LastCol, r As Integer
Dim rng As Range
Set Selec = Range(Target.Address)
LastCol = Range("XFD1").End(xlToLeft).Column
If Selec.Row > Range("A" & Rows.Count).End(xlUp).Row Then
LastRow = Selec.Row
Cells(Selec.Row, 1).value = Application.WorksheetFunction.Max(Range(Cells(1, 1), Cells(Selec.Row - 1, 1))) + 1
With Cells(Selec.Row, 4).Validation
.Delete
.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:="=Parameters!$A$1:$A$3"
End With
Cells(Selec.Row, 4).value = "Not Started"
Else: LastRow = Range("A" & Rows.Count).End(xlUp).Row
End If
Set rng = Range(Cells(1, 1), Cells(LastRow, LastCol))
If Not Application.Intersect(rng, Selec) Is Nothing Then
For r = 2 To LastRow
Select Case Cells(r, 4)
Case "Completed"
Range(Cells(r, 1), Cells(r, LastCol)).Interior.Color = RGB(146, 208, 80)
Case "Not Started"
Range(Cells(r, 1), Cells(r, LastCol)).Interior.Color = RGB(255, 255, 255)
Case "In Progress"
Range(Cells(r, 1), Cells(r, LastCol)).Interior.Color = RGB(255, 255, 0)
End Select
If IsEmpty(Range(Cells(r, 1), Cells(r, LastCol))) = False Then
With Range("A" & r & "," & "C" & r & "," & "D" & r)
.HorizontalAlignment = xlCenter
End With
With Range("B" & r & "," & "E" & r & "," & "F" & r)
.WrapText = True
.HorizontalAlignment = xlLeft
End With
End If
Next r
End If
'Show Only In Progress and Not Started
Worksheets("Task List").Range("A1").AutoFilter Field:=4, Criteria1:=Array("In Progress", "=", "Not Started"), Operator:=xlFilterValues
'Worksheets("Task List").AutoFilter.Sort.SortFields.Add Key:=Range("C1" & Range("C" & Rows.Count).End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending
Worksheets("Task List").AutoFilter.Sort.SortFields.Add Key:=Range("C:C"), SortOn:=xlSortOnValues, Order:=xlAscending
With Worksheets("Task List").AutoFilter.Sort
.Header = xlYes
.Apply
End With
//Without one of the following lines Excel jumps to the top, but I want to stay at the end/selected cell. Uncommenting one of the following three lines solves this problem.
'Range(Target.Address).Select
'Selection.Select
'Selection.Activate
End Sub
It seems like that the jumping up is a side effect of filtering or sorting. To prevent Excel to jump up just add the following code at the end:
Target.Select
The following two lines of code work too, but are not recommended:
Selection.Select
or
Selection.Activate
All of them should prevent Excel to jump to the top and go back to current selection.
Try this good sir! This makes a temporary view (called "TempView") at the beginning of the code, then shows that view at the end and then immediately deletes this same view.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Selec, LastRow, LastCol, r As Integer
Dim rng As Range
ActiveWorkbook.CustomViews.Add ViewName:="TempView", PrintSettings:=True, _
RowColSettings:=True
Set Selec = Range(Target.Address)
LastCol = Range("XFD1").End(xlToLeft).Column
If Selec.Row > Range("A" & Rows.Count).End(xlUp).Row Then
LastRow = Selec.Row
Cells(Selec.Row, 1).Value = Application.WorksheetFunction.Max(Range(Cells(1, 1), Cells(Selec.Row - 1, 1))) + 1
With Cells(Selec.Row, 4).Validation
.Delete
.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:="=Parameters!$A$1:$A$3"
End With
Cells(Selec.Row, 4).Value = "Not Started"
Else: LastRow = Range("A" & Rows.Count).End(xlUp).Row
End If
Set rng = Range(Cells(1, 1), Cells(LastRow, LastCol))
If Not Application.Intersect(rng, Selec) Is Nothing Then
For r = 2 To LastRow
Select Case Cells(r, 4)
Case "Completed"
Range(Cells(r, 1), Cells(r, LastCol)).Interior.Color = RGB(146, 208, 80)
Case "Not Started"
Range(Cells(r, 1), Cells(r, LastCol)).Interior.Color = RGB(255, 255, 255)
Case "In Progress"
Range(Cells(r, 1), Cells(r, LastCol)).Interior.Color = RGB(255, 255, 0)
End Select
If IsEmpty(Range(Cells(r, 1), Cells(r, LastCol))) = False Then
With Range("A" & r & "," & "C" & r & "," & "D" & r)
.HorizontalAlignment = xlCenter
End With
With Range("B" & r & "," & "E" & r & "," & "F" & r)
.WrapText = True
.HorizontalAlignment = xlLeft
End With
End If
Next r
End If
'Show Only In Progress and Not Started
Worksheets("Task List").Range("A1").AutoFilter Field:=4, Criteria1:=Array("In Progress", "=", "Not Started"), Operator:=xlFilterValues
'Worksheets("Task List").AutoFilter.Sort.SortFields.Add Key:=Range("C1" & Range("C" & Rows.Count).End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending
Worksheets("Task List").AutoFilter.Sort.SortFields.Add Key:=Range("C:C"), SortOn:=xlSortOnValues, Order:=xlAscending
With Worksheets("Task List").AutoFilter.Sort
.Header = xlYes
.Apply
End With
ActiveWorkbook.CustomViews("TempView").Show
ActiveWorkbook.CustomViews("TempView").Delete
End Sub

How to paste in VBA having issues

I am having trouble pasting.
I have written the code but when it gets to cell and selects it I have tried putting paste in but still does not work.
Code below, the starred bit is the issue
Sheets("Data").Select
If Range("A2") = "" Then
Range("A1").Paste
Else
Selection.End(xlDown).Select
ActiveCell. Offset(0, 1).Select
With Range("A" & Rows.Count).End(xlUp).Offset(1)
End With
ActiveSheet.Range("$D$1:$D$50000").AutoFilter Field:=6, Criteria1:="B"
lastrow = Range("D" & Rows.Count).End(xlUp).Row
Range("D:M").SpecialCells(xlCellTypeVisible).Select
Set Rng = ActiveSheet.AutoFilter.Range
Windows("Pull Back Scans.xlsm").Activate
Sheets("Data").Select
If Range("A2") = "" Then
Range("A1").Paste
Else
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
With Range("A" & Rows.Count).End(xlUp).Offset(1)
End With
**ActiveSheet.PasteSpecial**
Columns("A:J").Select
Columns("A:J").EntireColumn.AutoFit
Windows("Belfast CDC Scans.xlsb").Activate
ActiveWorkbook.Close savechanges:=False
End If
Columns("A:J").Select
Columns("A:J").EntireColumn.AutoFit
Windows("Belfast CDC Scans.xlsb").Activate
ActiveWorkbook.Close savechanges:=False
End If

Copy pasting the cells when they are equal to another spreadsheet using Macro

I have a work question and i want my macro to do the following
i have two columns (column A and B). Column A has the names and column B contains their info.
I want my macro to find duplicate names and copy both col A and B and paste them into another spreadsheet in the following location
C:\Users\kentan\Desktop\Managed Fund
Each spreadsheet created must contain the name of that name as the file name
I have create the macro to do the following but it's not giving me the right result
Sub IRIS()
Dim i As Integer
With ActiveSheet.Sort
.SetRange Range("A:B")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlStroke
.Apply
End With
i=1
Do Until Len(Cells(i, 1).Value) = 0
If Cells(i, 1).Value = Cells(i + 1, 1).Value Then
Range(Cells(i, 1), Cells(i, 2)).Select
Selection.Copy
Workbooks.Add
Range("A1").PasteSpecial
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\kentan\Desktop\Managed Fund" & cells(i,1) & ".xls"
ActiveWorkbook.Close
Else
i = i + 1
End If
Loop
Application.CutCopyMode = False
End Sub
Given the repetitious action of adding multiple workbooks, I would shovel that operation to a 'helper' sub.
Option Explicit
Public Const strSA As String = "C:\Users\kentan\Desktop\Managed Fund "
Sub iris()
Dim i As Long
With ActiveSheet
With .Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp).Offset(0, 1))
.Sort key1:=.Columns(1), order1:=xlAscending , _
key2:=.Columns(2), order2:=xlAscending , _
Header:=xlYes, MatchCase:=False, _
Orientation:=xlTopToBottom, SortMethod:=xlStroke
End With
For i = 2 To .Rows.Count
If LCase(.Cells(i, "A").Value2) = LCase(.Cells(i - 1, "A").Value2) And _
LCase(.Cells(i, "A").Value2) <> LCase(.Cells(i + 1, "A").Value2) Then
newiris .Cells(i, "A").Value2, .Cells(i, "B").Value2
End If
Next i
End With
End Sub
Sub newiris(nm As String, nfo As String)
Application.DisplayAlerts = false
With Workbooks.Add
Do While .Worksheets.Count > 1: .Worksheets(2).Delete: Loop
.Worksheets(1).Cells(1, "A").Resize(1, 2) = Array(nm, nfo)
.SaveAs filename:=strSA & nm, FileFormat:=xlOpenXMLWorkbook
.Close savechanges:=False
End With
Application.DisplayAlerts = true
End Sub

excel VBA 2013 Compatibility issues

I have just had an upgrade from office 2010 to office 2013. The VBA scripting which I had ran perfectly fine in office 2010 with no issues, since the upgrade it now crashes when the button is clicked to run the code. I stepped through the code line by line to see what the problem was but it worked fine, didn't crash it done everything it was suppose to do.
Is this a compatibility issue? I know the scripting is correct, but it know causes excel to not respond and shut down when the button is clicked and the scripting now runs really slow where as before it was fast.
Here is the code which I am running:
Dim x As Workbook 'Saved workbook from email (MEP)
Dim y As Workbook 'Saved workbook from email (PS)
Dim sht1 As Worksheet 'Current active worksheet (Formatted)
Dim LResult As String
Dim RangeSort As Range
Dim RangeKey As Range
Dim majVarCount As Integer
Dim minVarCount As Integer
Dim onTrackCount As Integer
Dim rng As Range
Dim iVal As Integer
Dim compStartRow As Integer
Dim compEndRow As Integer
'Open Closed Project PS report
Set y = Workbooks.Open("C:\documents\Closed.xlsx") 'Path for workbook to copy from
lastRow = Cells(Rows.Count, 2).End(xlUp).Row 'Find the last row
Rows(lastRow).Delete 'Deletes un-necessary row
'Find last row again after deleting un-necessary row
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
'Copy over required data
Range("B6:E" & lastRow).Select
Selection.Hyperlinks.Delete
Selection.Copy
Workbooks("Formatted").Worksheets("Formatted").Range("E22").Insert Shift:=xlDown
Range("F6:F" & lastRow).Select
Selection.Copy
Workbooks("Formatted").Worksheets("Formatted").Range("K22").Insert Shift:=xlDown '(Global/Regional)
Range("G6:G" & lastRow).Select
Selection.Copy
Workbooks("Formatted").Worksheets("Formatted").Range("M22").Insert Shift:=xlDown
Range("H6:H" & lastRow).Select
Selection.Copy
Workbooks("Formatted").Worksheets("Formatted").Range("X22").Insert Shift:=xlDown
Range("I6:K" & lastRow).Select
Selection.Copy
Workbooks("Formatted").Worksheets("Formatted").Range("AS22").Insert Shift:=xlDown
Application.DisplayAlerts = False
y.Close
'To remove characters after the first blank space in column M
lastRow = Cells(Rows.Count, "M").End(xlUp).Row
Range("M22:M" & lastRow).Replace What:=" *", Replacement:="", LookAt:=xlPart
'Change strings from "Green = OnTrack, Amber = Minor Variance, Red = Significant Variance"
With Range("AS:AU")
.Replace What:="Green", Replacement:="On Track"
.Replace What:="Amber", Replacement:="Minor Variance"
.Replace What:="Red", Replacement:="Significant Variance"
End With
'Checking the last row
lastRow = Cells(Rows.Count, 5).End(xlUp).Row
'Used to confirm that LastRow worked displays as message
'MsgBox "Last Row: " & lastRow
'Searching for blank cells and populating with'On track'
With Range("AS22:AU" & lastRow)
.Replace What:="", Replacement:="On Track"
End With
With Range("X22:X" & lastRow)
.Replace What:="", Replacement:="Complete"
End With
'Clear contents in column DP no not needed
lastRow = Cells(Rows.Count, 5).End(xlUp).Row
Range("DP22:DP" & lastRow).ClearContents
With Worksheets("Formatted")
lastRow = .Cells(Rows.Count, 5).End(xlUp).Row
For Each rng In .Range("DP22:DP" & lastRow)
rng.Formula = "=IF(COUNTIF(AS" & rng.Row & ":AU" & rng.Row & ", ""Significant Variance""), ""Significant Variance"", " & _
"IF(COUNTIF(AS" & rng.Row & ":AU" & rng.Row & ", ""Minor Variance""), ""Minor Variance"", " & _
"""On Track""))"
rng.Value = rng.Value
Next rng
End With
'Find the range of cells for Complete Project
compStartRow = Range("X:X").Find(What:="Complete", after:=Range("X21")).Row
compEndRow = Range("X:X").Find(What:="Complete", after:=Range("X21"), SearchDirection:=xlPrevious).Row
'MsgBox "First and Last Row for Complete Projects: " & compStartRow & compEndRow 'Used for checking first and last row values are correct
'Counts the values and paste in to Count Table sheet
iVal = Application.WorksheetFunction.CountIf(Range("DP" & compStartRow & ":DP" & compEndRow), "On Track")
Worksheets("Count Table").Range("E8").Value = iVal
iVal = Application.WorksheetFunction.CountIf(Range("DP" & compStartRow & ":DP" & compEndRow), "Minor Variance")
Worksheets("Count Table").Range("D8").Value = iVal
iVal = Application.WorksheetFunction.CountIf(Range("DP" & compStartRow & ":DP" & compEndRow), "Significant Variance")
Worksheets("Count Table").Range("C8").Value = iVal
'Clear contents in column DP not needed
lastRow = Cells(Rows.Count, 5).End(xlUp).Row
Range("DP22:DP" & lastRow).ClearContents
'Copy information from the Lookup table sheet into the Formatted sheet and clears the clipboard
Sheets("Lookup Table").Range("C5").Copy Sheets("Formatted").Range("L22:L" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C4").Copy Sheets("Formatted").Range("J22:J" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C6").Copy Sheets("Formatted").Range("K22:K" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C8").Copy Sheets("Formatted").Range("Q22:Q" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C10").Copy Sheets("Formatted").Range("AQ22:AQ" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C11").Copy Sheets("Formatted").Range("AR22:AR" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C13").Copy Sheets("Formatted").Range("BD22:BD" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C15").Copy Sheets("Formatted").Range("BF22:BF" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C17").Copy Sheets("Formatted").Range("BH22:BH" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C19").Copy Sheets("Formatted").Range("BJ22:BJ" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C21").Copy Sheets("Formatted").Range("BL22:BL" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C23").Copy Sheets("Formatted").Range("BN22:BN" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C24").Copy Sheets("Formatted").Range("BO22:BO" & lastRow)
Application.CutCopyMode = False
Sheets("Lookup Table").Range("C25").Copy Sheets("Formatted").Range("BP22:BP" & lastRow)
Application.CutCopyMode = False
'Remove cell borders
Set rng = ActiveSheet.Range("E22:BP" & lastRow)
rng.Borders.LineStyle = xlNone
'Save formatted sheet as new workbook before overlay has been applied
FPath = "C:\documents\Reports\formatted\"
FName = "Formatted with Closed" & Format(Now, "ddmmmyyyy_hhmm") & ".xls"
Set NewBook = Workbooks.add
ThisWorkbook.Sheets("Formatted").Copy Before:=NewBook.Sheets(1)
NewBook.SaveAs Filename:=FPath & "\" & FName
Application.DisplayAlerts = False
NewBook.Close

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