+ Reply to Thread
Results 1 to 4 of 4

Get VBA to match cell data with a worksheet name and then copy and paste a cell

  1. #1
    Registered User
    Join Date
    03-13-2019
    Location
    France
    MS-Off Ver
    Office 2013
    Posts
    2

    Post Get VBA to match cell data with a worksheet name and then copy and paste a cell

    Good Morning,

    I've got two workbooks, one named "Synthese" and another one named "Equity".

    In the workbook "Equity", I've got different worksheets which names correspond to the general ledger's accounts. My file "Synthese" gathers in column B the names of all the accounts I may found in my file "Equity"'s worksheets.

    I'd like to know if there's a VBA code that will allow me to copy the value of the cell E60 for each worksheet of my EQUITY and then paste it in the proper line of my file Synthese.

    For example :

    If the value of the cell B2 in the Synthese file is 145000, I want the VBA Code to look for the worksheet named 145000 in the file EQUITY, go into the worksheet 145000, copy the cell E60 and then paste it in the workbook Synthese in cell H2


    Due to company policy I am unable to provide you a file.

    Below is what I already did :

    Sub Sheet2Sheet()
    '
    ' Sheet2Sheet Macro
    Dim rData As Range
    Dim rCell As Range
    Dim rFrom As Range
    Dim rTo As Range
    Dim lRightCol As Long

    Workbooks.Open ("EQUITY.xlsx")
    Windows("Equity.xlsx").Activate
    With ActiveSheet
    lRightCol = Range("E60")

    Windows("0 - 108 - Fichier Synthese.xlsx").Activate
    Set rData = Range(Range("B7"), Range("B" & Rows.Count).End(xlUp))
    Windows("0 - 108 - Fichier Synthese.xlsx").Activate
    For Each rCell In rData
    Set rTo = Range("H" & Rows.Count)
    Range("E60").Copy
    Next
    Windows("0 - 108 - Fichier Synthese.xlsx").Activate
    Application.Goto Reference:="Sheet2Sheet"
    End Sub

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Get VBA to match cell data with a worksheet name and then copy and paste a cell

    This might work. It assumes that the Synthese workbook is already open but EQUITY is not. If EQUITY is open when the code runs, it will display an alert message telling you that the file is already open.

    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    03-13-2019
    Location
    France
    MS-Off Ver
    Office 2013
    Posts
    2

    Re: Get VBA to match cell data with a worksheet name and then copy and paste a cell

    Thank you for your quick answer.

    It's not working I have Run-time Error 9 on the line below :

    Set sh = wb2.Sheets(syn.Range("B2").Value)

    Also, how can I adapt your code to all the lines in column B?

    Thanks

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Get VBA to match cell data with a worksheet name and then copy and paste a cell

    I was not sure that would work since I could not see what cell B2 of the worksheet in the Synthese workbook actually contained. I based it on your statement that it would contain a worksheet name. If there is no name in cell B2 or if the entry in B2 is not a worksheet name then it will produce the Subscript out of Range error. I did not put an error trap there because the code is written to only run once against the other workbook, per your example scenario in the OP.

+ 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] Find last cell with data in a column copy and paste to another worksheet
    By muss1210 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-25-2017, 11:04 AM
  2. VBA Match Cell Range to Worksheet Name and copy/paste to respective worksheet
    By rlsublime in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-24-2014, 04:06 PM
  3. Match Cell Value to Worksheet Name Then Copy/ Paste Range to Worksheet
    By matt2072 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-31-2014, 01:23 PM
  4. Prevent Copy Paste over Data Validation cell if it does not match?
    By Akime in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-04-2013, 02:07 PM
  5. Replies: 0
    Last Post: 11-06-2012, 06:44 PM
  6. copy and paste data from one worksheet cell to another using command button
    By jasonruiz in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-13-2011, 04:34 AM
  7. Copy data from cell in one worksheet, add text, and paste to cell in another sheet
    By drewship in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-14-2009, 01:27 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