+ Reply to Thread
Results 1 to 13 of 13

Loop and IF help

  1. #1
    Registered User
    Join Date
    10-12-2011
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2003
    Posts
    13

    Loop and IF help

    Hello, I am a newbie to VBA and I know I will make some people cringe when you read my code. I am trying to set up a macro that will copy data from a input workbook, paste into an application, then paste into another output workbook.

    I am trying to take baby steps for now, I need to set up the part where I can take data from a spreadsheet thats populated in columns A:F, and a variable number of rows.

    I want the macro to start on cell A2 and work its way to F2, then move to the next row, etc (The paste in application and paste into output file I will handle later)

    I think I am hitting trouble in 2 areas:
    1) setting up the declarations
    2) the intersect (If (R,C) is in Col A, I need to paste it into a particular field in the application, same for each other column)

    Please Login or Register  to view this content.

    Any help would be greatly appreciated!
    Last edited by mgnickel; 04-17-2012 at 09:27 PM. Reason: tags

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Loop and IF help

    Welcome to the forum.

    Please read the forum rules and then edit your post to add code tags as required.
    Only after applying the missing code tags will others chip in the help.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Registered User
    Join Date
    10-12-2011
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop and IF help

    Code tags added

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Loop and IF help

    Is there a reason you can't copy and paste the whole block of data? Maybe something like:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-12-2011
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop and IF help

    No, the application requires me to copy and paste one cell at a time. Also, each cell per row needs to have a seperate copy and paste operation as each column needs to be pasted into a different field in the application... So "account" gets pasted into the "account" field. I was thinking intersect would be the best way to accomplish this, but I'm sure there is a more efficient way.

  6. #6
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Loop and IF help

    In my opinion, it would help if you gave a better description of your data. From what you've written, it seems that you have three sheets: (a) Active, (b) Application, and (c) Output. And you want to copy data from Active to both Application and Output. In the Active sheet you have six columns of data: "Office", "Account", "AT", "Journal", "Sign" and "Desc". What do the columns in Application & Output look like and how do you want the data copied?

  7. #7
    Registered User
    Join Date
    10-12-2011
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop and IF help

    The actual copy and paste part I can deal with, the part I need help with is setting up the loop and declarations to "activate" one cell at a time in the following order: A2, B2, C2, D2, E2, F2, A3, B3, C3, D3, E3, F3, and so on. The reason I added the 'paste to application and 'paste to output is because the application is not a spreadsheet, but an actual AS400 database that I use the "APPACTIVATE" and "SENDKEYS" syntax with. The output is a seperate spreadsheet. The operations of copy and paste I can handle, I just need help with the loops and figuring out how to activate each cell in that order.

  8. #8
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Loop and IF help

    How about:

    Please Login or Register  to view this content.
    x is up to you.

  9. #9
    Registered User
    Join Date
    10-12-2011
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop and IF help

    That helped a ton! I understand how you set that up now. Thanks for helping a newbie. Here is what I was looking for, a small modification to what you had. Obviously I will be copy and pasting the value instead of renaming it A-F.

    Please Login or Register  to view this content.
    Again, thank you very much!

  10. #10
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Loop and IF help

    For what it is worth, you can also use letters instead of numbers.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    08-17-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Loop and IF help

    Hello, I saw great examples but when I tried to change the ActiveCell to an actual cell address and it falls apart. I also tried using looping, next i, offset, ByVal etc. falls apart. I am attempting to do what I thought would be simple and that is shift the cell contents say: going from right to left, from Z4 to C4. In other words, I am wanting the previous cell's data kept in the new cell. I have a spreadsheet example of what I have working (but too lengthy for large data and what's not). Thanks for any help, [email protected]
    Attached Files Attached Files

  12. #12
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Loop and IF help

    To: gr8guitar

    Are you wanting something like this?

    Shift Left Cells C7 to F7
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    08-17-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Loop and IF help

    that works !!! thanks, it was driving me nuts. The only thing is, there are two columns (on the right side) with the same data. But it works:

    For nCol = 10 To 20
    Cells(8, nCol - 1) = Cells(8, nCol)
    Cells(8, 20) = Cells(5, 6)
    Next nCol
    Last edited by gr8guitar; 04-19-2012 at 04:31 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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