+ Reply to Thread
Results 1 to 17 of 17

Populating a table using vba

  1. #1
    Registered User
    Join Date
    10-30-2013
    Location
    Devon
    MS-Off Ver
    Excel 2010
    Posts
    15

    Populating a table using vba

    Hi all,

    I am hoping someone can help me come up with a way populate a table from a list of data automatically. Sounds easy I know.

    I work for a construction company that has a number of different sites active at any one time. Once a quarter we compare the costs of the site against how we thought it would perform previously.


    The first 6 tabs are sites. They contain a table with a number of column’s. I would like to fill the Column named “Cost to Date”.

    In the next tab “Download of costs” is a download from our accounting system with the costs for the sites. The report has been pasted straight from our system and used the “text to columns” function in excel to make it legible. Other than that it is under editable which I imagine will make this task more difficult.


    As you will be able to see I will want all of the costs in the “To Date” figures from the download to be populated into its corresponding cell on the site tabs. The left hand column (2000/2005/2010) etc is cost heads.


    Eg: I would like code 2000 (Site Manager) for £1 to be put in to the first tab (Site 1) in the cost to date column to line up with code 2000 there.




    In my example I have only changed the figures and site names to keep all our data secure and there are only 6 sites. In reality there are around 100 sites.

    If this will involve VBA which I must add I have very little knowledge of, I will need a code that is simple, and can be edited easily as the list of sites grow etc.

    Some one pleeeeease help me. This tasks takes hours and hours to complete manually and with the company growing it is only likely to become worse. I want to save time and eradicate the chances of human error..

    Any more info needed let me know


    Cheers

    Chris
    Last edited by chris-streeter; 09-30-2014 at 11:17 AM.

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Populating a table using vba

    Cross posted here:
    http://www.mrexcel.com/forum/excel-q...ml#post3949970

    A message to forum cross posters

    Please read this:
    http://www.excelguru.ca/node/7
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Populating a table using vba

    Try the code in Returned file on sheet "Download of Costs".
    Last edited by MickG; 09-30-2014 at 11:31 AM.

  4. #4
    Registered User
    Join Date
    10-30-2013
    Location
    Devon
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Populating a table using vba

    PHP Code: 
    Option Explicit

    Private Sub CommandButton1_Click()
    Dim Sht         As Worksheet
    Dim Dic         
    As Object
    Dim Str         
    As String
    Dim nRng        
    As Range
    Dim Num         
    As Integer
    Dim Site        
    As String
    Dim R           
    As Range
    Dim Rng         
    As Range
    Dim Dn          
    As Range
    Set Dic 
    CreateObject("scripting.dictionary")
            
    Dic.CompareMode vbTextCompare
    With Sheets
    ("Download of Costs")
        
    Set Rng = .Range(.Range("A10"), .Range("A" Rows.Count).End(xlUp))
    End With
    For Each Dn In Rng
        
    If Dn "CONTRAC" Then
            
    If Str "" Then
                Str 
    Str "," Dn.Address
            
    Else
                
    Str Str ":" Dn.Offset(-1).Address "," Dn.Address
            End 
    If
        
    End If
    Next Dn
    Str 
    Str ":" Range("A" Rng.Count 10).Address
    Set nRng 
    Range(Mid(Str2))

    For 
    Each Dn In nRng.Areas
        Num 
    InStr(Dn(1).Offset(, 1), "Site")
        
    Site Mid(Dn(1).Offset(, 1), Num, (Len(Trim(Dn(1).Offset(, 1))) - Num) - 1)
            For 
    Each R In Dn
                
    If Application.IsNumber(R.Value) = True Then
                    
    If Not Dic.exists(SiteThen
                        Set Dic
    (Site) = CreateObject("Scripting.Dictionary")
                    
    End If
                        
    Dic(Site)(R.Value) = R.Offset(, 4)
                
    End If
            
    Next R
    Next Dn

    For Each Sht In Worksheets
          
    If Not Sht.Name "Download of Costs" Then
             
    If Dic.exists(Sht.NameThen
            With Sheets
    (Sht.Name)
                
    Set Rng = .Range(.Range("A10"), .Range("A" Rows.Count).End(xlUp))
            
    End With
                
            
    For Each Dn In Rng
                 
    If Dic(Sht.Name).exists(Dn.ValueThen
                    Dn
    .Offset(, 3).Value Dic(Sht.Name).Item(Dn.Value)
                 
    End If
            
    Next Dn
         End 
    If
        
    End If
    Next Sht
    MsgBox 
    "Run"
    End Sub

    [PHP
    [/PHP]


    Hi this is great! Exactly what I am looking for.

    Is there anyway you can split the code up into sections and put something brief to describe what each part of the code is doing.. i.e top section is looking through searching for site name, next section bla bla bla..hopefully I will be able to understand it then and edit it slightly, as this needs to be adjusted to search through multiple tabs (which are sites).

    Really appreciate your help

    Cheers

    Chris

  5. #5
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Populating a table using vba

    I can do that if you think it will help !!!, but the code should already search through all the sheets and alter them if the name of the sheet is in sheet "Download of costs" column "B" next to word "CONTRAC". (NB:- the Site name being in that string)

    Regrds Mick

    PS :- Have you Checked the results to see their correctness?????
    Last edited by MickG; 09-26-2014 at 09:50 AM.

  6. #6
    Registered User
    Join Date
    10-30-2013
    Location
    Devon
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Populating a table using vba

    if you don't mind I think it will help yes. Mainly because it just got stuck when I tried it with real data.

    I was hoping that along with a little research and my boss, who knows a lot more about VBA we will be able to fine tune it so I don't have to keep pestering you.

    And yes I checked the result very good indeed!

    Thanks

    Chris
    Last edited by chris-streeter; 09-30-2014 at 11:19 AM.

  7. #7
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Populating a table using vba

    Try some light bedtime reading :-
    Hope it helps!!!!
    Please Login or Register  to view this content.
    Regards Mick

  8. #8
    Registered User
    Join Date
    10-30-2013
    Location
    Devon
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Populating a table using vba

    That's great thanks very much! I'll endeavor to get better at VBA

    I can't work out why it isn't working on the version I have loaded with more sites.

    Any chance I can email you the file to look at? its 3mb so too big for the forum?

    Thanks

    Chris

  9. #9
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Populating a table using vba

    I would prefer it you reduced it size, and sent it in as a PM on of the Forum.

  10. #10
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Populating a table using vba

    I would prefer it you reduced it size, and sent it in as a PM on of the Forum.
    PS:- You send the file using "Box.com"

  11. #11
    Registered User
    Join Date
    10-30-2013
    Location
    Devon
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Populating a table using vba

    Hi Mick,

    Not sure I have done it correctly as I have never used box.com.

    But here is the link to the file: https://app.box.com/s/aht2fu04meyj8utygcpz

  12. #12
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Populating a table using vba

    Thank you for the File.
    I think the main problem is the "Sheet Names" in relation to the String of words that are suppose to relate to those names in Column "B" of "Download of Cost".
    Below is a list oF the sheets Names and Next to them a list of the Related sheet names from column "B". In between them are the names that appears to have show some relationship.
    As you will see this relationship appears a bit tenuous.
    If you can come up with a more specific way to find the correct relationships, I think the code will work ???
    Last edited by MickG; 09-30-2014 at 11:32 AM.

  13. #13
    Registered User
    Join Date
    10-30-2013
    Location
    Devon
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Populating a table using vba

    I see the problem yes, some don't have much common ground.

    Is it not possible for the code to search for the "Site Code number" I.E Crinnis Wood has a site number of C0064. There is a site number on all of the sheet names and the download of costs col b names?

  14. #14
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Populating a table using vba

    Try this, it appears to be working OK:-
    https://app.box.com/s/t288bpxwfk1sfn2h0kgf

  15. #15
    Registered User
    Join Date
    10-30-2013
    Location
    Devon
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Populating a table using vba

    This is great. It will save me and other staff a mountain of time.

    Cannot thank you enough for the effort you put in and for your patience with me!

    Thanks again

  16. #16
    Registered User
    Join Date
    10-30-2013
    Location
    Devon
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Populating a table using vba

    This is great. It will save me and other staff a mountain of time.

    Cannot thank you enough for the effort you put in and for your patience with me!

    Thanks again

  17. #17
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Populating a table using vba

    You're welcome

+ 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. Replies: 6
    Last Post: 03-07-2014, 07:26 AM
  2. Populating a list from a data table
    By troop2118 in forum Excel General
    Replies: 5
    Last Post: 07-20-2012, 04:28 AM
  3. Populating a Table from a Dynamic List selection
    By seanfoxen in forum Excel General
    Replies: 3
    Last Post: 12-19-2011, 01:38 PM
  4. [SOLVED] sounds extremely easy but its NOT!
    By aaaaaaaa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-17-2010, 01:05 PM
  5. Replies: 0
    Last Post: 03-24-2006, 01:00 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