Consulting

Results 1 to 15 of 15

Thread: Solved: VBA Code, who can help?

  1. #1
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location

    Solved: VBA Code, who can help?

    I am creating a workbook (week report) which must be filled with data from other workbooks (day reports). The data I want to retrieve from the different day reports, must be set in the week report.

    This is the script, i have got this far... There are still a lot of questions and challenges for me...


    [vba]Option Explicit

    Sub GetData()

    Dim Extra As Integer
    Dim cRange As Range
    Dim FileValue

    For Each cRange In Range("L2:R2")
    For Extra = 0 To 6

    FileValue = cRange
    Dim FilePath$, Row&, Column&, Address$


    Dim FileName$
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim sDay$
    Dim sMonth$
    Dim sYear$
    Dim dtmDate As Date
    dtmDate = cRange.Value
    sDay = Format(Day(dtmDate), "00")
    sMonth = Format(Month(dtmDate), "00")
    sYear = Format(Year(dtmDate), "0000")

    FileName$ = sDay & sMonth & sYear & ".xls"

    Const SheetName$ = "Blad1"
    Const NumRows& = 10
    Const NumColumns& = 1
    FilePath = fso.GetFolder(ThisWorkbook.path & "\..").path & "\Test1\"

    DoEvents
    Application.ScreenUpdating = False
    If Dir(FilePath & FileName) = Empty Then
    MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist"
    Exit Sub
    End If

    Dim cRow As Range
    Dim CellValue
    For Each cRow In Range("A1:A5, A8:A10")

    Address = cRow.Address
    CellValue = cRow.Address
    Worksheets("Blad2").Range(CellValue).Offset(0, 1).Offset(0, Extra) = GetData(FilePath, FileName, SheetName, Address)
    Next cRow
    ActiveWindow.DisplayZeros = False

    Next Extra
    Next cRange
    End Sub


    Private Function GetData(path, file, sheet, Address)
    Dim Data$
    Data = "'" & path & "[" & file & "]" & sheet & "'!" & Range(Address).Range("A1").Address(, , xlR1C1)
    GetData = ExecuteExcel4Macro(Data)
    End Function[/vba]
    Last edited by Aussiebear; 03-25-2011 at 02:58 PM. Reason: added VBA Tags to code

  2. #2
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    Please use the VBA tags "[vba]Option Explicit

    Sub GetData()

    Dim Extra As Integer
    Dim cRange As Range
    Dim FileValue

    For Each cRange In Range("L2:R2")
    For Extra = 0 To 6

    FileValue = cRange
    Dim FilePath$, Row&, Column&, Address$


    Dim FileName$
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim sDay$
    Dim sMonth$
    Dim sYear$
    Dim dtmDate As Date
    dtmDate = cRange.Value
    sDay = Format(Day(dtmDate), "00")
    sMonth = Format(Month(dtmDate), "00")
    sYear = Format(Year(dtmDate), "0000")

    FileName$ = sDay & sMonth & sYear & ".xls"

    Const SheetName$ = "Blad1"
    Const NumRows& = 10
    Const NumColumns& = 1
    FilePath = fso.GetFolder(ThisWorkbook.path & "\..").path & "\Test1\"

    DoEvents
    Application.ScreenUpdating = False
    If Dir(FilePath & FileName) = Empty Then
    MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist"
    Exit Sub
    End If

    Dim cRow As Range
    Dim CellValue
    For Each cRow In Range("A1:A5, A8:A10")

    Address = cRow.Address
    CellValue = cRow.Address
    Worksheets("Blad2").Range(CellValue).Offset(0, 1).Offset(0, Extra) = GetData(FilePath, FileName, SheetName, Address)
    Next cRow
    ActiveWindow.DisplayZeros = False

    Next Extra
    Next cRange
    End Sub


    Private Function GetData(path, file, sheet, Address)
    Dim Data$
    Data = "'" & path & "[" & file & "]" & sheet & "'!" & Range(Address).Range("A1").Address(, , xlR1C1)
    GetData = ExecuteExcel4Macro(Data)
    End Function[/VBA]". Where do you need help?
    I am a Newbie, soon to be a Guru

  3. #3
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location
    In all the loop parts...

    Currently the data which is written away in the main workbook (week report) is the data which is retrieved from the last file.

    The range (A1:A5, A8:A10) from the last file ("R2") is placed in the week report range (B1:H5, B8:B10). The situation i want is the next:

    Range (A1:A5, A8:A10) from file ("L2") must written in the workbook (week report) range(B1:B5, B8:B10).
    Range (A1:A5, A8:A10) from file ("M2") must written in the workbook (week report) range(C1:C5, C8:C10).
    etc...

    BTW: This script is placed in the workbook (week report)

    I hope this clear, otherwise I can upload my files!

  4. #4
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    [VBA] For Each cRow In Range("A1:A5, A8:A10")

    Address = cRow.Address ' You dont need this line
    CellValue = cRow.Address
    Worksheets("Blad2").Range(CellValue).Offset(0, 1).Offset(0, Extra) = GetData(FilePath, FileName, SheetName, Address)
    Next cRow
    ActiveWindow.DisplayZeros = False

    Next Extra [/VBA]

    The above loop has a undefined variable Address. Could you upload a sample to test this.
    I am a Newbie, soon to be a Guru

  5. #5
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location
    I tested it and you do need this line!

    Attached my test samples!

    In the folder Test there is the main file (test.xls) see worksheet2 -- the button.
    In this sheet I want the results from the files in folder Test1
    The data (A1:A5, A8:A10) from the file 12122011.xls must be set at test.xls (B1:B5, B8:B10).
    The data (A1:A5, A8:A10) from the file 13122011.xls must be set at test.xls (C1:C5, C8:C10).
    Attached Files Attached Files

  6. #6
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    There is no Test.xls document in the zip file
    I am a Newbie, soon to be a Guru

  7. #7
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location
    The test.xls is in the folder Test

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Remove the For Extra loop and use this instead

    [VBA]
    Next cRow
    ActiveWindow.DisplayZeros = False

    Extra = Extra + 1
    Next cRange
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location
    If I cancel the loop, the data will only be placed in the B column and nothing in the C column.

    Also my other problem is still there, the data of the 2nd files (13122011.xls) is set and the data of the 1st file (12122011.xls) is ignored.

    This is the situation i want:

    Range (A1:A5, A8:A10) from file ("L2" --> 12122011) must written in the workbook (Test.xls) on the place range(B1:B5, B8:B10).
    Range (A1:A5, A8:A10) from file ("M2" --> 13122011) must written in the workbook (test.xls) on the place range(C1:C5, C8:C10).

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Did you try it?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location
    Yes!

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Strange, This what I get
    Attached Images Attached Images
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location
    Can you upload your sample? Maybe I did something wrong...

    btw does it make a difference of the version of excel? I use 2003!

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sure
    Attached Files Attached Files
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location
    Problem solved!!!!

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •