+ Reply to Thread
Results 1 to 7 of 7

Looking to Apply Macro to an Entire Column without running every time

  1. #1
    Registered User
    Join Date
    06-05-2015
    Location
    Washington D.C.
    MS-Off Ver
    MS Office 2010
    Posts
    4

    Looking to Apply Macro to an Entire Column without running every time

    Hey this may be a simple question but I am new to the Macro business any help would be greatly appreciated. Basically I am trying to Copy information from one sheet of my excel file which contains analyzed data and then paste that data to the sheet that has thousands of cells with raw data. The Copy would be based on a specific userID and then the paste would have to happen multiple times on the other sheet everywhere that the specific username is in the adjacent cell. I would then want this to run for an entire column. I will post a basic recorded Macro just to give an idea where this is to happen and also provide any information that could be useful:

    Sub BusinessMove()
    '
    ' BusinessMove Macro
    '
    ' Keyboard Shortcut: Ctrl+m
    '
    ActiveCell.Offset(0, -1).Range("A1").Select
    Selection.Copy
    Sheets("businesses").Select
    Cells.Find(What:="70149726", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("raw data").Select
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(1, 0).Range("A1").Select


    Application.CutCopyMode = False
    Selection.Copy
    Sheets("businesses").Select
    Cells.Find(What:="70008363", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("raw data").Select
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveSheet.Paste
    End Sub

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Looking to Apply Macro to an Entire Column without running every time

    This is a great example where recorded code can be both a blessing and a curse. It's helping you do what you want, but man is this terrible code (no fault of your own). You don't need a single one of those .Activate or .Selects, and the repetitive code can be replaced by a simple Do While Loop.

    Providing a sample worksheet would help, though, with a before and after. Doesn't need to be a huge data set, just enough to demonstrate the functionality you want.

    I ask for an example because some of the code is confusing. Like I don't understand in your first few lines of code why you are copying something, doing nothing with it, switching sheets, then copying something else.

    This will likely only end up being ~a dozen lines of code at most, so please don't get discouraged.

    Your use of ActiveCell isn't great practice either unless absolutely necessary, so if you can show which cells you're trying to copy, that would help.
    Last edited by walruseggman; 06-05-2015 at 11:25 AM.

  3. #3
    Registered User
    Join Date
    06-05-2015
    Location
    Washington D.C.
    MS-Off Ver
    MS Office 2010
    Posts
    4

    Re: Looking to Apply Macro to an Entire Column without running every time

    Okay, give me a minute or two
    Last edited by notaprogramer; 06-05-2015 at 12:24 PM.

  4. #4
    Registered User
    Join Date
    06-05-2015
    Location
    Washington D.C.
    MS-Off Ver
    MS Office 2010
    Posts
    4

    Re: Looking to Apply Macro to an Entire Column without running every time

    Hopefully this makes more sense, basically I have the needed information on the second sheet, I want to copy paste the userID from this page, store it and then also store the “hobby” (the reason I copied and didn’t do anything with it is because I stored it in the search bar of the “Search” function, but as I’m sure you know, that doesn’t work. ) I would then like to take this information to the first sheet and wherever that UserID occurs to paste the hobby in the adjacent cell.

    So I guess to sum it up for both you and I:
    Copy Unique search criteria (UserID) and “hobby” from page 2
    Paste Hobby with corresponding UserIDs on page 1
    Repeat x workfile
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Looking to Apply Macro to an Entire Column without running every time

    7 lines, not too bad. It's not going to look like it "works" on your example sheet because you used RAND to generate the dummy User IDs. Which is fine, except that it means the IDs don't match across the sheets, so nothing is found that matches, so no hobbies are copied. Once I hard coded the user IDs to match across the sheets, the code worked as-expected. Hopefully it works on your real data, too.

    Let me know if you have any questions. The sheet names on the first two lines of code may need to be changed to match your actual workbook, I commented which was which.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-05-2015
    Location
    Washington D.C.
    MS-Off Ver
    MS Office 2010
    Posts
    4

    Re: Looking to Apply Macro to an Entire Column without running every time

    If I weren't happily married I would propose haha, thank you for your help good sir or maddam

  7. #7
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Looking to Apply Macro to an Entire Column without running every time

    Glad to help, thanks for the rep.

+ 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] How to apply formula to entire column
    By i.r.smith in forum Excel General
    Replies: 3
    Last Post: 09-01-2014, 05:44 PM
  2. Running Macro for entire column
    By Pinnz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-15-2014, 02:57 PM
  3. How can I apply a 20% discount to an entire price list at one time?
    By akrhodes in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-18-2013, 07:21 PM
  4. Apply formula to entire column macro
    By aznprod517 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2009, 10:41 AM
  5. Formula that will apply to entire column???
    By borbafett in forum Excel General
    Replies: 2
    Last Post: 04-29-2008, 03:40 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