Results 1 to 9 of 9

Excel VBA Code adapt paste location based on variable in reference cell

Threaded View

  1. #1
    Registered User
    Join Date
    12-16-2019
    Location
    Colchester,England
    MS-Off Ver
    2013
    Posts
    18

    Excel VBA Code adapt paste location based on variable in reference cell

    Hi, I previously posted this question in another thread. At the suggestion of the user attempting to answer the question I have reviewed the way the way I am asking the question (as there was some confusion) and started fresh here.

    I have a workbook, in which the first sheet will be raw data, and then a sheet which will perform a series of calculations on data entered into it. There might be several dozen copies of this second sheet (varying from project to project), always performing the same calculations, but on different parts of the raw data. I need to copy in the sections of raw data to each sheet to calculate my various results. At the moment I have to search through my data, which could be hundreds or even thousands of rows long, highlight and copy the section I need to the relevant area of the relent sheet. Iam trying to write a VBA to speed this process up.

    I have attached an example workbook. This is simplified in terms of the amount of data, the calculations which the successive sheets are performing and the exact locations of the elements but the principle is the same.

    I would like to be able to enter two variables on a sheet. One to select the file number of the raw data, and one to set the location to paste. Hopefully the following images show this idea
    Enter variable sheet2 (the active sheet in this example):
    Variable.JPG

    Source data to be copied based on variable in cell A1 sheet1 (the raw data)
    Source.JPG

    Pasted data in sheet2 (the active sheet in this example)
    Outcome.JPG

    To populate each location in a given calculation sheet I would re-enter the variable and re-run the macro (assigned to a keyboard shortcut). It does not have to copy to different locations or different sheets at the same time.

    At the moment I have the following code:
    Sub FindIt()
    
    Application.ScreenUpdating = False
    
           Dim lRow As Long
           Dim aValue As Range
    
    lRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
    
    For Each Cell In Sheet1.Range("A1:A" & lRow)
             Set aValue = ActiveSheet.Columns("B:B").Find(Cell.Value)
               If aValue Is Nothing Then GoTo NextCell
               If Cell.Value = aValue.Value Then
               Cell.Offset(0, 3).Copy
               ActiveSheet.Range("E4").PasteSpecial xlPasteValues
               Cell.Offset(0, 4).Copy
               ActiveSheet.Range("E5").PasteSpecial xlPasteValues
               Cell.Offset(0, 5).Copy
               ActiveSheet.Range("E6").PasteSpecial xlPasteValues
               Cell.Offset(0, 6).Copy
               ActiveSheet.Range("E7").PasteSpecial xlPasteValues
               Cell.Offset(1, 3).Copy
               ActiveSheet.Range("F4").PasteSpecial xlPasteValues
               Cell.Offset(1, 4).Copy
               ActiveSheet.Range("F5").PasteSpecial xlPasteValues
               Cell.Offset(1, 5).Copy
               ActiveSheet.Range("F6").PasteSpecial xlPasteValues
               Cell.Offset(1, 6).Copy
               ActiveSheet.Range("F7").PasteSpecial xlPasteValues
               Cell.Offset(2, 3).Copy
               ActiveSheet.Range("G4").PasteSpecial xlPasteValues
               Cell.Offset(2, 4).Copy
               ActiveSheet.Range("G5").PasteSpecial xlPasteValues
               Cell.Offset(2, 5).Copy
               ActiveSheet.Range("G6").PasteSpecial xlPasteValues
               Cell.Offset(2, 6).Copy
               ActiveSheet.Range("G7").PasteSpecial xlPasteValues
               Cell.Offset(3, 3).Copy
               ActiveSheet.Range("H4").PasteSpecial xlPasteValues
               Cell.Offset(3, 4).Copy
               ActiveSheet.Range("H5").PasteSpecial xlPasteValues
               Cell.Offset(3, 5).Copy
               ActiveSheet.Range("H6").PasteSpecial xlPasteValues
               Cell.Offset(3, 6).Copy
               ActiveSheet.Range("H7").PasteSpecial xlPasteValues
               
                       
    End If
    
    NextCell:
    Next Cell
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
    End Sub
    This selects a range of values from the raw data page sheet1 (a 4x4 grid), based on the file number in column A being matched to the value of cell A1 in the activesheet (the first two columns are ignored using the offset), and copies it to a fixed grid of cells in whatever the active sheet is.

    What I need is for the location in which the values are pasted to to be chosen by the variable entered in cell B1 of the active sheet matching to the corresponding location name in columnC of the active sheet (with the values then pasted starting in the adjacent cell). So if I enter 10 in A1, and B in B1 in the active sheet the code will search for file 10 in column A sheet1, skip the first two columns, copy a 4x4 grid of values, then paste these into the location b box, starting in the cell to the right of 'B'.

    Does anyone know how to adapt the code above to achieve this?

    Also, it would be useful to know if there is a more efficient way of picking the range to be copied instead of having to type out the individual offset.copy for each cell.

    Please let me know if I can clarify anything further.
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Adapt code to hide charts based on a cell value.
    By michelle 1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-07-2015, 11:10 AM
  2. [SOLVED] Adapt Code to copy and paste multiple Ws to multiples Ws to another Wb
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-13-2015, 11:42 AM
  3. Macro to cut and paste a variable number of rows to a variable location?
    By TheClaw2323 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-03-2015, 07:57 AM
  4. [SOLVED] Macro - Paste to ColumnCell Location Based on a Variable
    By hoops85 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-17-2014, 12:13 PM
  5. [SOLVED] Adapt code to copy and paste
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-09-2012, 07:52 PM
  6. Replies: 1
    Last Post: 02-23-2012, 05:49 PM
  7. Moving data to a variable location based on a cell value
    By EllandRoad Dave in forum Excel General
    Replies: 0
    Last Post: 02-02-2007, 01:58 PM

Tags for this Thread

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