Excel ignoring code that works in other workbooks - excel

I have code that runs in several other workbooks but seems to be ignored in a specific one.
The only difference I can see between the ones that work and the one that doesn't, is a line that has SaveAs Filename: vs SaveAs FileName:. Somehow I cannot imagine that would cause the whole script to be ignored but??
The other thing is when I attempt to change the code from Filename to FileName, excel changes it back as soon as I go to the next line.
Corrupt file?
Apologies for the sloppy code... :(
```Sub Create_Individual_Files()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Dim i As Integer
Dim x As String
Dim Lastrow As Long
Dim NewBook As Workbook
Dim Sourcewb As Workbook: Set Sourcewb = ThisWorkbook
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim FName As String
Dim Fpath As String
Dim FName2 As String
Fpath = Sheets("Variables").Range("B1").Text
FName = Sheets("Variables").Range("B9").Text
FName2 = Sheets("Variables").Range("B2").Text
'Find the last row of data in each tab.
Lastrow = Sourcewb.Sheets(1).Cells(Sourcewb.Sheets(1).Rows.Count, "A").End(xlUp).Row
'This section creates each new file, retaining all formulas, from the existing tabs in the master workbook. Then saves the file with the individuals name.
For i = 2 To Lastrow
x = Sourcewb.Sheets(1).Range("A" & i).Value
Sourcewb.Sheets(Array("Summary", "Pivot", "Data", "Modifier %", "Modifier Dollar", "Variables")).Copy
Set NewBook = ActiveWorkbook
FileExtStr = ".xlsx": FileFormatNum = 51
With NewBook
.SaveAs Filename:=Fpath & FName2 & "-" & x & FName & ".xlsx"
'.Close False
End With
''''''''''''''''''''''''''''''''''''''''''''
'The section below deletes data from each tab that is not specific to the individual.
Sheets("Variables").Select
' Range("C5").Select
' Selection.Copy
Range("B10").Select
ActiveCell.FormulaR1C1 = x
' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
Sheets("Summary").Select
Call Loop_Delete_Summary
Sheets("Data").Select
ActiveSheet.ListObjects(1).Unlist
Call Loop_Delete_Summary
Sheets("Modifier %").Select
ActiveWorkbook.ActiveSheet.ListObjects(1).Unlist
Call Delete_Modifier_Percent
Sheets("Modifier Dollar").Select
ActiveSheet.ListObjects(1).Unlist
Call Loop_Delete_Summary
'Sheets("Controls").Select
Sheets("Variables").Select
ActiveWindow.SelectedSheets.Delete
Call ResetCursor
''''''''''''''''''''''''''''''''''''''''''''
Next i
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
End Sub```
```Sub Loop_Delete_Summary()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim rng As Range
With ActiveSheet
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
'Set the first and last row to loop through
Firstrow = .UsedRange.Offset(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'Loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1
'Check the values in the A column
With .Cells(Lrow, "A")
If Not IsError(.Value) Then
If .Value <> Worksheets("Variables").Range("B10") Then
If rng Is Nothing Then
Set rng = .Cells
Else
Set rng = Application.Union(rng, .Cells)
End If
End If
End If
End With
Next Lrow
End With
'Delete all rows at once
If Not rng Is Nothing Then rng.EntireRow.Delete
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub```
```Sub Loop_Delete_Modifier_Percent()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With ActiveSheet
'We select the sheet so we can change the window view
.Select
'If you are in Page Break Preview Or Page Layout view go
'back to normal view, we do this for speed
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
'Turn off Page Breaks, we do this for speed
.DisplayPageBreaks = False
'Set the first and last row to loop through
Firstrow = 2
Lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1
'We check the values in the A column in this example
With .Cells(Lrow, "B")
If Not IsError(.Value) Then
Select Case .Value
Case Is <> Worksheets("Variables").Range("B10").Value: .EntireRow.Delete
End Select
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub```
*****The code when using F8 to step through seems to skip over this section...*****
``` x = Sourcewb.Sheets(1).Range("A" & i).Value
Sourcewb.Sheets(Array("Summary", "Pivot", "Data", "Modifier %", "Modifier Dollar", "Variables")).Copy
Set NewBook = ActiveWorkbook
FileExtStr = ".xlsx": FileFormatNum = 51
With NewBook
.SaveAs Filename:=Fpath & FName2 & "-" & x & FName & ".xlsx"
'.Close False
End With
Sheets("Variables").Select
' Range("C5").Select
' Selection.Copy
Range("B10").Select
ActiveCell.FormulaR1C1 = x
' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
Sheets("Summary").Select
Call Loop_Delete_Summary
Sheets("Data").Select
ActiveSheet.ListObjects(1).Unlist
Call Loop_Delete_Summary
Sheets("Modifier %").Select
ActiveWorkbook.ActiveSheet.ListObjects(1).Unlist
Call Delete_Modifier_Percent
Sheets("Modifier Dollar").Select
ActiveSheet.ListObjects(1).Unlist
Call Loop_Delete_Summary
'Sheets("Controls").Select
Sheets("Variables").Select
ActiveWindow.SelectedSheets.Delete
Call ResetCursor
Next i```

It appears that the order of the tabs was the issue. I noticed that Lastrow = Sourcewb.Sheets(1).Cells(Sourcewb.Sheets(1).Rows.Count, "A").End(xlUp).Row
Lastrow = 0.
I moved the front 2 tabs which had no data to the end of the tabs and it works.
Otherwise, for whatever reason, it would find zero as the last row and simply skip to the end.

Related

Excel VBA will not close my opened workbook

I am pretty much new to VBA and have been trying to learn, with this I have created a code that opens another work book and combines all data to a database file and then copy this to my current open file, the problem I have is that it will not close the workbook and takes a long time doing so. Any ideas please?.
'''
Option Explicit
Sub GasStockReport()
Dim wb As String
Dim st As String
Dim path As String
path = "C:\Users\si2066\OneDrive - ENGIE\Desktop\MP Templates\MP - Stock Control\"
wb = "Gas Stock Take v2"
Workbooks.Open path & wb
Dim sh As Worksheet
Dim destsh As Worksheet
Dim Last As Long
Dim CopyRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Database").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Set destsh = ActiveWorkbook.Worksheets.Add
destsh.Name = "Database"
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> destsh.Name Then
Last = GetLastRow(destsh, 1)
With sh
Set CopyRng = sh.Range("A2:K" & GetLastRow(sh, 1))
End With
If Last + CopyRng.Rows.Count > destsh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
Else
CopyRng.Copy IIf(Last = 1, destsh.Cells(1, "b"), destsh.Cells(Last + 1, "b"))
End If
If Last = 1 Then
destsh.Cells(Last, "A").Resize(CopyRng.Rows.Count).Value = sh.Name
Else
destsh.Cells(Last + 1, "A").Resize(CopyRng.Rows.Count).Value = sh.Name
End If
End If
Next
ExitTheSub:
Application.Goto destsh.Cells(1)
destsh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Range("A1").Select
Range(Selection, ActiveCell.End(xlDown).End(xlToRight)).Select
Selection.Copy
ActiveWindow.SelectedSheets.Visible = False
ThisWorkbook.Sheets("Stock History").Activate
Range("N1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.DisplayAlerts = False
On Error Resume Next
Workbooks("Gas Stock Take v2").Close SaveChanges:=True
On Error GoTo 0
Application.DisplayAlerts = True
ThisWorkbook.Sheets("Stock Take").Activate
Call Click
End Sub
Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1)
As Long
With ws
GetLastRow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row
End With
End Function
'''
Your error handling is fine. Using On Error GoTo 0 turns off On Error Resume Next.
I think the issue is the amount of data you're leaving on the clipboard after copy and paste. If you use .copy and .paste always follow that up with Application.CutCopyMode = False

How to create dynamic ranges and Improving vba Code

I Have a little bit of code (below) that I would like to improve as I find it a little clunky with regards to the ranges as they vary daily, Ideally I would like to use last row instead of using a massive range but, sadly I am not that clever :(
this is the code, if anyone fancies taking a look to improve on I would be greatly appreciative, I think the process is self explanatory (i.e auto filter and copying from one sheet to another)
Sub Refresh_click()
Set DbExtract = ThisWorkbook.Sheets("Sheet1")
Set DuplicateRecords = ThisWorkbook.Sheets("Sheet2")
Sheets("Sheet2").Unprotect
Range("A4:A50").Select
Application.CutCopyMode = False
Selection.ClearContents
Sheets("Sheet1").Select
ActiveSheet.ListObjects("Table22").Range.AutoFilter Field:=23, Criteria1:= _
"="
DbExtract.Range("F2:F99999").SpecialCells(xlCellTypeVisible).Copy
DuplicateRecords.Range("A4:A50").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Protect
Sheets("Sheet1").Select
ActiveSheet.ListObjects("Table22").Range.AutoFilter Field:=23
MsgBox "Log - Updated"
End Sub
Try in this way, please:
Sub Refresh_click()
Dim DbExtract As Worksheet, DuplicateRecords As Worksheet, lastFRow As Long
Set DbExtract = ThisWorkbook.Sheets("Sheet1")
Set DuplicateRecords = ThisWorkbook.Sheets("Sheet2")
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
.CutCopyMode = False
End With
With DuplicateRecords
.Unprotect
.Range("A4:A50").ClearContents
End With
DbExtract.ListObjects("Table22").Range.AutoFilter field:=23, Criteria1:="="
lastFRow = DbExtract.Range("F" & rows.count).End(xlUp).row 'last row of F:F col
DbExtract.Range("F2:F" & lastFRow).SpecialCells(xlCellTypeVisible).Copy
DuplicateRecords.Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
DuplicateRecords.Protect
DbExtract.ListObjects("Table22").Range.AutoFilter field:=23
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
MsgBox "Log - Updated"
End Sub
Please, test it and send some feedback. I couldn't test it...
Copy Using AutoFilter
Adjust the values in the constants section.
The Code
Option Explicit
Sub Refresh_click()
Const srcName As String = "Sheet1"
Const srcTblName As String = "Table22"
Const srcCol As Long = 6
Const srcField As Long = 23
Const srcCrit As String = "="
Const dstName As String = "Sheet2"
Const dstFirst As String = "A4"
Dim wb As Workbook: Set wb = ThisWorkbook
Dim cel As Range
Dim rng As Range
Dim Updated As Boolean
With wb.Worksheets(dstName)
.Unprotect
Set cel = .Range(dstFirst)
cel.Resize(cel.Worksheet.Rows.Count - cel.Row + 1).ClearContents
With wb.Worksheets(srcName).ListObjects(srcTblName)
.Range.AutoFilter
Set rng = .ListColumns(srcCol).Range _
.Resize(.ListRows.Count).Offset(1)
'Debug.Print rng.Address
.Range.AutoFilter Field:=srcField, Criteria1:=srcCrit
On Error Resume Next
rng.SpecialCells(xlCellTypeVisible).Copy
On Error GoTo 0
If Application.CutCopyMode = xlCopy Then
cel.PasteSpecial xlPasteValues
Updated = True
End If
.Range.AutoFilter
End With
.Protect
End With
If Updated Then
MsgBox "Log updated.", vbInformation, "Success"
Else
MsgBox "Log not updated.", vbCritical, "Fail"
End If
End Sub

Split data into multiple worksheets using MULTIPLE column filters

There are answers to this question using a single filter. BUT How do you split a worksheet into multiple worksheets based off of more than 1 filter (column). I have this worksheet below.
Name Age Branch Section Dept
Bob 20 1 2 A
Bill 20 1 2 A
Jill 20 1 2 B
Jane 20 1 3 A
Paul 20 2 3 B
Tom 20 2 3 B
I want to split this into multiple worksheets based off of 3 columns (Branch, Section, Dept). The results should look like this:
Name Age Branch Section Dept
Bob 20 1 2 A
Bill 20 1 2 A
Name Age Branch Section Dept
Jill 20 1 2 B
Name Age Branch Section Dept
Jane 20 1 3 A
Name Age Branch Section Dept
Paul 20 2 3 B
Tom 20 2 3 B
How would I write a VBA Excel macro to do this?
Also each worksheet should be named "BRANCH" # & "SECTION" # & "DEPT" letter. (e.g. BRANCH1SECTION2DEPTA)
Currently, I have this VBA code that can do this filtering for 1 column.
Sub SplitandFilterSheet()
'Step 1 - Name your ranges and Copy sheet
'Step 2 - Filter by Department and delete rows not applicable
'Step 3 - Loop until the end of the list
Dim Splitcode As Range
Sheets("Master").Select
Set Splitcode = Range("Splitcode")
For Each cell In Splitcode
Sheets("Master").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.Name = cell.Value
With ActiveWorkbook.Sheets(cell.Value).Range("MasterData")
.AutoFilter Field:=6, Criteria1:="NOT EQUAL TO" & cell.Value, Operator:=xlFilterValues
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
ActiveSheet.AutoFilter.ShowAllData
Next cell
End Sub
I just hacked this together. It seems to do what you described. Notice, I copied the data from C1:E7 and pasted it into AA1, then clicked Data > Remove Duplicates. You can record a Macro to do this and add it to the code, towards the top.
Sub Copy_To_Worksheets()
Dim My_Range As Range
Dim FieldNum As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim ws2 As Worksheet
Dim Lrow As Long
Dim cell As Range
Dim CCount As Long
Dim WSNew As Worksheet
Dim ErrNum As Long
Set My_Range = Range("A1:E" & LastRow(ActiveSheet))
My_Range.Parent.Select
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Add a worksheet to copy the a unique list and add the CriteriaRange
Set ws = Worksheets("Data")
With ws
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
'For Each cell In .Range("A2:A" & Lrow)
For Each c In Range("AA2:AA5")
'Filter the range
My_Range.AutoFilter Field:=3, Criteria1:="=" & c.Value
My_Range.AutoFilter Field:=4, Criteria1:="=" & c.Offset(0, 1).Value
My_Range.AutoFilter Field:=5, Criteria1:="=" & c.Offset(0, 2).Value
Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
WSNew.Name = "Branch" & c.Value & "Section" & c.Offset(0, 1).Value & "Dept" & c.Offset(0, 2).Value
On Error GoTo 0
'Copy the visible data to the new worksheet
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
Next c
'Next cell
'Delete the ws2 sheet
On Error Resume Next
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End With
'Turn off AutoFilter
'My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
'Restore ScreenUpdating, Calculation, EnableEvents, ....
'My_Range.Parent.Select
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Before:
After:
I am adding some modified code below, to address your last question. Use the code below, and keep the Function named 'LastRow'.
Sub TryThis()
Dim My_Range As Range
Dim FieldNum As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim ws2 As Worksheet
Dim Lrow As Long
Dim cell As Range
Dim CCount As Long
Dim WSNew As Worksheet
Dim ErrNum As Long
Set My_Range = Range("A1:E" & LastRow(ActiveSheet))
My_Range.Parent.Select
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Add a worksheet to copy the a unique list and add the CriteriaRange
Set ws = Worksheets("Data")
With ws
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
For Each c In Range("AA2:AA5")
'Filter the range
My_Range.AutoFilter Field:=3, Criteria1:="=" & c.Value
My_Range.AutoFilter Field:=4, Criteria1:="=" & c.Offset(0, 1).Value
My_Range.AutoFilter Field:=5, Criteria1:="=" & c.Offset(0, 2).Value
Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
WSNew.Name = "Branch" & c.Value & "Section" & c.Offset(0, 1).Value & "Dept" & c.Offset(0, 2).Value
On Error GoTo 0
'Copy the visible data to the new worksheet
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
Columns("C:E").Select
Selection.ClearContents
Next c
End With
'Turn off AutoFilter
'My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
'Restore ScreenUpdating, Calculation, EnableEvents, ....
'My_Range.Parent.Select
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub

Past value in vba

I am trying to select the column that contains SUM formula. and I want to copy the formula and past only the value in the same column. but this code does not change the formula into the value. any idea how i could solve this?
Sub Registrereren()
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False
On Error Resume Next
Dim oWkSht As Worksheet
Dim LastColumn As Long
Dim c As Date
Dim myCell As Range
Dim LastRow As Long
Sheets("Registration").Activate
Set oWkSht = ThisWorkbook.Sheets("Registration")
LastColumn = oWkSht.Range("A" & Columns.Count).End(xlToRight).Column
LastRow = oWkSht.Range("C" & Rows.Count).End(xlUp).Row
c = Date
Set myCell = oWkSht.Range("1:1").Find(What:=c, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False, SearchOrder:=xlByColumns)
If Not myCell Is Nothing Then
myCell.Offset(1, 0).Formula = "=New_Order!N2+New_Order!O2+New_Order!P2"
Range(myCell.Offset(1), Cells(LastRow, myCell.Column)).Select
Selection.FillDown
Range(myCell.Offset(1), LastRow).Select
Selection.Copy
Range(myCell.Offset(1), LastRow).PasteSpecial xlPasteValues
End If
Sheets("Main").Activate
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Try this. LastRow is not a valid range as it is only a row number.
Sub Registrereren()
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim oWkSht As Worksheet
Dim LastColumn As Long
Dim c As Date
Dim myCell As Range
Dim LastRow As Long
Set oWkSht = ThisWorkbook.Sheets("Registration")
LastColumn = oWkSht.Range("A" & Columns.Count).End(xlToRight).Column
LastRow = oWkSht.Range("C" & Rows.Count).End(xlUp).Row
c = Date
Set myCell = oWkSht.Range("1:1").Find(What:=c, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False, SearchOrder:=xlByColumns)
If Not myCell Is Nothing Then
With oWkSht.Range(myCell.Offset(1), oWkSht.Cells(LastRow, myCell.Column))
.Formula = "=New_Order!N2+New_Order!O2+New_Order!P2"
.Value = .Value
End With
End If
Sheets("Main").Activate
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Consolidation workbook macro not copying just the range and not pasting to end of list

I have a macro that takes information from one workbook and places in another workbook. It works fine except when I go to another workbook and use it to move items over it pastes over the previous information instead of placing at the bottom of the list. Also it does not seem to recognize my range and brings information over from below my range. I am at this time adding this macro to each workbook I need information from if that makes a difference.
Sub Copy_With_AutoFilter()
Dim sName As String
Dim My_Range As Range
Dim wsMASTER As Worksheet
Dim shtName As Worksheet
Dim CalcMode As Long
Dim ViewMode As Long
Dim FilterCriteria As String
Dim CCount As Long
Dim rng As Range
Dim wbTarget As Workbook
Dim wbSource As Workbook
'Unprotect Sheet
ActiveSheet.Unprotect
'Set filter range on ActiveSheet
Set My_Range = Range("A94:E119")
'Set the sheet
Set wbSource = ThisWorkbook
Set wsMASTER = wbSource.Worksheets("MASTER")
'Set the destination worksheet
Set wbTarget = Workbooks.Open("A:\Accounting\Manifest Project\Manifest\2014\" & _
"Completion Bonus\Summer Bonus.xlsx")
sName = wsMASTER.Range("A1").Value
On Error Resume Next
Set shtName = wbTarget.Worksheets(sName)
On Error GoTo 0
If shtName Is Nothing Then
MsgBox "Sheet was not found in target workbook!"
Exit Sub
End If
'Change ScreenUpdating, Calculation, EnableEvents, .....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Firstly, remove the AutoFilter
My_Range.Parent.AutoFilterMode = False
'Filter and set the filter field and the filter criteria
My_Range.AutoFilter Field:=1, Criteria1:=">0"
'Copy the visible data and use PasteSpecial to paste to the shtName
With My_Range.Parent.AutoFilter.Range
On Error Resume Next
'Set rng to the visible cells in My_Range without the header row
Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then
'Copy and paste the cells into shtName below the existing data
rng.Copy
With shtName.Range("A1" & LastRow(shtName) + 1)
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
End With
'Protect workbook
wbSource.Protect
'Close AutoFilter
My_Range.Parent.AutoFilterMode = False
'Restore ScreenUpdating, Calculation, EnableEvents, ....
ActiveWindow.View = ViewMode
Application.Goto shtName.Range("A1")
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
So these are the lines where you paste:
With shtName.Range("A1" & LastRow(shtName) + 1)
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
It should be (I think):
With shtName.Range("A" & (LastRow(shtName) + 1))
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
You told it to paste in "A1." By removing that you will allow it to past in the cell I believe you intended.

Resources