I need to write a vba code that will copy only the cells in column A that have values…
- User
I need to write a vba code that will copy only the cells in column A that have values form sheets "Prem PM", "Prem AA", "Prem CL", Prem SU", and "Prem MG", in that order and paste into sheet "Observe" column E starting from row 2
- Assistant
Assuming that the sheets "Prem PM", "Prem AA", "Prem CL", "Prem SU", and "Prem MG" are in the same workbook, the following VBA code will achieve the desired result:
Sub CopyPremValues()
Dim PremSheets As Variant Dim ObserveSheet As Worksheet Dim LastRow As Long Dim CopyRange As Range Dim i As Long 'List of sheets to copy from PremSheets = Array("Prem PM", "Prem AA", "Prem CL", "Prem SU", "Prem MG") 'Destination sheet Set ObserveSheet = ThisWorkbook.Sheets("Observe") 'Loop through all the sheets in the list For i = LBound(PremSheets) To UBound(PremSheets) 'Check if the sheet exists in the workbook If WorksheetExists(PremSheets(i)) Then 'Get the last row in column A LastRow = ThisWorkbook.Sheets(PremSheets(i)).Cells(Rows.Count, "A").End(xlUp).Row 'Create a range object containing the values in column A Set CopyRange = ThisWorkbook.Sheets(PremSheets(i)).Range("A2:A" & LastRow).SpecialCells(xlCellTypeConstants) 'Copy the values to the Observe sheet ObserveSheet.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).Resize(CopyRange.Rows.Count, 1).Value = CopyRange.Value End If Next i - User
I am getting a compile error: ByRef argument type mismatch on "PremSheets" in this line of code - If WorksheetExists(PremSheets(i)) Then
- Assistant
It seems that the "PremSheets" parameter is not declared as an array in the WorksheetExists function. To fix the error, you can modify the WorksheetExists function as follows:
Function WorksheetExists(shtName As String, PremSheets As Variant) As Boolean WorksheetExists = False On Error Resume Next WorksheetExists = Len(Worksheets(shtName).Name) > 0 And IsNumeric(Application.Match(shtName, PremSheets, 0)) End FunctionThis will make the function accept the "PremSheets" array as an additional parameter, and use the Application.Match function to check if the sheet name exists in the array. You also need to update the function call in the CopyPremValues subroutine as follows:
If WorksheetExists(PremSheets(i), PremSheets) ThenThis will pass the "PremSheets" array as a parameter to the WorksheetExists function. After making these changes, the code should run without errors.
- User
Still getting exactly the same error message
- Assistant
I see. In that case, you can try explicitly declaring the data type of the "PremSheets" parameter in the WorksheetExists function as follows:
Function WorksheetExists(shtName As String, PremSheets() As String) As Boolean WorksheetExists = False On Error Resume Next WorksheetExists = Len(Worksheets(shtName).Name) > 0 And IsNumeric(Application.Match(shtName, PremSheets, 0)) End FunctionThis declares "PremSheets" as a string array, which should match the data type of the "PremSheets" variable in the CopyPremValues subroutine. You should also make sure that the "PremSheets" variable is properly initialized as a string array:
Dim PremSheets() As String PremSheets = Array("Prem PM", "Prem AA", "Prem CL", "Prem SU", "Prem MG")With these changes, the ByRef argument type mismatch error should be resolved.
- User
Somewhere in the event, I am getting a type mismatch error: Sub CopyPremValues()
Dim PremSheets() As String Dim ObserveSheet As Worksheet Dim LastRow As Long Dim CopyRange As Range Dim i As Long
'List of sheets to copy from PremSheets = Array("Prem PM", "Prem AA", "Prem CL", "Prem SU", "Prem MG")
'Destination sheet Set ObserveSheet = ThisWorkbook.Sheets("Observe")
'Loop through all the sheets in the list For i = LBound(PremSheets) To UBound(PremSheets)
'Check if the sheet exists in the workbook If WorksheetExists(PremSheets(i), PremSheets) Then
'Get the last row in column A LastRow = ThisWorkbook.Sheets(PremSheets(i)).Cells(Rows.Count, "A").End(xlUp).Row
'Create a range object containing the values in column A Set CopyRange = ThisWorkbook.Sheets(PremSheets(i)).Range("A2:A" & LastRow).SpecialCells(xlCellTypeConstants)
'Copy the values to the Observe sheet ObserveSheet.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).Resize(CopyRange.Rows.Count, 1).Value = CopyRange.Value
End If
Next i
End Sub
- Assistant
It seems that the issue may be related to the WorksheetExists function.
Try modifying the WorksheetExists function as follows:
Function WorksheetExists(shtName As String, PremSheets As Variant) As Boolean WorksheetExists = False On Error Resume Next WorksheetExists = Len(Worksheets(shtName).Name) > 0 And IsNumeric(Application.Match(shtName, PremSheets, 0)) On Error GoTo 0 End FunctionThis modification includes resetting the error handling with "On Error GoTo 0" after the WorksheetExists function is executed.
This will help to avoid the type mismatch error that you are getting.
- User
After the changes, still the same error
- Assistant
In that case, it might be helpful to include additional error handling in the WorksheetExists function to handle cases where an error is raised by the Application.Match method.
Please try modifying the WorksheetExists function as follows:
Function WorksheetExists(shtName As String, PremSheets As Variant) As Boolean Dim i As Long On Error Resume Next For i = LBound(PremSheets) To UBound(PremSheets) If Worksheets(PremSheets(i)).Name = shtName Then WorksheetExists = True Exit Function End If Next i WorksheetExists = False End FunctionThis modified version of the function loops through all the sheets in the "PremSheets" array and compares their names to the sheet name passed as a parameter. If a match is found, the function returns True.
Try replacing the existing WorksheetExists function with this modified version and testing to see if it resolves the type mismatch error.
- User
I have tried to simplify the code because the other sheets will always exist. But I am still getting a type mismatch error. Here is the new code. . . . Sub CopyPremValues()
Dim PremSheets() As String Dim ObserveSheet As Worksheet Dim LastRow As Long Dim CopyRange As Range Dim i As Long
PremSheets = Array("Prem PM", "Prem AA", "Prem CL", "Prem SU", "Prem MG")
Set ObserveSheet = ThisWorkbook.Sheets("Observe")
For i = LBound(PremSheets) To UBound(PremSheets)
LastRow = ThisWorkbook.Sheets(PremSheets(i)).Cells(Rows.Count, "A").End(xlUp).Row
Set CopyRange = ThisWorkbook.Sheets(PremSheets(i)).Range("A2:A" & LastRow).SpecialCells(xlCellTypeConstants)
ObserveSheet.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).Resize(CopyRange.Rows.Count, 1).Value = CopyRange.Value
Next i
End Sub
- Assistant
The issue may be that the SpecialCells method could be returning a range that includes a non-constant (e.g. a formula) cell, which could cause a type mismatch when trying to copy its value to the Observe sheet.
Try changing the SpecialCells(xlCellTypeConstants) method to SpecialCells(xlCellTypeVisible), which will include only the visible (i.e. not filtered out) cells in the range. Here is the updated code:
Sub CopyPremValues() Dim PremSheets() As String Dim ObserveSheet As Worksheet Dim LastRow As Long Dim CopyRange As Range Dim i As Long PremSheets = Array("Prem PM", "Prem AA", "Prem CL", "Prem SU", "Prem MG") Set ObserveSheet = ThisWorkbook.Sheets("Observe") For i = LBound(PremSheets) To UBound(PremSheets) LastRow = ThisWorkbook.Sheets(PremSheets(i)).Cells(Rows.Count, "A").End(xlUp).Row Set CopyRange = ThisWorkbook.Sheets(PremSheets(i)).Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible) ObserveSheet.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).Resize(CopyRange.Rows.Count, 1).Value = CopyRange.Value Next i End SubThis should prevent any non-constant cells from being included in the copied range, and hopefully avoid the type mismatch error.
- User
Still getting type mismatch in the code. . . . Sub CopyPremValues()
Dim PremSheets() As String Dim ObserveSheet As Worksheet Dim LastRow As Long Dim CopyRange As Range Dim i As Long
PremSheets = Array("Prem PM", "Prem AA", "Prem CL", "Prem SU", "Prem MG")
Set ObserveSheet = ThisWorkbook.Worksheets("Observe")
For i = LBound(PremSheets) To UBound(PremSheets)
LastRow = ThisWorkbook.Worksheets(PremSheets(i)).Cells(Rows.Count, "A").End(xlUp).Row
Set CopyRange = ThisWorkbook.Worksheets(PremSheets(i)).Range("A2:A" & LastRow) '.SpecialCells(xlCellTypeVisible)
ObserveSheet.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).Resize(CopyRange.Rows.Count, 1).Value = CopyRange.Value
Next i
End Sub
- Assistant
I see, there is a chance that some of the cells in the "A2:A" & LastRow range may contain errors or formulas that are causing the type mismatch error.
To handle this, you can modify the code to loop through each cell in the range and only copy the cells that are not errors or formulas. Here is an updated version of the code to try:
Sub CopyPremValues() Dim PremSheets() As String Dim ObserveSheet As Worksheet Dim LastRow As Long Dim CopyRange As Range Dim i As Long Dim j As Long PremSheets = Array("Prem PM", "Prem AA", "Prem CL", "Prem SU", "Prem MG") Set ObserveSheet = ThisWorkbook.Sheets("Observe") For i = LBound(PremSheets) To UBound(PremSheets) LastRow = ThisWorkbook.Sheets(PremSheets(i)).Cells(Rows.Count, "A").End(xlUp).Row Set CopyRange = ThisWorkbook.Sheets(PremSheets(i)).Range("A2:A" & LastRow) For j = 1 To CopyRange.Cells.Count If Not IsError(CopyRange.Cells(j)) And Not CopyRange.Cells(j).HasFormula Then ObserveSheet.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).Value = CopyRange.Cells(j).Value End If Next j Next i End SubThis version loops through each cell in the "A2:A" & LastRow range, and checks whether it is an error or contains a formula. If the cell does not meet either of these criteria, its value is copied to the "Observe" sheet.