+ Reply to Thread
Results 1 to 3 of 3

Hello, macro combining data but showing 0 as values for combined sheet

  1. #1
    Registered User
    Join Date
    05-22-2019
    Location
    United states
    MS-Off Ver
    latest
    Posts
    2

    Hello, macro combining data but showing 0 as values for combined sheet

    Hello, i have a macro which i found online written by Dan Wagner. the macro runs fine but the output gives me values as "0". Imagine this:

    Customer ID | Product | Price
    1 Apple 5
    2 mango 3
    0 0 0
    0 0 0

    here is the macro:

    *********************************Code Start****************************************************************************

    Option Explicit
    Public Sub CombineManyWorkbooksIntoOneWorksheet()

    Dim strDirContainingFiles As String, strFile As String, _
    strFilePath As String
    Dim wbkDst As Workbook, wbkSrc As Workbook
    Dim wksDst As Worksheet, wksSrc As Worksheet
    Dim lngIdx As Long, lngSrcLastRow As Long, _
    lngSrcLastCol As Long, lngDstLastRow As Long, _
    lngDstLastCol As Long, lngDstFirstFileRow As Long
    Dim rngSrc As Range, rngDst As Range, rngFile As Range
    Dim colFileNames As Collection
    Set colFileNames = New Collection

    'Set references up-front
    strDirContainingFiles = "U:\Final Roaster" '<~ your folder
    Set wbkDst = Workbooks.Add '<~ Dst is short for destination
    Set wksDst = wbkDst.ActiveSheet
    'Store all of the file names in a collection
    strFile = Dir(strDirContainingFiles & "\*.xlsx")
    Do While Len(strFile) > 0
    colFileNames.Add Item:=strFile
    strFile = Dir
    Loop

    ''CHECKPOINT: make sure colFileNames has the file names
    'Dim varDebug As Variant
    ' For Each varDebug In colFileNames
    ' Debug.Print varDebug
    ' Next varDebug

    'Now we can start looping through the "source" files
    'and copy their data to our destination sheet
    For lngIdx = 1 To colFileNames.Count

    'Assign the file path
    strFilePath = strDirContainingFiles & "\" & colFileNames(lngIdx)

    'Open the workbook and store a reference to the data sheet
    Set wbkSrc = Workbooks.Open(strFilePath)
    Set wksSrc = wbkSrc.Worksheets("Final Roster") '<~ change based on your Sheet name

    'Identify the last row and last column, then
    'use that info to identify the full data range
    lngSrcLastRow = LastOccupiedRowNum(wksSrc)
    lngSrcLastCol = LastOccupiedColNum(wksSrc)
    With wksSrc
    Set rngSrc = .Range(.Cells(1, 1), .Cells(lngSrcLastRow, _
    lngSrcLastCol))
    End With

    ''CHECKPOINT: make sure we have the full source data range
    ' wksSrc.Range("A1").Select
    ' rngSrc.Select

    'If this is the first (1st) loop, we want to keep
    'the header row from the source data, but if not then
    'we want to remove it
    If lngIdx <> 1 Then
    Set rngSrc = rngSrc.Offset(1, 0).Resize(rngSrc.Rows.Count - 1)
    End If

    ''CHECKPOINT: make sure that we remove the header row
    ''from the source range on every loop that is not
    ''the first one
    ''wksSrc.Range("A1").Select
    '' rngSrc.Select

    'Copy the source data to the destination sheet, aiming
    'for cell A1 on the first loop then one past the
    'last-occupied row in column A on each following loop
    If lngIdx = 1 Then
    lngDstLastRow = 1
    Set rngDst = wksDst.Cells(1, 1)
    Else
    lngDstLastRow = LastOccupiedRowNum(wksDst)
    Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)
    End If
    rngSrc.Copy Destination:=rngDst '<~ this is the copy / paste

    'Almost done! We want to add the source file info
    'for each of the data blocks to our destination

    'On the first loop, we need to add a "Source Filename" column
    If lngIdx = 1 Then
    lngDstLastCol = LastOccupiedColNum(wksDst)
    wksDst.Cells(1, lngDstLastCol + 1) = "Source Filename"
    End If

    'Identify the range that we need to write the source file
    'info to, then write the info
    With wksDst

    'The first row we need to write the file info to
    'is the same row where we did our initial paste to
    'the destination file
    lngDstFirstFileRow = lngDstLastRow + 1

    'Then, we need to find the NEW last row on the destination
    'sheet, which will be further down (since we pasted more
    'data in)
    lngDstLastRow = LastOccupiedRowNum(wksDst)
    lngDstLastCol = LastOccupiedColNum(wksDst)

    'With the info from above, we can create the range
    Set rngFile = .Range(.Cells(lngDstFirstFileRow, lngDstLastCol), _
    .Cells(lngDstLastRow, lngDstLastCol))

    ''CHECKPOINT: make sure we have correctly identified
    ''the range where our file names will go
    ''wksDst.Range("A1").Select
    ''rngFile.Select

    'Now that we have that range identified,
    'we write the file name
    rngFile.Value = wbkSrc.Name

    End With

    'Close the source workbook and repeat
    wbkSrc.Close SaveChanges:=False

    Next lngIdx

    'Let the user know that the combination is done!
    MsgBox "Data combined!"

    End Sub

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'INPUT : Sheet, the worksheet we'll search to find the last row
    'OUTPUT : Long, the last occupied row
    'SPECIAL CASE: if Sheet is empty, return 1
    Public Function LastOccupiedRowNum(Sheet As Worksheet) As Long
    Dim lng As Long
    If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
    With Sheet
    lng = .Cells.Find(What:="*", _
    After:=.Range("A1"), _
    Lookat:=xlPart, _
    LookIn:=xlFormulas, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, _
    MatchCase:=False).Row
    End With
    Else
    lng = 1
    End If
    LastOccupiedRowNum = lng
    End Function

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'INPUT : Sheet, the worksheet we'll search to find the last column
    'OUTPUT : Long, the last occupied column
    'SPECIAL CASE: if Sheet is empty, return 1
    Public Function LastOccupiedColNum(Sheet As Worksheet) As Long
    Dim lng As Long
    If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
    With Sheet
    lng = .Cells.Find(What:="*", _
    After:=.Range("A1"), _
    Lookat:=xlPart, _
    LookIn:=xlFormulas, _
    SearchOrder:=xlByColumns, _
    SearchDirection:=xlPrevious, _
    MatchCase:=False).Column
    End With
    Else
    lng = 1
    End If
    LastOccupiedColNum = lng
    End Function


    *********************************Code End****************************************************************************
    Last edited by saad101; 05-22-2019 at 03:43 PM.

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Hello, macro combining data but showing 0 as values for combined sheet

    Hi Saad101,

    Welcome to the forum. I think it would be appreciated if you have a look at the forum rules, especially #2 about code tags.

    Additionally, people will prove much more helpful if you attach a sample book instead of just trying to represent sheet data by typing some text... I know it is well intended, but most of us don't enjoy having to create a sheet to try and emulate the setup. This is especially true when I see that you are getting files (.xlsx) from a specific location, and again we can't test the existing code (or fixes) without knowing what those files are and what they contain.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    05-22-2019
    Location
    United states
    MS-Off Ver
    latest
    Posts
    2

    Re: Hello, macro combining data but showing 0 as values for combined sheet

    Thanks for directing me to the rules. As far as data is concerned i cant share the data as it is confidential but the sheet i created should be enough to clear the concept of what i am trying to say.

+ 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. Macro to search a range with multiple combined values
    By COURTTROOPER in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-01-2017, 02:27 PM
  2. [SOLVED] Consolidating identical data to new sheet and showing different values attached
    By dlevisay in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-07-2016, 03:39 PM
  3. Replies: 3
    Last Post: 09-29-2014, 07:21 AM
  4. [SOLVED] Macro to compare Sheet2 values with Sheet1 cells range (combined with Name Manager)
    By Manish_Gupta in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-17-2014, 08:26 AM
  5. Showing combined bar and line chart
    By Lemons83 in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 08-05-2014, 04:47 AM
  6. How to open a form No.2 from the combined values of 2 combo boxes from sheet 1 ?
    By Harmeet Singh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-23-2013, 03:07 AM
  7. [SOLVED] Combining two sets of data, showing one instance of each entry
    By andrewc in forum Excel General
    Replies: 2
    Last Post: 10-30-2013, 11:57 AM

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