+ Reply to Thread
Results 1 to 8 of 8

Loop not copying value into desired cell

  1. #1
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Question Loop not copying value into desired cell

    Hello All,

    I am trying to get data from a table into various cells in another worksheet. I am doing this by selecting a facility from a drop down list in V16 of the sheet I want the data to go to.
    I then click a button with the following code in it:

    Please Login or Register  to view this content.
    It doesn't throw up any errors but when I look at the code execution I can see that it runs through each row in the table but doesn't match the facility, it simply stops there?

    The destination for the lookup info is B8 in the EQUFACsheet

    Any ideas?
    Last edited by Richard Buttrey; 03-13-2020 at 11:18 AM. Reason: Missing code tags.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop not copying value into desired cell

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.


    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here


    Since you are relatively new I'll add them for you on this occasion but please note for the future.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop not copying value into desired cell

    Difficult to say without seeing the workbook. Are you able to upload it?

    I note you appear to be using merged cells which is usually a no no since they just cause too many problems. There's a chance it might be something to do with that.

    Incidentally you don't need all the .Select stuff. Just address the objects directly in the code.
    Last edited by Richard Buttrey; 03-13-2020 at 11:35 AM.

  4. #4
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Re: Loop not copying value into desired cell

    Hi There,

    I have uploaded the file and changed the code slightly, Still doesn't work and doesn't make sense why.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Lightbulb Re: Loop not copying value into desired cell

    I have now simplified what i'm trying to do in test file 2, It still doesn't work but its easier to see what i'm trying to do.
    Attached Files Attached Files

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop not copying value into desired cell

    I fear you are over-complicating this. Where you have perfecly acceptable standard Excel functions then use them. Don't resort to VBA.

    In this case all you need in cells like F15 is a formula. Here
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    There is no need for any macro to loop through cells to find an copy stuff.
    Your code won't work incidentally for the following reason.
    Using Cells without an object qualifier is equivalent to ActiveSheet.Cells. So although you've Selected the Data sheet (and as I said previously .Select code is rarely necessary and just slows things down) the active sheet that the Cells() synatx is using is still the Equ Fac sheet containing the button you clicked.

    You would have to have used
    Please Login or Register  to view this content.
    to fully qualify the Cells object. And note the Sheet16. Another lesson - always use VBA sheet code names, the sheet tab name are too easily changed by a user and if changed will cause a hard coded tab name to fail.

    And before yuo get too far in your excel design skills please think carefully about using merged cells. Most of us avoid them like the plague. They just cause too many problems. Often a simple format of 'Center across cells is sufficient.

    I wrote this before I saw your simplified upload - and note for the future that we generally prefer copies of the production workbook rather than 'simple' versions whic are not representative of the actual layout and detail of the production workbook.
    Last edited by Richard Buttrey; 03-17-2020 at 07:53 AM.

  7. #7
    Registered User
    Join Date
    03-05-2020
    Location
    Stoke on Trent, England
    MS-Off Ver
    Excel 2016
    Posts
    59

    Re: Loop not copying value into desired cell

    To explain further, this is only part of what I’m intending it to do. I want to be able to pull selected data back into the input sheet, update it if necessary, then send it back into the table as an updated entry. My 1st step here is trying to get the data back into the input sheet and I only need the data if an update is required hence the button to retrieve.

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop not copying value into desired cell

    OK, understood.

    I think using a function in cells to read from the database is the optimum method.
    To achieve your next requirement of passing the changed data back to the database I'd adopt a similar approach to what you were doing.

    Create a row above your Input data. I always think it's best to keep this in a fixed range and above data rather than below.
    Make that range of cells an exact copy of your data labels, link each of the cells in that row of cells to the specific cells in the Input sheet and name the range, say "NewData"

    Now all you need is a macro which identifies the record ID row in the database, then copies and pastes the edited data to that row in te database.
    In order to find the data row that matches the edited record I tend to use an =MATCH() function that matches the edited Equipment ID to the data, rather than bothering with some VBA FIND code. So enter the following function in a spare cell and name that cell say "EquipNo"

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Then all the code yuo need is

    Please Login or Register  to view this content.

+ 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. Vba to insert picture into desired cell with cell size
    By meer_ali in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-21-2018, 03:52 AM
  2. [SOLVED] HELP copying multiple ranges per row based on cell criteria using a Loop
    By Jimbo42 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2016, 01:23 PM
  3. [SOLVED] Index Small Function Pulling Cell Below Desired Cell
    By sskgintl in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-03-2015, 04:26 PM
  4. Replies: 8
    Last Post: 07-09-2014, 10:42 PM
  5. [SOLVED] Count amount of desired numbers in a single cell and return that value in another cell
    By jmallory76 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-20-2013, 10:09 AM
  6. [SOLVED] How can I move from one desired cell to another in Excel using ENT
    By pibedoro in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-15-2006, 09:45 PM
  7. [SOLVED] For Next Loop...Copying from a cell one column to another...
    By bourbon84 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-29-2006, 10:15 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