+ Reply to Thread
Results 1 to 5 of 5

column alignment using cell insertion macro

  1. #1
    Registered User
    Join Date
    04-16-2016
    Location
    Maine, USA
    MS-Off Ver
    2007
    Posts
    3

    column alignment using cell insertion macro

    Hello,

    Total VBA noob here. My only coding experience is an introductory course in Python on Codecademy. I'm trying to write a macro that aligns the NDC numbers in column A with those in Column C on the Referenced Pivot worksheet in the attached file. The manual procedure is as follows: I scroll down the table until I see a mismatched NDC. I then insert 1x2 cells in the column with the greater NDC and shift the cells below down so that every NDC that appears in both lists is on the same row and every NDC and its total that appears on either but both has blank cells next to it in the opposite columns (A&B or C&D). This is so that I can eventually compare the totals for each NDC. It is the NDCs that need to match, not the totals.

    I watched a few VBA tutorials and I think what I need here is a For Each loop with a cell insertion macro inside. I recorded a macro with relative references to insert the cells that I have named "insertcell". The problem is that I have no idea what to declare or set or how to script the macro to trigger when the loop encounters an inequality.

    What I need is something like:

    For Each row in range A4:D[n]
    If cellA[n] > cellC[n] Then
    insertcell () at A[n]
    ElseIf cellA[n] < cellC[n]
    insertcell () at C[n]


    (I haven't fully learned VBA syntax yet; the above is just the logic I intend).

    Below is what I have so far. The code I wrote doesn't work as I don't know what to declare or set yet; I was really just practicing syntax more than anything. The only thing that works is the macro I recorded at the bottom:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by AtlanteanIdol; 04-16-2016 at 07:20 PM. Reason: added code tags

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: column alignment using cell insertion macro

    Welcome to the board.

    Please take a few minutes to real the forum rules, and then edit your post to add CODE tags.

    You want a result that looks like this?

    A
    B
    C
    D
    3
    EOB NDC Total Approved NDC Total
    80
    (blank)
    761032
    (blank)
    788821
    81
    00002322830
    540
    00002322830
    540
    82
    00002322930
    863
    00002322930
    863
    83
    00002323930
    60
    00002323930
    90
    84
    00002324030
    30
    00002324030
    30
    85
    00002327030
    270
    00002327030
    270
    86
    00002416502
    90
    00002416502
    270
    87
    00002446230
    432
    00002446230
    504
    88
    00002446430
    4
    00002446430
    6
    89
    00002512330
    360
    00002512330
    360
    90
    00002751001
    620
    00002751001
    620
    91
    00002803101
    4
    00002803101
    4
    92
    00002840001
    2.4
    00002840001
    2.4
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: column alignment using cell insertion macro

    Try something like this...

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    04-16-2016
    Location
    Maine, USA
    MS-Off Ver
    2007
    Posts
    3

    Re: column alignment using cell insertion macro

    Quote Originally Posted by AlphaFrog View Post
    Try something like this...

    Please Login or Register  to view this content.
    This works perfectly. I guess I didn't need to record the insertion macro after all; the resize command works also. Everything in your code makes sense to me (I had to look up what "Long" and "ScreenUpdating" were).

    Thank you so much! This makes me want to learn more VBA so that I can write these simple macros myself.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: column alignment using cell insertion macro

    You're welcome. Thanks for the feedback.

+ 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. freezing a column from insertion of other columns
    By sara ghaith in forum Excel General
    Replies: 2
    Last Post: 12-16-2013, 04:23 PM
  2. Transposing and insertion of rows into a column
    By thomsonjim in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-13-2012, 08:15 AM
  3. Replies: 1
    Last Post: 02-02-2012, 11:32 AM
  4. Conditional Formatting the Insertion of a Column
    By wmfinance in forum Excel General
    Replies: 2
    Last Post: 08-27-2010, 08:28 AM
  5. No column insertion in Copy code
    By flebber in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2010, 04:37 AM
  6. Merging previous one column (after insertion of a row) in a range
    By acsishere in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-16-2008, 12:23 AM
  7. Create a column upon insertion or overwriting of data.
    By Swiatkowski Peter in forum Excel General
    Replies: 1
    Last Post: 12-20-2005, 07:50 AM
  8. Proctect worksheet column/row insertion and deletions
    By kckmikeg in forum Excel General
    Replies: 1
    Last Post: 05-16-2005, 02:16 PM

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