Results 1 to 6 of 6

loop range in sheet 3 to find value and copy offset cell to sheet 2

Threaded View

  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    loop range in sheet 3 to find value and copy offset cell to sheet 2

    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
    Last edited by Sc0tt1e; 06-27-2017 at 09:30 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy range to new sheet and offset each cell in range
    By Groovicles in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2017, 09:21 AM
  2. Copy range of cells from sheet to another sheet using offset
    By Biplab1985 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-29-2016, 04:56 PM
  3. Copy range of cells from sheet to another sheet using offset
    By Biplab1985 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-29-2016, 01:57 PM
  4. Loop to find a cell, then copy offset paste
    By mr.alexander in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-29-2013, 08:48 AM
  5. Code To Find and Offset works on first sheet but won't loop to other sheets
    By mgaworecki in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-10-2009, 03:02 PM
  6. Loop Through A Range, Find Cells > 1, Copy Offset To Another Sheet
    By bugmenot in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-25-2008, 06:30 PM
  7. From cell in sheet 2 find name in sheet 1 and value offset
    By Frank345 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 10-05-2008, 01:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1