+ Reply to Thread
Results 1 to 7 of 7

VBA Code to Paste Values a Table Column Based on Values in Another Column

Hybrid View

  1. #1
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    VBA Code to Paste Values a Table Column Based on Values in Another Column

    Hello - I need some help with a code that, through clicking on a button, will do the following:

    If the cells in Table1 "Helper Column" = "Match", then:
    1) Those cells in Table1 "Helper Column" will paste value into the same cells (i.e., remove the formulas and hard code them); and
    2) The corresponding cells in Table1 "Sales" will also paste value in the same cells.

    With the attached workbook example, this would mean F8:F10 and E8:E10 will be hard coded per #1 and #2 above, respectively.

    Thank you in advance!!
    Attached Files Attached Files

  2. #2
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: VBA Code to Paste Values a Table Column Based on Values in Another Column

    Give this a go

    Sub hardcode()
    
    Dim rownum As Integer
    
    rownum = 4
    
    Do While Range("F" & rownum).Value <> ""
    
        If Range("F" & rownum).Value = "Match" Then
            Range("E" & rownum & ":f" & rownum).Copy
           Range("E" & rownum).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        End If
        rownum = rownum + 1
    Loop
    
    End Sub
    Happy with my advice? Click on the * reputation button below

  3. #3
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Re: VBA Code to Paste Values a Table Column Based on Values in Another Column

    Thank you! This works perfectly, but I need one tweak. Instead of referencing the range by Column E and F, I want to reference by the table header name (in case new columns are inserted to the left of the table). I tried to replace by the header name ("Helper Column" and "Sales"), but it didn't work.

  4. #4
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: VBA Code to Paste Values a Table Column Based on Values in Another Column

    Will the header names always be in the same row?

  5. #5
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Re: VBA Code to Paste Values a Table Column Based on Values in Another Column

    Not necessarily, in case new rows are added above. Is it possible to reference the table as a named range in the code so that the location of the table doesn't matter (i.e., regardless of where it is placed)?

  6. #6
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: VBA Code to Paste Values a Table Column Based on Values in Another Column

    Try this. Its not real elegant but it searches a grid 20 columns wide and 10 rows deep from cell A1 looking for the table header columns then does its thing

    Sub hardcode()
    
    Dim rownum, colnum, startrow As Integer
    Dim Helpcolnum, salescolnum As Integer
    
    'find table header search 20 column by 10 rows looking for column headers "Helper Column" and "Sales"
    For colnum = 0 To 19
        For rownum = 0 To 9
            If Range("A1").Offset(rownum, colnum).Value = "Helper Column" Then
                Helpcolnum = colnum
                startrow = rownum
            End If
        
        
            If Range("A1").Offset(rownum, colnum).Value = "Sales" Then
                salescolnum = colnum
            End If
    
        Next rownum
    Next colnum
        
    
    rownum = startrow
    
    Do While Range("A1").Offset(rownum).Value <> ""
    
        If Range("a1").Offset(rownum, Helpcolnum).Value = "Match" Then
            Range("a1").Offset(rownum, Helpcolnum).Copy
             Range("a1").Offset(rownum, Helpcolnum).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Range("a1").Offset(rownum, salescolnum).Copy
             Range("a1").Offset(rownum, salescolnum).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
            
        End If
        rownum = rownum + 1
    Loop
    
    End Sub

  7. #7
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Re: VBA Code to Paste Values a Table Column Based on Values in Another Column

    Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 7
    Last Post: 10-23-2018, 10:18 AM
  2. Sum one column values based on color code in other column
    By civil8 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-26-2018, 05:25 AM
  3. [SOLVED] how to insert empty rows based on column B cell values and paste above values with macro?
    By genetist in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-11-2018, 11:47 AM
  4. [SOLVED] Copy Column in pivot table and paste values
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-26-2014, 09:32 AM
  5. Cut & paste rows based on duplicate column values
    By agolden7 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-26-2013, 11:09 AM
  6. How to filter a column in a table based on a set of values in another column
    By redhawk87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2013, 06:24 PM
  7. Macro that will copy values in Column J and paste values to Column B in new sheet
    By Phixtit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2010, 04:56 PM

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