Hi, I am fairly new to vba and I am trying to copy data from multiple sheets except from sheet names that are in the defaultsheetnames array.
DefaultSheetNames = Array("Admin", "QBBOM", "Index", "Master", "Template", "Revision Log", "Instructions", "Sample", "Deleted Items", "ChangeLog", "PartValidation", "1ef699ff-cb2f-4875-a1d3-6832011", "Table1")
I would like to copy data from row A11 to the dynamic last row defined by the variable LR (Last Row) from each sheet.
LR = Cells(Rows.Count, 1).End(xlUp).Row
Finally I would like to paste them in the Sheets("Master") starting at row A13 with no formatting to the data copied.
Below are some pictures inserted, they are also attached.
Below is the Picture of the data of an example sheet to be copied. Copy data from a11 (A00) to Last row.
Below is the Picture of the various sheet tab names and the destination sheet layout. Paste data from A13.
I am encountering errors and I do not know where to spot the mistake
Any help would be greatly appreciated.
DefaultSheetNames = Array("Admin", "QBBOM", "Index", "Master", "Template", "Revision Log", "Instructions", "Sample", "Deleted Items", "ChangeLog", "PartValidation", "1ef699ff-cb2f-4875-a1d3-6832011", "Table1")
I would like to copy data from row A11 to the dynamic last row defined by the variable LR (Last Row) from each sheet.
LR = Cells(Rows.Count, 1).End(xlUp).Row
Finally I would like to paste them in the Sheets("Master") starting at row A13 with no formatting to the data copied.
Below are some pictures inserted, they are also attached.
Below is the Picture of the data of an example sheet to be copied. Copy data from a11 (A00) to Last row.
Below is the Picture of the various sheet tab names and the destination sheet layout. Paste data from A13.
I am encountering errors and I do not know where to spot the mistake
Any help would be greatly appreciated.
VBA Code:
Sub Master_Creation()
Dim wkbk As Workbook: Set wkbk = ThisWorkbook
Dim wksht As Worksheet
Dim LR As Long, FirstBlankrow As Long
Dim DefaultSheetNames As Variant
'below are the sheet names I dont want data copied from but I want the destination sheet to be Master
DefaultSheetNames = Array("Admin", "QBBOM", "Index", "Master", "Template", "Revision Log", "Instructions", "Sample", "Deleted Items", "ChangeLog", "PartValidation", "1ef699ff-cb2f-4875-a1d3-6832011", "Table1")
Sheets("Master").Range("A13", "N10000").ClearContents ' clearing data on destination sheet before pasting
For Each wksht In Worksheets 'looping through worksheets
LR = Cells(Rows.Count, 1).End(xlUp).Row ' hopefully dynamic last row of each worksheet
If IsInArray(wksht.Name, DefaultSheetNames) = False Then ' hopefully this doesnt copy data from defaultsheetnames array
On Error GoTo CompatibilitySheetIssue
For i = 11 To LR
Debug.Print LR
FirstBlankrow = wksht("Master").Cells(Rows.Count, 1).End(xlUp).Row + 1 ' starting at first empty row
wksht.Rows(i).Copy Destination:=wksht("Master").Rows(FirstBlankrow)
Next i
End If
Next wksht
Done:
Exit Sub
CompatibilitySheetIssue:
MsgBox "Please check if anything realted to the Sheets have any errors."
End Sub
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim i
For i = LBound(arr) To UBound(arr)
If arr(i) = stringToBeFound Then
IsInArray = True
Exit Function
End If
Next i
IsInArray = False
End Function