+ Reply to Thread
Results 1 to 2 of 2

Thread: Loop a code using the values of various columns and rows within a table

  1. #1
    Registered User
    Join Date
    01-17-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    1

    Loop a code using the values of various columns and rows within a table

    Hi, I am new to excel and after teaching myself have made a code that will copy a set of data from a table into templates, then the data from these templates are copied into new workbooks. The workbooks already exist, and their filepaths are contained in cell values within the main spreadsheet (ThatFile) which I'm running the macro from (ThisFile).
    There are 20 repetative stages in my macro. It works fine, but I was seeing if it could be looped. I am having trouble coming up with a code that will loop the below instruction and use the values in various columns within the table of ThisFile.

    Here are 2 from the 20, the increment just goes down one for each of the 5 columns as you can see. I think the trouble is getting it to go back ThisFile each time so it is referencing the correct cell values.

    ThisFile = "Data to be input to template Macro.xlsm"
    ThisSheet = Range("D2").Value
    ThisRange = Range("E2").Value
    ThatFile = Range("G2").Value
    ThatSheet = Range("H2").Value
    ThatRange = Range("I2").Value

    Windows(ThisFile).Activate
    Sheets(ThisSheet).Select
    Range(ThisRange).Select
    Selection.Copy
    Windows(ThatFile).Activate
    Sheets(ThatSheet).Select
    Range(ThatRange).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.CutCopyMode = False

    Windows("Data to be input to template Macro.xlsm").Activate
    Sheets("Related Workbooks").Select

    ThisFile = "Data to be input to template Macro.xlsm"
    ThisSheet = Range("D3").Value
    ThisRange = Range("E3").Value
    ThatFile = Range("G3").Value
    ThatSheet = Range("H3").Value
    ThatRange = Range("I3").Value

    Windows(ThisFile).Activate
    Sheets(ThisSheet).Select
    Range(ThisRange).Select
    Selection.Copy
    Windows(ThatFile).Activate
    Sheets(ThatSheet).Select
    Range(ThatRange).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.CutCopyMode = False

    I've tried lots of different looping techniques but I might just cut my losses and leave it as an uber long macro! But I would like to further my knowledge of excel so any help from anyone would be most welcome.

    Happy New Year!!

  2. #2
    Valued Forum Contributor tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    USA
    MS-Off Ver
    Excel 2003 - 2007
    Posts
    2,352

    Re: Loop a code using the values of various columns and rows within a table

    TommyToe,

    Welcome to the forum!
    Unfortunately, we can't help you until you use code tags. Check the forum rules (link in my sig) for how
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0