I am having trouble trying to set up some code to carry out the following task.
I would like to loop through a range in sheet 2 column G and if the cell I am currently on is less than the predetermined AVAge variable I want the value in column A (same row) to be copied to the last empty row in Sheet 2 column A.
I am struggling with the jumping from one sheet to another and having to reset the variables for last row in sheet 2 after each paste. The below is the latest unsuccessful attempt. If anyone can suggest anything to solve this issue I would be greatly appreciative.
' Determines if cell in Sheet 3's Profit column (G) is greater than APAve and then copies column A data for that row to Sheet 2 column A's last empty row
With ThisWorkbook.Sheets(3)
For MyCells = LRows To 2 Step -1
If .Cells(MyCells, 7) < APAve Then
With ThisWorkbook.Sheets(2)
PRow = .Range("A" & .Rows.Count).End(xlUp).row + 1
End With
PRow.Value = Application.WorksheetFunction.Offset(MyCells, 0, -6).Value2
Next MyCells
End With
The complete code is below incase you were confused about data types or previously declared values
Option Explicit
Dim FName As String, SaveFileName As String
Dim W As Workbook
Dim CTR As Integer, Visits As Integer
Dim LRow As Long, LRows As Long, MyCell As Long, MyCells As Long, PRow As Long
Dim MyRange As Range, MyRanges As Range, PasteRange As Range
Dim APAve As Single
Private Sub CommandButton1_Click()
'UserForm1.TextBox1.Value = Format(UserForm1.TextBox1.Value, "#.###")
APAve = Format(APAve, "#.##0")
' Sets the name & location of the file to extract Custom Variables from
FName = Application.GetOpenFilename
CTR = UserForm1.TextBox1.Value
Visits = UserForm1.TextBox2.Value
Application.ScreenUpdating = False
Application.DisplayAlerts = False
UserForm1.Hide
' Opens Workbook and copies data to spreadsheet and closes workbook
Set W = Workbooks.Open(FName)
W.Sheets(1).Copy after:=ThisWorkbook.Sheets(2)
W.Close False
' Delete entire row if Profit is greater than 0
With ThisWorkbook.Sheets(3)
LRow = .Range("G" & .Rows.Count).End(xlUp).row
For MyCell = LRow To 2 Step -1
If .Cells(MyCell, 7) > 0 Then .Rows(MyCell).EntireRow.Delete
Next MyCell
' Finds last row and averages row based on cells with a value greater than 0
LRows = .Range("G" & .Rows.Count).End(xlUp).row
APAve = Application.WorksheetFunction.AverageIf(Range("M2:M" & LRows), ">0")
End With
' Determines if cell in Sheet 3's Profit column (G) is greater than APAve and then copies column A data for that row to Sheet 2 column A's last empty row
With ThisWorkbook.Sheets(3)
For MyCells = LRows To 2 Step -1
If .Cells(MyCells, 7) < APAve Then
With ThisWorkbook.Sheets(2)
PRow = .Range("A" & .Rows.Count).End(xlUp).row + 1
End With
PRow.Value = Application.WorksheetFunction.Offset(MyCells, 0, -6).Value2
Next MyCells
End With
'SaveFileName = ThisWorkbook.Sheets(2).Range("J3").Value & "\" & FName & "_Output.xlsm"
'ThisWorkbook.Sheets(3).Delete
'ThisWorkbook.SaveAs SaveFileName
'ThisWorkbook.Close
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Work Complete"
End Sub
Bookmarks