+ Reply to Thread
Results 1 to 11 of 11

Macro - Get adjacent cell values based on a condition & save as a different workbook

  1. #1
    Registered User
    Join Date
    07-12-2005
    Posts
    7

    Macro - Get adjacent cell values based on a condition & save as a different workbook

    Hi,

    here's my requirement.

    1) find a Column Name " Factor Name"
    2) Look down in the same column for a particular value such as "ABC" or "DEC" on all the cell values below this column name (which is Factor Name), say A10
    3) if there is a match, copy all the values from adjacent cell values, Say A11 to A21

    i am trying to do it using functions such as MATCH & OFFSET

    so, the Macro should do the following
    with or Without opening the workbooks in that directory, navigate to the multiple Sheets (which is given as Input) of that workbook and extract the data as per above 3 steps and save it as new workbook

    Please assist me. Many thanks

    Best Regards

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    Hi, ManInRed,

    this code starts on the active sheet and searches the header in row 1 and will copy to Sheet3:
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    07-12-2005
    Posts
    7

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    Hi Holger,

    thanks for your reply.
    the code didnt go into the first IF condition..

    it executed the statement and it came out..


    Regards
    MIR

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    Hi, ManInRed,

    the code didnt go into the first IF condition..
    So either the seach item isnīt found in the first row of the sheet (you could alter the area to search to like Rows("1:4") for the first 4 rows in the sheet) or the search doesnīt need to search for the whole item to match but for a partly match so that the parameter LookAt:=xlWhole should be LookAt:=xlPart if the item is preceeded by a blank like in your first mentioning of the search string (which I thought to be a typo).

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    07-12-2005
    Posts
    7

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    yes, now it worked. i did the change as you had mentioned.
    so it do not copy the header. it copies the contents below.

    if in case if i want to copy teh header as well. ie., the column name which consists of Factor Name also to be copied. - with that statement it didnt work out well..

    i am tryign out other options

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    Hi, ManInRed,

    you already havbe set a range to that value so it should not really be a big problem. My suggestion might be
    Please Login or Register  to view this content.
    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    07-12-2005
    Posts
    7

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    thanks Holger. i will tell you how it goes tomorrow.

    i also need to do folowing tasks
    a) need to get unique values based on one column (Factor Name)
    b) and get the contents from 5 different sheets based on that Factor name

    need to work on that tomorrow

    Best Regards

  8. #8
    Registered User
    Join Date
    07-12-2005
    Posts
    7

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    Hi Holger,

    the code is not working now. couldn't find the issue.
    there is no error and there are no contents copied..!! Please help

    Dim rngHeader As Range
    Dim rngDetail As Range

    ThisWorkbook.Activate
    ThisWorkbook.Sheets("Test Cycle 1").Select

    Set rngHeader = Rows("1:300").Find(What:="Factor Name", LookAt:=xlWhole)

    If Not rngHeader Is Nothing Then
    Set rngDetail = rngHeader.EntireColumn.Find(What:="ABC", LookAt:=xlWhole)
    If Not rngDetail Is Nothing Then
    rngHeader.Copy Destination:=Sheets("Sheet3").Range("D1")
    rngDetail.Offset(1, 0).Resize(10, 1).Copy Destination:=Sheets("Sheet3").Range("D2")
    End If
    End If

    Set rngDetail = Nothing
    Set rngHeader = Nothing

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    Hi, ManInRed,

    you should add code-tags to the procedure as requested by the Forum Rules.

    As I donīt know your data and what might be the issue with the code not working you should attach a workbook without sensitive data to have a look at.
    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic
    Ciao,
    Holger

  10. #10
    Registered User
    Join Date
    07-12-2005
    Posts
    7

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    Book1.xls

    Hi Holger,

    PFA, attached workbook about the problem

    pls let me know, if you can download the workbook.

    Thanks
    MIR

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro - Get adjacent cell values based on a condition & save as a different workbook

    Hi, ManInRed,

    in the attachment you have Sheet1 whereas the code looks for Sheet 1. I can reproduce the error and the reason might be that the code is placed behind Sheet3 whereas I would it expect it to be placed in a standard module which you would need to insert.

    The code may be like
    Please Login or Register  to view this content.
    I placed the code in Module1 and ran it from there without problems except that you reduced the number of rows to copy to just one.

    Ciao,
    Holger

+ 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: 1
    Last Post: 01-10-2013, 08:22 AM
  2. macro to save workbook using cell values
    By af01waco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-16-2012, 03:43 PM
  3. Replies: 0
    Last Post: 06-19-2012, 10:16 AM
  4. Calculate Adjacent Cell based on a Condition
    By Eamonn in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-13-2011, 11:05 AM
  5. Average / SD values based on condition in adjacent cell
    By gypsyeyes in forum Excel General
    Replies: 3
    Last Post: 07-30-2009, 09:23 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