+ Reply to Thread
Results 1 to 8 of 8

Copy value of cell with given group ID to cells in different rows with same group ID

  1. #1
    Registered User
    Join Date
    12-30-2022
    Location
    Oslo, Norway
    MS-Off Ver
    365
    Posts
    3

    Red face Copy value of cell with given group ID to cells in different rows with same group ID

    I'm trying to create a macro that constantly checks a range of cells for value changes (i.e., changes in values of dropdown lists; see example file). When a change is detected, it copies the cell's value to all cells in the same column that share a group ID with the original cell. I've cobbled something together using a simple loop (see below), but this is my first time working with VBA, and I don't get it to work. I get a runtime error 1004: "Method 'Range' of object '_Worksheet' failed" for the line setting the new cell value. Any help would be much appreciated!



    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim ws As Worksheet
    Dim value As Integer, rowid As Integer, columnid As Integer
    Dim lastRow As Long
    Set ws = Worksheets("Sheet1")

    lastRow = ws.Range("A" & Rows.Count).End(xlUp).row

    If Not Intersect(Target, Range("C2:E9")) Is Nothing Then

    rowid = ActiveCell.row
    columnid = ActiveCell.Column

    For i = 2 To lastRow
    If i <> rowid Then
    If ws.Range("B" & i).value = ws.Range("B" & rowid).value Then
    Set Range(columnid & i).value = Range(columnid & rowid).value
    End If
    End If
    Next

    End If

    End Sub
    Attached Files Attached Files
    Last edited by missingvalue; 12-30-2022 at 10:14 AM.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Copy value of cell with given group ID to cells in different rows with same group ID


    Just remove the useless Set statement and well read Set VBA help in order to understand why …

  3. #3
    Registered User
    Join Date
    12-30-2022
    Location
    Oslo, Norway
    MS-Off Ver
    365
    Posts
    3

    Re: Copy value of cell with given group ID to cells in different rows with same group ID

    Thanks for the suggestions. I removed the Set statement, but the error still occurs. Btw, I put this code in the code section of the workbook. The data that should be copied between cells is text/string.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Copy value of cell with given group ID to cells in different rows with same group ID


    As your columnid & i has no sense, check at least the variable content …
    Last edited by Marc L; 12-30-2022 at 03:07 PM. Reason: typo ...

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Copy value of cell with given group ID to cells in different rows with same group ID


    When using numeric value as column / row index then rather than Range just use the Cells statement as explained in VBA help …

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,284

    Re: Copy value of cell with given group ID to cells in different rows with same group ID

    This is what you are wanting to achieve with your code...
    Please Login or Register  to view this content.
    Here is a non looping option...
    Please Login or Register  to view this content.
    Last edited by sintek; 12-30-2022 at 04:08 PM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  7. #7
    Registered User
    Join Date
    12-30-2022
    Location
    Oslo, Norway
    MS-Off Ver
    365
    Posts
    3

    Re: Copy value of cell with given group ID to cells in different rows with same group ID

    Quote Originally Posted by sintek View Post
    This is what you are wanting to achieve with your code...
    Please Login or Register  to view this content.
    Here is a non looping option...
    Please Login or Register  to view this content.

    Brilliant--this works beautifully. Very nice also with the non-looping option. Thanks!

  8. #8
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,284

    Re: Copy value of cell with given group ID to cells in different rows with same group ID

    Glad to have helped...Tx for rep +
    For future reference...No need to quote entire post...just clutters thread...
    Can just...@ sintek...Code in Post 6 works...

+ 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. [SOLVED] Copy Value of cells of rows into next sheets row without spilling into next cell
    By Ajaxs87 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-31-2022, 08:24 PM
  2. [SOLVED] VBA Split Cell Contents to New Rows & Copy Cells containing single values to the new rows
    By jaimelwilson in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-14-2017, 05:30 PM
  3. Replies: 3
    Last Post: 09-02-2015, 01:21 PM
  4. [SOLVED] Identify duplicate rows and copy/Paste the first cell to all cells of equal rows
    By vs5 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-15-2014, 06:31 AM
  5. Replies: 2
    Last Post: 11-12-2013, 08:42 PM
  6. [SOLVED] Insert rows based on the current cell value and, then, copy some cells into the new rows
    By Excel-RZ in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-03-2013, 05:19 PM
  7. copy several rows of cells into one cell
    By AGA in forum Excel General
    Replies: 4
    Last Post: 07-14-2011, 07:19 AM

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