The program's main function is to copy all the data in the worksheets opened and put it in a worksheet named "consolidated." Everything's working well, however, I want to add a column called "Data Source" wherein it can state where the data came from (e.g. Sheet1, Sheet2). But the thing is, it outputs the wrong sheet name.
I have tried different codes and I have attached what currently works below. I will highlight the part where I assume the problem comes from. I would really appreciate some help as I'm not a coder myself (just learned this a week ago).
For Each wksSrc In ThisWorkbook.Worksheets
'Skip Destination worksheet
If wksSrc.Name <> wksDst.Name And wksSrc.Name <> "Tool" Then
With wksSrc
'Identify the last row and column on this sheet
'so we know when to stop looping through the data
lngLastSrcRowNum = LastOccupiedRowNum(wksSrc)
lngLastSrcColNum = LastOccupiedColNum(wksSrc)
'Identify the last row of the Destination sheet
'so we know where to (eventually) paste the data
lngLastDstRowNum = LastOccupiedRowNum(wksDst)
'Loop through the headers on this sheet, looking up
'the appropriate Destination column from the Final
'Headers dictionary and creating ranges on the fly
For lngIdx = 1 To lngLastSrcColNum
strColHeader = Trim(CStr(.Cells(1, lngIdx)))
'Set the Destination target range using the
'looked up value from the Final Headers dictionary
Set rngDst = wksDst.Cells(lngLastDstRowNum + 1, _
dicFinalHeaders(strColHeader))
'Set the source target range using the current
'column number and the last-occupied row
Set rngSrc = .Range(.Cells(2, lngIdx), _
.Cells(lngLastSrcRowNum, lngIdx))
'Copy the data from this sheet to the destination
rngSrc.Copy Destination:=rngDst
Next lngIdx
Dim TargetColumn As Long
Dim FinalRow As Long
Dim rngAddress As Range
Dim i As Long
With wksDst
FinalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rngAddress = .Range("A:Z").Find("Data Source")
If Not rngAddress Is Nothing Then
TargetColumn = rngAddress.Column
''''THIS IS THE PART WHERE I ASSUME THE PROBLEM COMES FROM'''''
For i = 1 To FinalRow
.Cells(i, TargetColumn).Value = wksSrc.Name
Next i
End If
End With
End With
End If
Next wksSrc
End Sub
I already added the "Data Source" column but the wksSrc.Name part in the code always outputs only the last worksheet name when it should be different/dynamic depending on the worksheets where I got the data from.
This might help. When I make the i value to 1, it outputs the correct sheet name, however, it replaces the column header with the sheet name and the loop is only done once. When I make the i value to 2, it outputs the wrong sheet name but it starts to output in the correct first blank row of the Data Source column.
As mentioned in the comments. You are looping through the ENTIRE source name column each time. As such, at the end of your process you will be left with the last worksheet looped through. So in order to overcome this you need a StartRow as well as a FinalRow to only loop through data from that sheet. See below code (not tested) but I think you will get the idea of how to implement and it may work off the bat.
For Each wksSrc In ThisWorkbook.Worksheets
'Skip Destination worksheet
If wksSrc.Name <> wksDst.Name And wksSrc.Name <> "Tool" Then
With wksSrc
'Identify the last row and column on this sheet
'so we know when to stop looping through the data
lngLastSrcRowNum = LastOccupiedRowNum(wksSrc)
lngLastSrcColNum = LastOccupiedColNum(wksSrc)
'Identify the last row of the Destination sheet
'so we know where to (eventually) paste the data
lngLastDstRowNum = LastOccupiedRowNum(wksDst)
'Loop through the headers on this sheet, looking up
'the appropriate Destination column from the Final
'Headers dictionary and creating ranges on the fly
For lngIdx = 1 To lngLastSrcColNum
strColHeader = Trim(CStr(.Cells(1, lngIdx)))
'Set the Destination target range using the
'looked up value from the Final Headers dictionary
Set rngDst = wksDst.Cells(lngLastDstRowNum + 1, _
dicFinalHeaders(strColHeader))
'Set the source target range using the current
'column number and the last-occupied row
Set rngSrc = .Range(.Cells(2, lngIdx), _
.Cells(lngLastSrcRowNum, lngIdx))
'Copy the data from this sheet to the destination
rngSrc.Copy Destination:=rngDst
Next lngIdx
Dim TargetColumn As Long
Dim FinalRow As Long, StartRow As Long
Dim rngAddress As Range
Dim i As Long
With wksDst
Set rngAddress = .Range("A:Z").Find("Data Source")
If Not rngAddress Is Nothing Then
TargetColumn = rngAddress.Column
'set the start for this sheet
StartRow = lngLastDstRowNum + 1
'set the final row for this worksheet
FinalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range(.Cells(StartRow, TargetColumn), .Cells(FinalRow, TargetColumn)).Value = wksSrc.Name
End If
End With
End With
End If
Next wksSrc
As can be seen your range adjustment on the wksDst should take into account the sheets data.
Related
I have a workbook that contains several sheets with different types of inventory and one summary sheet.
I am trying to use checkboxes, that if checked as "True", will copy that row of data and paste into the summary sheet starting on a specific row. Each inventory sheet has several rows of differing data and I'd like users to be able to check multiple boxes they need on each sheet and this data to be copied to the summary sheet.
I found this code below that is working for the most part except it skips over some lines of data that are marked as "true". It also adds an unnecessary extra row between the lines once it copies the data over to the new sheet. What can I change so that all of the data marked "true" can be copied over and eliminate the extra rows?
Code I found is from this video: https://www.youtube.com/watch?v=TJoRUwrEe0g
Sub CopyRowBasedOnCellValue()
Dim xRg As Range
Dim xCell As Range
Dim A As Long
Dim B As Long
Dim C As Long
A = Worksheets("Exterior Items").UsedRange.Rows.Count
B = Worksheets("Customer Sheet").UsedRange.Rows.Count
If B = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Customer Sheet").UsedRange) = 0 Then B = 0
End If
Set xRg = Worksheets("Exterior Items").Range("B1:B" & A)
On Error Resume Next
Application.ScreenUpdating = False
For B = 1 To xRg.Count
If CStr(xRg(B).Value) = "True" Then
xRg(B).EntireRow.Copy Destination:=Worksheets("Customer Sheet").Range("A" & B + 9)
B = B + 1
End If
Next
Application.ScreenUpdating = True
End Sub
Try this:
Sub Copy_table_where_B_is_TRUE_row_by_row()
'declarations
Dim shtSource As Worksheet
Dim shtDestination As Worksheet
'reference source and destination sheets
Set shtSource = Worksheets("Exterior Items")
Set shtDestination = Worksheets("Customer Sheet")
'find limits of tables present on source and destination sheets
LastRowSource = shtSource.Cells(shtSource.Rows.Count, "A").End(xlUp).Row
LastRowDestination = shtDestination.Cells(shtDestination.Rows.Count, "A").End(xlUp).Row
'set output row index
OutputRow = LastRowDestination + 1
'using the source table..
For Each rw In shtSource.Range("1:" & LastRowSource).Rows
'if 2nd cell in row is TRUE
If rw.Cells(2).Value = "True" Then
'copy to destination sheet
rw.Copy shtDestination.Cells(OutputRow, 1)
'increment output row index
OutputRow = OutputRow + 1
End If
Next
End Sub
An entirely different method, that doesn't require any loops or counters would be to use a filter:
Sub Copy_filtered_table_where_B_is_TRUE()
'declarations
Dim shtSource As Worksheet
Dim shtDestination As Worksheet
'reference source and destination sheets
Set shtSource = Worksheets("Exterior Items")
Set shtDestination = Worksheets("Customer Sheet")
'find limits of tables present on source and destination sheets
'(these can be manually set if source table is fixed and destination location is fixed)
LastRowSource = shtSource.Cells(shtSource.Rows.Count, "A").End(xlUp).Row
LastRowDestination = shtDestination.Cells(shtDestination.Rows.Count, "A").End(xlUp).Row
'using the source table..
With shtSource.Range("1:" & LastRowSource)
'apply a filter
.AutoFilter
'set filter to column 2 = True
.AutoFilter Field:=2, Criteria1:="True"
'copy cells visible after application of filter, to next available row on destination sheet
.SpecialCells(xlCellTypeVisible).Copy shtDestination.Cells(LastRowDestination + 1, 1)
'remove filter
shtSource.AutoFilterMode = False
End With
End Sub
I have been running into some issues trying to use VBA to compare 2 tables in different worksheets, and then copy any rows in the "Master" sheet that are not found in the "New" sheet. Both tables are formatted as tables. The match is based on an "ID" column in Column A of both tables. If an ID is in the "Master" sheet, but not in the "New" sheet, than that entire row should be copy and pasted to the end of the table in the "New" sheet.
I updated some code found in another forum, which is almost working. However, it only seems to paste over the ID data into Column A, and not the entire corresponding row of data which is needed.
Sub compare()
Dim i As Long
Dim lrs As Long
Dim lrd As Long
With Worksheets("Master")
lrs = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To lrs 'assumes header in row 1
If Application.IfError(Application.Match(.Cells(i, 1), Worksheets("New").Columns(1), 0), 0) = 0 Then
lrd = Worksheets("New").Cells(Worksheets("test").Rows.Count, 1).End(xlUp).Row
Worksheets("New").Cells(lrd + 1, 1).Value = .Cells(i, 1).Value
End If
Next i
End With
End Sub
I think the issue has to do with the "Cells" reference, instead of a range, but I do not know how to make that line dynamic.
Slightly different approach, but you need to use something like Resize() to capture the whole row, and not just the cell in Col A.
Sub compare()
Const NUM_COLS As Long = 10 'for example
Dim wb As Workbook, wsSrc As Worksheet, wsDest As Worksheet
Dim c As Range, cDest As Range
Set wb = ThisWorkbook 'or ActiveWorkbook for example
Set wsSrc = wb.Worksheets("Master")
Set wsDest = wb.Worksheets("New")
Set cDest = wsDest.Cells(Rows.Count, 1).End(xlUp).Offset(1) 'next empty row
For Each c In wsSrc.Range("A2:A" & wsSrc.Cells(Rows.Count, 1).End(xlUp).Row).Cells
If IsError(Application.Match(c.Value, wsDest.Columns(1), 0)) Then
cDest.Resize(1, NUM_COLS).Value = c.Resize(1, NUM_COLS).Value
Set cDest = cDest.Offset(1) 'next row
End If
Next c
End Sub
Currently I have a lot of sheets in my Excel file but I want to get the last row of data in sheets that start with "6" as their names as the other sheets are not relevant. In the sheets that start with 6 are all in the same format but have different number of rows, I am interested in extracting the last row in all those sheets (columns D:J) and placing it into a "master sheet". Since I am quite new to VBA, how would I go about doing that? Thanks in advance!
What I have currently that can copy one sheet and paste into my master sheet "Sheet2":
With Sheets("6363")
With Range(.Cells(.Rows.Count, "D").End(xlUp), _
.Cells(.Cells(.Rows.Count, "D").End(xlUp).row, .Columns.Count).End(xlToLeft))
Worksheets("Sheet2").Range("D1").Resize(, .Columns.Count).Value = .Value
End With
End With
This will copy the last row, columns D-J, from every sheet with a name that starts with '6'.
It will put the copied data into the next empty row, starting at column D, of 'Sheet2'.
Sub CopySix()
Dim wsSrc As Worksheet
Dim wsDst As Worksheet
Dim rngSrc As Range
Dim rngDst As Range
Set wsDst = ActiveWorkbook.Sheets("Sheet2")
Set rngDst = wsDst.Range("D" & Rows.Count).End(xlUp).Offset(1)
For Each wsSrc In ActiveWorkbook.Sheets
If Left(wsSrc.Name, 1) = "6" Then
Set rngSrc = wsSrc.Range("D" & Rows.Count).End(xlUp).Resize(, 7)
rngDst.Resize(, 7).Value = rngSrc.Value
Set rngDst = rngDst.Offset(1)
End If
Next wsSrc
End Sub
I have a workbook with 20+ worksheets all of the same format with the same header in row 1 and 2. I have a macro I cobbled together to pull all the data (except for a few worksheets called out) into one master in the same workbook. Works great, however, various users "own" each tab and when they decide to filter or hide columns in their worksheet and leave them the hidden/filtered data doesn't pull to the master properly.
Any suggestions for how to get all of the data from row 3 down regardless of filters/hidden from each worksheet to the combined worksheet? I'd even survive if it just copied every cell in the worksheet to the master because the next sheet in the loop would past at the last occupied row and I could filter the repeated headers easily enough.
Worst case I could add code to unfilter and unhide everything before running but they want to come back to their tabs with their filters in place for convenience sigh. I never know what they are filtering/hiding for so I'm not sure how I could do that then put it back smartly either.
Many thanks in advance
LastOccupiedRowNum/LastOccupiedColNum functions are defined elsewhere
Public Sub CombineDataFromAllSheets()
Application.ScreenUpdating = False
Dim wksSrc As Worksheet, wksDst As Worksheet
Dim rngSrc As Range, rngDst As Range
Dim lngLastCol As Long, lngSrcLastRow As Long, lngDstLastRow As Long
'Notes: "Src" is short for "Source", "Dst" is short for "Destination"
'Clean old data first
Sheets("Data DO NOT EDIT").Select
Rows("3:2000").Select
Selection.Delete Shift:=xlUp
'Set references
Set wksDst = ThisWorkbook.Worksheets("Data DO NOT EDIT")
lngDstLastRow = LastOccupiedRowNum(wksDst)
lngLastCol = LastOccupiedColNum(wksDst)
'Set the initial destination range
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)
'Loop through all sheets
For Each wksSrc In ThisWorkbook.Worksheets
'Skip these
If wksSrc.Name <> "Acronyms" And _
wksSrc.Name <> "Template" And _
wksSrc.Name <> "Permitter" And _
wksSrc.Name <> "Plans" And _
wksSrc.Name <> "Summary DO NOT EDIT" Then
'Identify last occupied row
lngSrcLastRow = LastOccupiedRowNum(wksSrc)
'Store data then copy it to destination
With wksSrc
Set rngSrc = .Range(.Cells(3, 1), .Cells(lngSrcLastRow, lngLastCol))
rngSrc.Copy Destination:=rngDst
End With
'Redefine destination range to next empty row
lngDstLastRow = LastOccupiedRowNum(wksDst)
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)
End If
Next wksSrc
Application.ScreenUpdating = True
End Sub
I am assuming that you data is not a table, try adding this in the For loop
wksSrc.Rows.EntireRow.Hidden = False
wksSrc.Columns.EntireColumn.Hidden = False
On Error Resume Next
wksSrc.ShowAllData
On Error GoTo 0
I have 6 different headers under the WIPTX worksheet that will be pulling information from the TestData tab which is essentially data that will be uploaded from a SharePoint site. I want to be able to copy and paste rows that have specific values like the type of status or by name
under each header in the WIPTX worksheet. Headers are in columns A-C, E-G, I-K, M-O, Q-S, and U-W. Headers are of different status's that are in the TestData worksheet. Status include Assigned, Accepted, In Progress, On Hold, Completed, and Cancelled.
Will this be possible?
Code that I have so far works but it does not paste under specific header columns.
I have tried researching and looing at other sources but I am still not able to find the right code that is specific to what I am looking for.
Sub Update1()
Dim LastRow1 As Long, LastRow2 As Long, i As Long
With ThisWorkbook.Worksheets("TestData")
LastRow1 = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow1
If .Range("A" & i).Value = "Thomas Xiong" Then
LastRow2 = ThisWorkbook.Worksheets("All Projects with NetBuilds").Cells(ThisWorkbook.Worksheets("All Projects with NetBuilds").Rows.Count, "A").End(xlUp).Row
.Rows(i).Copy ThisWorkbook.Worksheets("All Projects with NetBuilds").Rows(LastRow2 + 1)
End If
Next i
End With
End Sub
Is this possible?
I think this should help you:
Option Explicit
Sub Update1()
Dim wsData As Worksheet, wsProjects As Worksheet, LastRow As Long, Col As Integer, CopyRange As Range, C As Range
With ThisWorkbook
Set wsData = .Sheets("TestData") 'refering the worksheet with all the data
Set wsProjects = .Sheets("All Projects with NetBuilds") 'refering the worksheet with the headers
End With
For Each C In wsData.Range("A2", wsData.Cells(1, 1).End(xlDown)) 'Lets assume the criteria is on the column A
With wsData
Select Case C.Value
Case "Assigned"
With wsData
Set CopyRange = .Range(.Cells(C.Row, 3), .Cells(C.Row, 5)) 'Here I'm assuming you want to copy data from Columns B To D
End With
Case "Accepted"
With wsData
Set CopyRange = .Range(.Cells(C.Row, 7), .Cells(C.Row, 9)) 'Here I'm assuming you want to copy data from Columns G To I
End With
'... all your headers
End Select
End With
With wsProjects
Col = .Cells.Find(C).Column 'Find the header column
LastRow = .Cells(.Rows.Count, Col).End(xlUp).Row + 1 'find the last row on that header
CopyRange.Copy .Cells(LastRow, Col) 'paste the range (this method will copy everything from the source)
End With
Next C
'In case you are always copying the same range of cells skip the select case, delete the CopyRange variable and just copy paste on the last block
End Sub