Ok, I have this very complex code that works most of the time for a daily report, but isn't completely dependable.
This Macro is trying to copy columns B and L for every worksheet into 1 worksheet in a new Workbook(in columns A and B).
The problem is, sometimes all 3 worksheets are present and sometimes only 1 or 2, sometimes the worksheet names change, and sometimes come in blank, which sometimes breaks the code.
To prevent this, I think I want to use a "For Each Worksheet in ThisWorkbook" statement.
How do you recommend achieving this?
Thanks.
This Macro is trying to copy columns B and L for every worksheet into 1 worksheet in a new Workbook(in columns A and B).
The problem is, sometimes all 3 worksheets are present and sometimes only 1 or 2, sometimes the worksheet names change, and sometimes come in blank, which sometimes breaks the code.
To prevent this, I think I want to use a "For Each Worksheet in ThisWorkbook" statement.
How do you recommend achieving this?
Thanks.
Code:
Function WorksheetExists(SheetName As String, lr As Long) As Boolean
Dim ws As Worksheet
WorksheetExists = False
For Each ws In Worksheets
If ws.Name = SheetName Then
WorksheetExists = True
Exit For
End If
Next
End Function
Sub UBTCopy_Copy_Columns_B_And_L_to_New_WS()
'
' UBTCopy_And_Paste Macro
'
'
Dim ws1 As String, ws2 As String, ws3 As String
Dim S As String
Dim Ary As Variant
Dim fname As String, DestinationFileName As String
Dim SourceFileName As String
SourceFileName = ejFullDate & " EdJones A Share Restrictions Voids " & My_Initials & " " & ejFullDate4 & ".xlsx"
DestinationFileName = "529UBTREJ" & ejFullDate2 & ".xlsx"
Workbooks(SourceFileName).Activate
ws1 = UBT_WS1
ws2 = UBT_WS2
ws3 = UBT_WS3
' Find last row in column U with data
lr = Cells(Rows.Count, "U").End(xlUp).Row
If WorksheetExists(ws1) Then
' Copy data
lr = Cells(Rows.Count, "U").End(xlUp).Row
If lr > 3 Then
ws1Row_Start = 2
ws1Row_Count = Worksheets(ws1).Cells(Rows.Count, "U").End(xlUp).Row - 3
With Worksheets(ws1).UsedRange
Ary = Application.Index(.Value, .Worksheet.Evaluate("row(4:" & .Rows.Count & ")"), Array(2, 12))
'Pastes data in destination file in cell A2 Data:
End With
With Workbooks(DestinationFileName).ActiveSheet.Range("A" & ws1Row_Start).Resize(UBound(Ary), 2)
.NumberFormat = "@"
.Value = Ary
ActiveSheet.UsedRange.EntireColumn.AutoFit
ActiveSheet.UsedRange.EntireRow.AutoFit
End With
Else
ws1Row_Start = 2
ws1Row_Count = 0
End If
Else
ws1Row_Start = 2
ws1Row_Count = 0
End If
If WorksheetExists(ws2) Then
' Copy data
ws2Row_Start = ws1Row_Start + ws1Row_Count
ws2Row_Count = Worksheets(ws2).Cells(Rows.Count, "U").End(xlUp).Row - 3
With Worksheets(ws2).UsedRange
Ary = Application.Index(.Value, .Worksheet.Evaluate("row(4:" & .Rows.Count & ")"), Array(2, 12))
End With
'Pastes data in destination file under WS1 Data:
With Workbooks(DestinationFileName).ActiveSheet.Range("A" & ws2Row_Start).Resize(UBound(Ary), 2)
.NumberFormat = "@"
.Value = Ary
ActiveSheet.UsedRange.EntireColumn.AutoFit
ActiveSheet.UsedRange.EntireRow.AutoFit
End With
End If
If WorksheetExists(ws3) Then
' Copy data
ws3Row_Count = Worksheets(ws3).Cells(Rows.Count, "U").End(xlUp).Row - 3
ws3Row_Start = ws2Row_Start + ws2Row_Count
With Worksheets(ws3).UsedRange
Ary = Application.Index(.Value, .Worksheet.Evaluate("row(4:" & .Rows.Count & ")"), Array(2, 12))
End With
'pastes data in destination file under the WS1 and WS2 data:
With Workbooks(DestinationFileName).ActiveSheet.Range("A" & ws3Row_Start).Resize(UBound(Ary), 2)
.NumberFormat = "@"
.Value = Ary
ActiveSheet.UsedRange.EntireColumn.AutoFit
ActiveSheet.UsedRange.EntireRow.AutoFit
End With
End If
Workbooks(DestinationFileName).Activate
lr = Cells(Rows.Count, "A").End(xlUp).Row
Range("A2:A" & lr).Copy
'Saving Account Numbers to Notepad on Desktop:
Workbooks.Add
'
Range("A1").PasteSpecial Paste:=xlPasteValues
'
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=My_Desktop & "Notepad.txt", FileFormat:=xlText
ActiveWorkbook.SaveAs FileName:=TEMPLATES_FOLDER & "Notepad.txt", FileFormat:=xlText
Application.DisplayAlerts = True
ActiveWorkbook.Close False
With ActiveWindow
.WindowState = xlNormal
.Width = 400
.Height = 591.75
.Left = 1000
.Top = 0
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub