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
Related
I have a problem with my VBA code. The problem is that I have duplicate names - the main sheet "Manager" and the names of the sheets.
The code should go to every sheet and look for the value "Engagements ID" and then go one cell down. In every sheet the number of Engagements ID is different, so the code should search in every sheet (500 rows) - look for the value "Engagements ID" then copy and paste the cell what is one row below into my main sheet, which is called "Manager".
Thank you for help!! :) The value what I looking for is on every sheet in column B.
This is my code:
Option Explicit
Sub Check_Account()
Dim rng As Range
Dim xName As String
Dim i, j As Integer
For i = 3 To 6
xName = Cells(i, 1)
If xName = "" Then Exit Sub
On Error Resume Next
ActiveWorkbook.Sheets(xName).Select
Sheets(xName).Select
For j = 1 To 500
If rng.Cells(j, 2) = "Engagements ID" Then
rng.Offset(1, 0).Select
Selection.Copy
Sheets("Manager").Select
If Range("B" & i) = "" Then
Range("B" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWorkbook.Sheets(xName).Select
Sheets(xName).Select
Cells(j, 2).Offset(1, 0).Select
Else
Range("B" & i).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWorkbook.Sheets(xName).Select
Sheets(xName).Select
Cells(j, 2).Offset(1, 0).Select
End If
End If
Next j
On Error GoTo 0
Next i
End Sub
Please try this code. I think you will like it.
Option Explicit
Sub Check_Account()
' 24 Nov 2017
Dim TabName As String
Dim Rng As Range
Dim Fnd As Range
Dim Rl As Long ' last row
Dim FirstFnd As Long
Dim i As Integer
For i = 3 To 6
' Tab names are found at Manager!A3:A6
TabName = Worksheets("Manager").Cells(i, "A").Value
If Len(TabName) = 0 Then Exit For
On Error Resume Next
With Worksheets(TabName)
If Err Then
MsgBox "Worksheet """ & TabName & """ doesn't exist.", _
vbInformation, "Missing Worksheet"
Else
Rl = .Cells(.Rows.Count, "B").End(xlUp).Row
Set Rng = Range(.Cells(1, "B"), .Cells(Rl, "B"))
Set Fnd = Rng.Find("Engagements ID", _
After:=Rng.Cells(Rng.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
MatchByte:=False)
If Not Fnd Is Nothing Then
FirstFnd = Fnd.Row
Do
With Worksheets("Manager")
Rl = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
' start writing in row 2
If Rl < 2 Then Rl = 2
.Cells(Rl, "B").Value = Fnd.Offset(1).Value
End With
Set Fnd = Rng.FindNext(Fnd)
Loop While Not Fnd Is Nothing And Fnd.Row <> FirstFnd
End If
End If
End With
Next i
End Sub
I have tried and tested the code below, and I believe it does what you expected it to do:
Sub foo()
For i = 3 To 6
xName = Sheets("Manager").Cells(i, 1).Value
LastRow = Sheets(xName).Cells(Sheets(xName).Rows.Count, "B").End(xlUp).Row
For x = 1 To LastRow
If Sheets(xName).Cells(x, 2).Value = "Engagements ID" Then
Sheets("Manager").Cells(i, 2).Value = Sheets(xName).Cells(x + 1, 2).Value
End If
Next x
Next i
End Sub
This does not have any validation against possible errors, if the manager sheet does not exist, then you will get an error... But at least the code is more concise and it points you in the right direction.
Hi I have the following VBA code and it fails at one place time and again.
Sub theathersplitmacro()
Dim SDrv As String
Dim DDrv As String
Dim Sfname As String
Dim Dfname As String
Dim wkbSrc As Workbook
Dim wkbDst As Workbook
Dim shtname(1 To 16) As Variant
Dim i As Integer
Dim Lastrow As Variant
Dim destination_file As String
'Dim regions As String
Dim theater As String
Dim j As Integer
For j = 2 To 9
destination_file = Workbooks("VBA Master
Copy.xlsb").Sheets("Data").Range("A" & j).Value & ".xlsb"
'regions = Workbooks("VBA Master Copy.xlsb").Sheets("Data").Range("C" &
j).Value
theater = Workbooks("VBA Master Copy.xlsb").Sheets("Data").Range("D" &
j).Value
shtname(1) = "DataQTR"
shtname(2) = "DataSWDriver"
shtname(3) = "DataMTD"
shtname(4) = "DataWeekly"
shtname(5) = "DataSoftware"
shtname(6) = "DataCloud"
shtname(7) = "DataServices"
shtname(8) = "TopCustomer"
shtname(9) = "TopDeals"
shtname(10) = "TopPartners"
shtname(11) = "DataForecast"
shtname(12) = "DataFcstCloud"
shtname(13) = "DataFcstSoftware"
shtname(14) = "DataFcstServices"
shtname(15) = "DataServicesSW"
shtname(16) = "TopCustomerDebooking"
SDrv = "C:\Users\skumawat\Documents\Explore\"
Sfname = "Theater_Bookings - New Format with formulae.xlsb"
DDrv = "C:\Users\skumawat\Documents\Explore\"
Dfname = destination_file
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
.Calculation = xlCalculationManual
End With
Set wkbSrc = Workbooks.Open(SDrv & Sfname)
Set wkbDst = Workbooks.Open(DDrv & Dfname)
For i = 1 To 15
wkbSrc.Worksheets(shtname(i)).Activate
Lastrow = wkbSrc.Worksheets(shtname(i)).Range("k" &
Rows.Count).End(xlUp).Row
wkbSrc.Worksheets(shtname(i)).Range("A1:BZ" & Lastrow).Select
If Worksheets(shtname(i)).AutoFilterMode = True Then
wkbSrc.Worksheets(shtname(i)).AutoFilter.Sort.SortFields.Clear
End If
wkbSrc.Worksheets(shtname(i)).Range("A1:BZ" & Lastrow).Select
Selection.AutoFilter
wkbSrc.Worksheets(shtname(i)).Range("$k$1:$k$" & Lastrow).AutoFilter
Field:=11, Criteria1:=theater
Range("$A$1:$BZ$" & Lastrow).SpecialCells(xlCellTypeVisible).Copy
wkbDst.Worksheets(shtname(i)).Range("A1").PasteSpecial
xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
Next i
wkbDst.Worksheets("aMapping").Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Workbooks("VBA Master Copy.xlsb").Sheets("Data").Activate
Range("G" & j).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
wkbDst.Worksheets("aMapping").Activate
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks
_
:=False, Transpose:=True
Application.CutCopyMode = False
With wkbDst
.Save
.Close
End With
Workbooks("VBA Master Copy.xlsb").Sheets("Data").Range("E" & j).Value =
"Completed"
Next j
With wkbSrc
.Close
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
End With
Workbooks("VBA Master Copy.xlsb").Activate
End Sub
The error I get is in the following line
wkbSrc.Worksheets(shtname(i)).Range("$k$1:$k$" & Lastrow).AutoFilter
Field:=11, Criteria1:=theater
You use a wrong range. To set the autofilter user your range "A1:BZ" & lastrow again. If you use only $K you have no column 11 for your criteria.
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)
I need the macro to open wkbk(B) goto row (??) based value entered in wkbk(A) copy certain colmns and paste back to col (j14) in wkbk (A).
Sub AutofillData()
Dim wkbkSource As Workbook
Dim strPath As String
Dim myRange As Range
Dim i As Integer
Dim c As Range
Application.ScreenUpdating = False
strPath = "\\"
Set wkbkSource = Workbooks.Open(strPath & Range("A13").Value & ".xls?")
Windows("Book1.xlsm").Activate
Set myRange = Range("i14:i25")
For Each c In myRange
i = c.Value
wkbkSource.Activate
Worksheets("Main Data").Select
Range("D" & i & ":O" & i).Select
Selection.Copy
Windows("Book1.xlsm").Activate
Range("J14").Select
Sheets("Data").Cells(Rows.Count, 9).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Range("J14").Select
Application.CutCopyMode = False
Next
wkbkSource.Close savechanges:=False
Application.ScreenUpdating = True
End Sub
This will do it
Sub AutofillData()
Dim wkbkSource As Workbook
Dim strPath As String
Dim myRange As Range
Dim i As Integer
Dim c As Range
Dim wkbkTarget As Workbook
Application.ScreenUpdating = False
strPath = "C:\temp\"
Set wkbkA = ThisWorkbook
Set wkbkB = Workbooks.Open(strPath & Range("A13").Value & ".xlsx")
Set myRange = wkbkA.Sheets("Sheet2").Range("i14:i25")
offs = 0
For Each c In myRange
i = c.Value
wkbkB.Worksheets("Main Data").Range("D" & i & ":O" & i).Copy
wkbkA.Sheets("Data").Range("J14").Offset(offs, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
offs = offs + 1
Next
wkbkB.Close savechanges:=False
Application.ScreenUpdating = True
End Sub
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