+ Reply to Thread
Results 1 to 10 of 10

Create a loop with VBA

  1. #1
    Registered User
    Join Date
    01-31-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    64

    Create a loop with VBA

    Hi I have the code below where I send the value in column H2 to a sql database.
    I would like to do a loop where the values of all cells in the column H (i.e. H2 to HXX) are sent to the sql database.
    Could someone help to adjust my code with the proper loop? Thank you!


    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    What exactly do you have in column H?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    01-31-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    64

    Re: Create a loop with VBA

    Below is the content of cells in column H.

    INSERT INTO VFA_UPPF..SOC VALUES ('EXY561', 'Audi A1')
    INSERT INTO VFA_UPPF..SOC VALUES ('EXZ281', 'Audi A1')
    INSERT INTO VFA_UPPF..SOC VALUES ('MBY644', 'Audi A1')
    INSERT INTO VFA_UPPF..SOC VALUES ('EZC841', 'Audi A3')
    INSERT INTO VFA_UPPF..SOC VALUES ('NHS412', 'Audi A3')
    INSERT INTO VFA_UPPF..SOC VALUES ('NHX117', 'Audi A3')
    INSERT INTO VFA_UPPF..SOC VALUES ('MPY952', 'Audi A3')
    INSERT INTO VFA_UPPF..SOC VALUES ('UBZ245', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('EWZ584', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('TDP068', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('NTY330', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('BXX579', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('JXU072', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('GNL900', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('DHS359', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('WMJ173', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('MDN757', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('KER025', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('COL355', 'Audi A4')
    INSERT INTO VFA_UPPF..SOC VALUES ('NXG937', 'Audi A5')
    INSERT INTO VFA_UPPF..SOC VALUES ('MWE239', 'Audi A5')
    INSERT INTO VFA_UPPF..SOC VALUES ('OYE642', 'Audi A5')
    INSERT INTO VFA_UPPF..SOC VALUES ('COM773', 'Audi A5')
    INSERT INTO VFA_UPPF..SOC VALUES ('LAB727', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('EUZ276', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('EUZ256', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('HCL961', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('PTB333', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('EUS923', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('MBO818', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('MBN266', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('NHZ262', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('EWB642', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('EUW531', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('DPC365', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('NHZ259', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('EUU355', 'Audi A6')
    INSERT INTO VFA_UPPF..SOC VALUES ('EXG307', 'Audi Q3')
    INSERT INTO VFA_UPPF..SOC VALUES ('TDU305', 'Audi Q3')
    INSERT INTO VFA_UPPF..SOC VALUES ('CGS339', 'Audi Q5')
    INSERT INTO VFA_UPPF..SOC VALUES ('EXF396', 'Audi Q5')
    INSERT INTO VFA_UPPF..SOC VALUES ('KFB913', 'Audi Q5')

  4. #4
    Registered User
    Join Date
    01-31-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    64

    Re: Create a loop with VBA

    Isn't it possible to do a loop that takes Cells(8,X) as long as X is populated.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Create a loop with VBA

    Yes it is, why did you think it wasn't possible?
    Please Login or Register  to view this content.
    PS I still think you should look into using OPENROWSET to do the append in one go rather than a query for each row.

  6. #6
    Registered User
    Join Date
    01-31-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    64

    Re: Create a loop with VBA

    Worked perfectly again. I know you suggested the Openrowset solution for me. Although i don't really understand how to do it.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Create a loop with VBA

    If I knew the filename of the workbook, the sheet name, the table name etc I might be able to come up with something for OPENROWSET.

    Mind you, if the loop isn't taking too long then perhaps you should stick with that.

  8. #8
    Registered User
    Join Date
    01-31-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    64

    Re: Create a loop with VBA

    Filename: Missing_vehicles_links_test.xlsm
    Sheetname: Nyabilar
    TableName: Table_Query_from_vfa_uppf

    Do you mind helping me out with this thread, i have not recieved an answer from anyone yet: http://www.excelforum.com/excel-prog...html?p=3354529

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Create a loop with VBA

    This is a pretty rough guess and I can't currently test - don't have SQL Server installed on this.

    I've italicised the parts of the code you'll need to fill in.
    Please Login or Register  to view this content.
    To run this query you would use something like this, where con is the connection to the database.
    Please Login or Register  to view this content.
    PS lngRecsAff wil return the no of records affected, so could be used to check if the query was succesful

  10. #10
    Registered User
    Join Date
    01-31-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    64

    Re: Create a loop with VBA

    Hi Norie,

    I tried the code you sent me, although i could not really figure it out. The performance with the first solution was not too bad though, so i'll stick with it.

    Many Thanks!

+ 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. How to create VBA loop?
    By bergjes in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-18-2013, 03:11 PM
  2. Create a Loop
    By elfvis in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-10-2012, 06:13 PM
  3. How to create a Loop
    By Sahak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-08-2008, 02:49 PM
  4. [SOLVED] create a loop
    By Ivano in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-18-2006, 01:10 PM
  5. How do I create a For loop within a For loop?
    By Linking to specific cells in p in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2005, 06:07 AM

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