VBA for copying all data from 8 sheets in a single workbook to a new sheet in new workbook

sdhasan

New Member
Joined
Oct 12, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

Need help in creating a macro which will be saved in the workbook containing 8 sheets with the same table. The macro will copy all the data in the table in those eight sheets and will paste it in a new sheet in a new workbook. The fourth row in each sheet is the header of the table which needs to be copied only once.

1666444448677.png
 
Hi Danish,

I hope you are well again.

At the end of my code I was taking care of what you are asking now: converting everything to values, taking away any formats, apply a ListObject to the area with

VBA Code:
'...
With wsNew.UsedRange
  .Value = .Value
  .Borders(xlDiagonalDown).LineStyle = xlNone
  .Borders(xlDiagonalUp).LineStyle = xlNone
  .Borders(xlEdgeLeft).LineStyle = xlNone
  .Borders(xlEdgeTop).LineStyle = xlNone
  .Borders(xlEdgeBottom).LineStyle = xlNone
  .Borders(xlEdgeRight).LineStyle = xlNone
  .Borders(xlInsideVertical).LineStyle = xlNone
  .Borders(xlInsideHorizontal).LineStyle = xlNone
  With .Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
  End With
  With .Font
    .Name = "Calibri"
    .FontStyle = "Standard"
    .Size = 10
    .Bold = False
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
  End With
  wsNew.ListObjects.Add(xlSrcRange, wsNew.Range("A1").CurrentRegion, , xlYes).Name = "Table1"
  .HorizontalAlignment = xlGeneral
  .WrapText = False
  .Orientation = 0
  .AddIndent = False
  .IndentLevel = 0
  .ShrinkToFit = False
  .ReadingOrder = xlContext
  .MergeCells = False
  .Rows(1).Font.Bold = True
  .EntireColumn.AutoFit
End With
'...

Altered code:
VBA Code:
Sub MrE161350C_CopyValues()
'https://www.mrexcel.com/board/threads/vba-for-copying-all-data-from-8-sheets-in-a-single-workbook-to-a-new-sheet-in-new-workbook.1220016/
'/// changed by HaHoBe, 221105
'/// reason: user request for values only in Target Sheet
Dim wbWork                      As Workbook
Dim wbNew                       As Workbook
Dim ws                          As Worksheet
Dim wsNew                       As Worksheet

Dim blnRngCopy                  As Boolean

Dim lngStart                    As Long
Dim lngCounter                  As Long
Dim lngLastRow                  As Long
Dim lngEmptyRows                As Long
Dim lngStartRow                 As Long
Dim lngCalcMeth                 As Long

Dim rng2Copy                    As Range
Dim rngCell                     As Range
Dim rngArea                     As Range

Dim strColumn                   As String
Dim strSC                       As String
Dim strStartCol                 As String
Dim strEndCol                   As String
Dim strDestCol                  As String

'no screen update, calculation is set to Manual
With Application
  .ScreenUpdating = False
  lngCalcMeth = .Calculation
  .Calculation = xlCalculationManual
End With

On Error GoTo err_here

'!!! workbook must be open, please change name and extension to suit
Set wbWork = Workbooks("sdhasan Taxi Master Attendance_Sample Festwerte.xlsx")

Set wbNew = Workbooks.Add(1)
Set wsNew = wbNew.Sheets(1)
wsNew.Range("A1:M1").Value = wbWork.Worksheets("Airport Taxi").Range("A4:M4").Value
lngStart = 2

For Each ws In wbWork.Worksheets
  Select Case ws.Name
   
    Case "Airport Taxi", "Airport Taxi (Temp. driver)", "Karwa White", "Karwa White (Temporary)"
      With ws
        For lngCounter = 1 To 4
          blnRngCopy = True
          Select Case lngCounter
            Case 1
              strColumn = "C":  strStartCol = "A":  strEndCol = "M":  strDestCol = "A"
            Case 2
              strColumn = "N":  strStartCol = "N":  strEndCol = "U":  strDestCol = "B"
              lngStart = wsNew.Cells(Rows.Count, "A").End(xlUp).Row + 1
            Case 3
              strColumn = "O":  strStartCol = "O":  strEndCol = "O":  strDestCol = "A"
            Case 4
              strColumn = "O":  strStartCol = "J":  strEndCol = "M":  strDestCol = "J"
            Case Else
              strColumn = "O":  strStartCol = vbNullString:  strEndCol = vbNullString:  strDestCol = "J"
          End Select
          lngStartRow = 5
          lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
          If lngLastRow > 0 Then
            lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
            If lngLastRow >= lngStartRow Then
              Select Case lngEmptyRows
                Case Is > 0
                  If .Range(strColumn & lngLastRow).HasFormula Then
                    strSC = xlCellTypeFormulas
                  Else
                    strSC = xlCellTypeConstants
                  End If
                  Select Case lngCounter
                    Case 1, 2
                      Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strStartCol & lngLastRow).SpecialCells(strSC)
                      If rng2Copy.Areas.Count = 1 Then
                        Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
                      Else
                        For Each rngArea In rng2Copy.Areas
                          For Each rngCell In rngArea
'/// begin change 221105_01
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'                            .Range(rngCell, rngCell.Offset(0, 7)).Copy _
'                                wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1, 0)
'                            .Range(.Range("J" & rngCell.Row), .Range("M" & rngCell.Row)).Copy _
'                                wsNew.Range("J" & wsNew.Cells(Rows.Count, "A").End(xlUp).Row + 1)
'                            .Range(rngCell, rngCell.Offset(0, 7)).Copy _
'                                wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1, 0)
'/// new codeline(s)
                            wsNew.Range("J" & wsNew.Cells(Rows.Count, "A").End(xlUp).Row + 1).Value = _
                                .Range(.Range("J" & rngCell.Row), .Range("M" & rngCell.Row)).Value
                            wsNew.Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = .Range("A" & rngCell.Row).Value
                            wsNew.Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = .Range("A" & rngCell.Row).Value
'/// end change 221105_01
                          Next rngCell
                        Next rngArea
                        blnRngCopy = False
                      End If
                    Case 3, 4
                      blnRngCopy = False
                  End Select
                Case Else
                  Select Case lngCounter
                    Case 1, 2, 4
                      Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
                    Case 3
                      Set rng2Copy = .Range("A" & lngStartRow & ":A" & lngLastRow)
                End Select
              End Select
'/// begin change 221105_02
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'              If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
              If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_02
            End If
          Else
            Select Case MsgBox("Problems copying data from Sheet '" & .Name & "*." & vbCrLf & _
                  "Cancel or Continue?", vbOKCancel, "Now what...?")
              Case vbCancel
                wbNew.Close False
                GoTo end_here
              Case Else
                'continue
            End Select
          End If
        Next lngCounter
      End With
   
    Case "Revenue Share Scheme"
      blnRngCopy = True
      lngStartRow = 5
      With ws
        strColumn = "C":  strStartCol = "A":  strEndCol = "M":  strDestCol = "A"
        lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
        If lngLastRow > 0 Then
          lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
          If lngLastRow >= lngStartRow Then
            Select Case lngEmptyRows
              Case Is > 0
                If .Range(strColumn & lngLastRow).HasFormula Then
                  strSC = xlCellTypeFormulas
                Else
                  strSC = xlCellTypeConstants
                End If
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strStartCol & lngLastRow).SpecialCells(strSC)
                If rng2Copy.Areas.Count = 1 Then
                  Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
                Else
                  For Each rngArea In rng2Copy.Areas
                    For Each rngCell In rngArea
'/// begin change 221105_03
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'                      .Range(.Cells(rngCell.Row, strStartCol), .Cells(rngCell.Row, strEndCol)).Copy _
'                          wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1)
'/// new codeline(s)
                      wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1).Value = _
                          .Range(.Cells(rngCell.Row, strStartCol), .Cells(rngCell.Row, strEndCol)).Value
'/// end change 221105_03
                    Next rngCell
                  Next rngArea
                  blnRngCopy = False
                End If
              Case Else
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
            End Select
'/// begin change 221105_04
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'            If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
              If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_04
          Else
            Select Case MsgBox("Problems copying data from Sheet '" & .Name & "'." & vbCrLf & _
                  "Cancel or Continue?", vbOKCancel, "Now what...?")
              Case vbCancel
                wbNew.Close False
                GoTo end_here
              Case Else
                'continue
            End Select
          End If
        End If
      End With
   
    Case "Annual Leave"
      blnRngCopy = True
      lngStartRow = 3
      With ws
        strColumn = "C":  strStartCol = "A":  strEndCol = "G":  strDestCol = "A"
        lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
        If lngLastRow > 0 Then
          lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
          If lngLastRow >= lngStartRow Then
            Select Case lngEmptyRows
              Case Is > 0
                If .Range(strColumn & lngLastRow).HasFormula Then
                  strSC = xlCellTypeFormulas
                Else
                  strSC = xlCellTypeConstants
                End If
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strStartCol & lngLastRow).SpecialCells(strSC)
                If rng2Copy.Areas.Count = 1 Then
                  Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
                Else
                  For Each rngArea In rng2Copy.Areas
                    For Each rngCell In rngArea
'/// begin change 221105_05
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'                      .Range(.Cells(rngCell.Row, strStartCol), .Cells(rngCell.Row, strEndCol)).Copy _
'                          wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1)
'/// new codeline(s)
                      wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1).Resize(1, 7).Value = _
                          .Range(.Cells(rngCell.Row, strStartCol), .Cells(rngCell.Row, strEndCol)).Value
'/// end change 221105_05
                    Next rngCell
                  Next rngArea
                  blnRngCopy = False
                End If
              Case Else
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
            End Select
'/// begin change 221105_06
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'            If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
            If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_06
          End If
        Else
          Select Case MsgBox("Problems copying data from Sheet '" & .Name & "'." & vbCrLf & _
          "Cancel or Continue?", vbOKCancel, "Now what...?")
            Case vbCancel
              wbNew.Close False
              GoTo end_here
            Case Else
              'continue
          End Select
        End If
      End With
   
    Case "Resign & Termination"
      blnRngCopy = True
      lngStartRow = 2
      With ws
        strColumn = "C":  strStartCol = "A":  strEndCol = "E":  strDestCol = "A"
        lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
        If lngLastRow > 0 Then
          lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
          If lngLastRow >= lngStartRow Then
            Select Case lngEmptyRows
              Case Is > 0
                If .Range(strColumn & lngLastRow).HasFormula Then
                  strSC = xlCellTypeFormulas
                Else
                  strSC = xlCellTypeConstants
                End If
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
              Case Else
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
            End Select
'/// begin change 221105_07
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'            If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
            If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_07
          End If
        Else
          Select Case MsgBox("Problems copying data from Sheet '" & .Name & "'." & vbCrLf & _
                "Cancel or Continue?", vbOKCancel, "Now what...?")
            Case vbCancel
              wbNew.Close False
              GoTo end_here
            Case Else
              'continue
          End Select
        End If
      End With
   
    Case "Incident"
      With ws
        For lngCounter = 1 To 2
          blnRngCopy = True
          lngStartRow = 2
           Select Case lngCounter
            Case 1
              strColumn = "C":  strStartCol = "A":  strEndCol = "E":  strDestCol = "A"
            Case 2
              strColumn = "C":  strStartCol = "H":  strEndCol = "H":  strDestCol = "H"
          End Select
          lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
          If lngLastRow > 0 Then
            lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
            If lngLastRow >= lngStartRow Then
              Select Case lngEmptyRows
                Case Is > 0
                  If .Range(strColumn & lngLastRow).HasFormula Then
                    strSC = xlCellTypeFormulas
                  Else
                    strSC = xlCellTypeConstants
                  End If
                  Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
                Case Else
                  Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
              End Select
'/// begin change 221105_08
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'            If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
            If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_08
            End If
          Else
            Select Case MsgBox("Problems copying data from Sheet '" & .Name & "'." & vbCrLf & _
                  "Cancel or Continue?", vbOKCancel, "Now what...?")
              Case vbCancel
                wbNew.Close False
                GoTo end_here
              Case Else
                'continue
            End Select
          End If
        Next lngCounter
      End With
   
    Case Else
  End Select
  lngStart = wsNew.Cells(Rows.Count, "A").End(xlUp).Row + 1
Next ws

wsNew.Columns(5).NumberFormat = wbWork.Worksheets("Airport Taxi").Range("E5").NumberFormat 'time values
wsNew.Columns(4).Copy wsNew.Range("N1")
wsNew.Range("N1").Value = "Status"
'/// begin change 221105_09
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'With wsNew.UsedRange
'  .Value = .Value
'  .Borders(xlDiagonalDown).LineStyle = xlNone
'  .Borders(xlDiagonalUp).LineStyle = xlNone
'  .Borders(xlEdgeLeft).LineStyle = xlNone
'  .Borders(xlEdgeTop).LineStyle = xlNone
'  .Borders(xlEdgeBottom).LineStyle = xlNone
'  .Borders(xlEdgeRight).LineStyle = xlNone
'  .Borders(xlInsideVertical).LineStyle = xlNone
'  .Borders(xlInsideHorizontal).LineStyle = xlNone
'  With .Interior
'    .Pattern = xlNone
'    .TintAndShade = 0
'    .PatternTintAndShade = 0
'  End With
'  With .Font
'    .Name = "Calibri"
'    .FontStyle = "Standard"
'    .Size = 10
'    .Bold = False
'    .Strikethrough = False
'    .Superscript = False
'    .Subscript = False
'    .OutlineFont = False
'    .Shadow = False
'    .Underline = xlUnderlineStyleNone
'    .ColorIndex = xlAutomatic
'    .TintAndShade = 0
'    .ThemeFont = xlThemeFontMinor
'  End With
'  wsNew.ListObjects.Add(xlSrcRange, wsNew.Range("A1").CurrentRegion, , xlYes).Name = "Table1"
'  .HorizontalAlignment = xlGeneral
'  .WrapText = False
'  .Orientation = 0
'  .AddIndent = False
'  .IndentLevel = 0
'  .ShrinkToFit = False
'  .ReadingOrder = xlContext
'  .MergeCells = False
'  .Rows(1).Font.Bold = True
'  .EntireColumn.AutoFit
'End With
'/// end change 221105_09
wsNew.Name = "Data " & Format(Now, "yymmdd_hhmmss")   'name sheet
Application.Goto wsNew.Range("A1"), True
With ActiveWindow
  .Split = False
  .SplitColumn = 2
  .SplitRow = 1
  .FreezePanes = True
End With
wbNew.SaveAs Application.DefaultFilePath & Application.PathSeparator & wsNew.Name & ".xlsx", FileFormat:=51

end_here:
  With Application
    .ScreenUpdating = True
'    .Calculation = lngCalcMeth
    .Calculation = xlCalculationAutomatic
  End With
 
  Set rng2Copy = Nothing
  Set wsNew = Nothing
  Set wbNew = Nothing
  Set wbWork = Nothing
 
  Exit Sub

err_here:

  If Not ws Is Nothing Then
    Debug.Print "Worksheet: " & ws.Name
    Debug.Print "ErrNum: " & Err.Number
    Debug.Print "ErrTest: " & Err.Description
    Debug.Print "lngCounter: " & lngCounter
    Debug.Print "strColumn: " & strColumn
    Debug.Print "strStartCol: " & strStartCol
    Debug.Print "strEndCol: " & strEndCol
    Debug.Print "strDestCol: " & strDestCol
    Debug.Print "lngLastRow: " & lngLastRow
    Debug.Print "lngEmptyRows: " & lngEmptyRows
    Debug.Print "strSC: " & strSC
    If Not rng2Copy Is Nothing Then Debug.Print "rng2Copy: " & rng2Copy.Address
    If Not rngCell Is Nothing Then Debug.Print "rngCell Add: " & rngCell.Address
    If Not rngCell Is Nothing Then Debug.Print "rngCell Val: " & rngCell.Value
  End If
  Err.Clear
  Resume end_here

End Sub

Function fncGetLastFilledRow(ws As Worksheet, strCol As String) As Long
  Dim varReturn As Variant
 
  On Error GoTo err_here
  With ws.Range(strCol)
    varReturn = .Find(What:="*", _
          After:=.Cells(1), _
          LookIn:=xlValues, _
          LookAt:=xlWhole, _
          SearchOrder:=xlByRows, _
          SearchDirection:=xlPrevious, _
          MatchCase:=False).Row
  End With
  fncGetLastFilledRow = varReturn
  Exit Function
 
err_here:
  If Err <> 0 Then
    Err.Clear
    On Error GoTo 0
  End If
End Function

Ciao,
Holger
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi Danish,

I hope you are well again.

At the end of my code I was taking care of what you are asking now: converting everything to values, taking away any formats, apply a ListObject to the area with

VBA Code:
'...
With wsNew.UsedRange
  .Value = .Value
  .Borders(xlDiagonalDown).LineStyle = xlNone
  .Borders(xlDiagonalUp).LineStyle = xlNone
  .Borders(xlEdgeLeft).LineStyle = xlNone
  .Borders(xlEdgeTop).LineStyle = xlNone
  .Borders(xlEdgeBottom).LineStyle = xlNone
  .Borders(xlEdgeRight).LineStyle = xlNone
  .Borders(xlInsideVertical).LineStyle = xlNone
  .Borders(xlInsideHorizontal).LineStyle = xlNone
  With .Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
  End With
  With .Font
    .Name = "Calibri"
    .FontStyle = "Standard"
    .Size = 10
    .Bold = False
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
  End With
  wsNew.ListObjects.Add(xlSrcRange, wsNew.Range("A1").CurrentRegion, , xlYes).Name = "Table1"
  .HorizontalAlignment = xlGeneral
  .WrapText = False
  .Orientation = 0
  .AddIndent = False
  .IndentLevel = 0
  .ShrinkToFit = False
  .ReadingOrder = xlContext
  .MergeCells = False
  .Rows(1).Font.Bold = True
  .EntireColumn.AutoFit
End With
'...

Altered code:
VBA Code:
Sub MrE161350C_CopyValues()
'https://www.mrexcel.com/board/threads/vba-for-copying-all-data-from-8-sheets-in-a-single-workbook-to-a-new-sheet-in-new-workbook.1220016/
'/// changed by HaHoBe, 221105
'/// reason: user request for values only in Target Sheet
Dim wbWork                      As Workbook
Dim wbNew                       As Workbook
Dim ws                          As Worksheet
Dim wsNew                       As Worksheet

Dim blnRngCopy                  As Boolean

Dim lngStart                    As Long
Dim lngCounter                  As Long
Dim lngLastRow                  As Long
Dim lngEmptyRows                As Long
Dim lngStartRow                 As Long
Dim lngCalcMeth                 As Long

Dim rng2Copy                    As Range
Dim rngCell                     As Range
Dim rngArea                     As Range

Dim strColumn                   As String
Dim strSC                       As String
Dim strStartCol                 As String
Dim strEndCol                   As String
Dim strDestCol                  As String

'no screen update, calculation is set to Manual
With Application
  .ScreenUpdating = False
  lngCalcMeth = .Calculation
  .Calculation = xlCalculationManual
End With

On Error GoTo err_here

'!!! workbook must be open, please change name and extension to suit
Set wbWork = Workbooks("sdhasan Taxi Master Attendance_Sample Festwerte.xlsx")

Set wbNew = Workbooks.Add(1)
Set wsNew = wbNew.Sheets(1)
wsNew.Range("A1:M1").Value = wbWork.Worksheets("Airport Taxi").Range("A4:M4").Value
lngStart = 2

For Each ws In wbWork.Worksheets
  Select Case ws.Name
  
    Case "Airport Taxi", "Airport Taxi (Temp. driver)", "Karwa White", "Karwa White (Temporary)"
      With ws
        For lngCounter = 1 To 4
          blnRngCopy = True
          Select Case lngCounter
            Case 1
              strColumn = "C":  strStartCol = "A":  strEndCol = "M":  strDestCol = "A"
            Case 2
              strColumn = "N":  strStartCol = "N":  strEndCol = "U":  strDestCol = "B"
              lngStart = wsNew.Cells(Rows.Count, "A").End(xlUp).Row + 1
            Case 3
              strColumn = "O":  strStartCol = "O":  strEndCol = "O":  strDestCol = "A"
            Case 4
              strColumn = "O":  strStartCol = "J":  strEndCol = "M":  strDestCol = "J"
            Case Else
              strColumn = "O":  strStartCol = vbNullString:  strEndCol = vbNullString:  strDestCol = "J"
          End Select
          lngStartRow = 5
          lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
          If lngLastRow > 0 Then
            lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
            If lngLastRow >= lngStartRow Then
              Select Case lngEmptyRows
                Case Is > 0
                  If .Range(strColumn & lngLastRow).HasFormula Then
                    strSC = xlCellTypeFormulas
                  Else
                    strSC = xlCellTypeConstants
                  End If
                  Select Case lngCounter
                    Case 1, 2
                      Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strStartCol & lngLastRow).SpecialCells(strSC)
                      If rng2Copy.Areas.Count = 1 Then
                        Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
                      Else
                        For Each rngArea In rng2Copy.Areas
                          For Each rngCell In rngArea
'/// begin change 221105_01
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'                            .Range(rngCell, rngCell.Offset(0, 7)).Copy _
'                                wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1, 0)
'                            .Range(.Range("J" & rngCell.Row), .Range("M" & rngCell.Row)).Copy _
'                                wsNew.Range("J" & wsNew.Cells(Rows.Count, "A").End(xlUp).Row + 1)
'                            .Range(rngCell, rngCell.Offset(0, 7)).Copy _
'                                wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1, 0)
'/// new codeline(s)
                            wsNew.Range("J" & wsNew.Cells(Rows.Count, "A").End(xlUp).Row + 1).Value = _
                                .Range(.Range("J" & rngCell.Row), .Range("M" & rngCell.Row)).Value
                            wsNew.Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = .Range("A" & rngCell.Row).Value
                            wsNew.Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = .Range("A" & rngCell.Row).Value
'/// end change 221105_01
                          Next rngCell
                        Next rngArea
                        blnRngCopy = False
                      End If
                    Case 3, 4
                      blnRngCopy = False
                  End Select
                Case Else
                  Select Case lngCounter
                    Case 1, 2, 4
                      Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
                    Case 3
                      Set rng2Copy = .Range("A" & lngStartRow & ":A" & lngLastRow)
                End Select
              End Select
'/// begin change 221105_02
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'              If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
              If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_02
            End If
          Else
            Select Case MsgBox("Problems copying data from Sheet '" & .Name & "*." & vbCrLf & _
                  "Cancel or Continue?", vbOKCancel, "Now what...?")
              Case vbCancel
                wbNew.Close False
                GoTo end_here
              Case Else
                'continue
            End Select
          End If
        Next lngCounter
      End With
  
    Case "Revenue Share Scheme"
      blnRngCopy = True
      lngStartRow = 5
      With ws
        strColumn = "C":  strStartCol = "A":  strEndCol = "M":  strDestCol = "A"
        lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
        If lngLastRow > 0 Then
          lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
          If lngLastRow >= lngStartRow Then
            Select Case lngEmptyRows
              Case Is > 0
                If .Range(strColumn & lngLastRow).HasFormula Then
                  strSC = xlCellTypeFormulas
                Else
                  strSC = xlCellTypeConstants
                End If
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strStartCol & lngLastRow).SpecialCells(strSC)
                If rng2Copy.Areas.Count = 1 Then
                  Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
                Else
                  For Each rngArea In rng2Copy.Areas
                    For Each rngCell In rngArea
'/// begin change 221105_03
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'                      .Range(.Cells(rngCell.Row, strStartCol), .Cells(rngCell.Row, strEndCol)).Copy _
'                          wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1)
'/// new codeline(s)
                      wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1).Value = _
                          .Range(.Cells(rngCell.Row, strStartCol), .Cells(rngCell.Row, strEndCol)).Value
'/// end change 221105_03
                    Next rngCell
                  Next rngArea
                  blnRngCopy = False
                End If
              Case Else
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
            End Select
'/// begin change 221105_04
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'            If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
              If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_04
          Else
            Select Case MsgBox("Problems copying data from Sheet '" & .Name & "'." & vbCrLf & _
                  "Cancel or Continue?", vbOKCancel, "Now what...?")
              Case vbCancel
                wbNew.Close False
                GoTo end_here
              Case Else
                'continue
            End Select
          End If
        End If
      End With
  
    Case "Annual Leave"
      blnRngCopy = True
      lngStartRow = 3
      With ws
        strColumn = "C":  strStartCol = "A":  strEndCol = "G":  strDestCol = "A"
        lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
        If lngLastRow > 0 Then
          lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
          If lngLastRow >= lngStartRow Then
            Select Case lngEmptyRows
              Case Is > 0
                If .Range(strColumn & lngLastRow).HasFormula Then
                  strSC = xlCellTypeFormulas
                Else
                  strSC = xlCellTypeConstants
                End If
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strStartCol & lngLastRow).SpecialCells(strSC)
                If rng2Copy.Areas.Count = 1 Then
                  Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
                Else
                  For Each rngArea In rng2Copy.Areas
                    For Each rngCell In rngArea
'/// begin change 221105_05
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'                      .Range(.Cells(rngCell.Row, strStartCol), .Cells(rngCell.Row, strEndCol)).Copy _
'                          wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1)
'/// new codeline(s)
                      wsNew.Cells(Rows.Count, strDestCol).End(xlUp).Offset(1).Resize(1, 7).Value = _
                          .Range(.Cells(rngCell.Row, strStartCol), .Cells(rngCell.Row, strEndCol)).Value
'/// end change 221105_05
                    Next rngCell
                  Next rngArea
                  blnRngCopy = False
                End If
              Case Else
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
            End Select
'/// begin change 221105_06
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'            If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
            If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_06
          End If
        Else
          Select Case MsgBox("Problems copying data from Sheet '" & .Name & "'." & vbCrLf & _
          "Cancel or Continue?", vbOKCancel, "Now what...?")
            Case vbCancel
              wbNew.Close False
              GoTo end_here
            Case Else
              'continue
          End Select
        End If
      End With
  
    Case "Resign & Termination"
      blnRngCopy = True
      lngStartRow = 2
      With ws
        strColumn = "C":  strStartCol = "A":  strEndCol = "E":  strDestCol = "A"
        lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
        If lngLastRow > 0 Then
          lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
          If lngLastRow >= lngStartRow Then
            Select Case lngEmptyRows
              Case Is > 0
                If .Range(strColumn & lngLastRow).HasFormula Then
                  strSC = xlCellTypeFormulas
                Else
                  strSC = xlCellTypeConstants
                End If
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
              Case Else
                Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
            End Select
'/// begin change 221105_07
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'            If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
            If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_07
          End If
        Else
          Select Case MsgBox("Problems copying data from Sheet '" & .Name & "'." & vbCrLf & _
                "Cancel or Continue?", vbOKCancel, "Now what...?")
            Case vbCancel
              wbNew.Close False
              GoTo end_here
            Case Else
              'continue
          End Select
        End If
      End With
  
    Case "Incident"
      With ws
        For lngCounter = 1 To 2
          blnRngCopy = True
          lngStartRow = 2
           Select Case lngCounter
            Case 1
              strColumn = "C":  strStartCol = "A":  strEndCol = "E":  strDestCol = "A"
            Case 2
              strColumn = "C":  strStartCol = "H":  strEndCol = "H":  strDestCol = "H"
          End Select
          lngLastRow = fncGetLastFilledRow(ws, Range(strColumn & ":" & strColumn).Address)
          If lngLastRow > 0 Then
            lngEmptyRows = WorksheetFunction.CountBlank(.Range(strColumn & lngStartRow & ":" & strColumn & lngLastRow))
            If lngLastRow >= lngStartRow Then
              Select Case lngEmptyRows
                Case Is > 0
                  If .Range(strColumn & lngLastRow).HasFormula Then
                    strSC = xlCellTypeFormulas
                  Else
                    strSC = xlCellTypeConstants
                  End If
                  Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow).SpecialCells(strSC)
                Case Else
                  Set rng2Copy = .Range(strStartCol & lngStartRow & ":" & strEndCol & lngLastRow)
              End Select
'/// begin change 221105_08
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'            If blnRngCopy Then rng2Copy.Copy wsNew.Cells(lngStart, strDestCol)
'/// new codeline(s)
            If blnRngCopy Then wsNew.Cells(lngStart, strDestCol).Resize(rng2Copy.Rows.Count, rng2Copy.Columns.Count).Value = rng2Copy.Value
'/// end change 221105_08
            End If
          Else
            Select Case MsgBox("Problems copying data from Sheet '" & .Name & "'." & vbCrLf & _
                  "Cancel or Continue?", vbOKCancel, "Now what...?")
              Case vbCancel
                wbNew.Close False
                GoTo end_here
              Case Else
                'continue
            End Select
          End If
        Next lngCounter
      End With
  
    Case Else
  End Select
  lngStart = wsNew.Cells(Rows.Count, "A").End(xlUp).Row + 1
Next ws

wsNew.Columns(5).NumberFormat = wbWork.Worksheets("Airport Taxi").Range("E5").NumberFormat 'time values
wsNew.Columns(4).Copy wsNew.Range("N1")
wsNew.Range("N1").Value = "Status"
'/// begin change 221105_09
'/// HaHoBe, 221105
'/// user request for values only in Target Sheet
'///
'/// disabled codelines
'With wsNew.UsedRange
'  .Value = .Value
'  .Borders(xlDiagonalDown).LineStyle = xlNone
'  .Borders(xlDiagonalUp).LineStyle = xlNone
'  .Borders(xlEdgeLeft).LineStyle = xlNone
'  .Borders(xlEdgeTop).LineStyle = xlNone
'  .Borders(xlEdgeBottom).LineStyle = xlNone
'  .Borders(xlEdgeRight).LineStyle = xlNone
'  .Borders(xlInsideVertical).LineStyle = xlNone
'  .Borders(xlInsideHorizontal).LineStyle = xlNone
'  With .Interior
'    .Pattern = xlNone
'    .TintAndShade = 0
'    .PatternTintAndShade = 0
'  End With
'  With .Font
'    .Name = "Calibri"
'    .FontStyle = "Standard"
'    .Size = 10
'    .Bold = False
'    .Strikethrough = False
'    .Superscript = False
'    .Subscript = False
'    .OutlineFont = False
'    .Shadow = False
'    .Underline = xlUnderlineStyleNone
'    .ColorIndex = xlAutomatic
'    .TintAndShade = 0
'    .ThemeFont = xlThemeFontMinor
'  End With
'  wsNew.ListObjects.Add(xlSrcRange, wsNew.Range("A1").CurrentRegion, , xlYes).Name = "Table1"
'  .HorizontalAlignment = xlGeneral
'  .WrapText = False
'  .Orientation = 0
'  .AddIndent = False
'  .IndentLevel = 0
'  .ShrinkToFit = False
'  .ReadingOrder = xlContext
'  .MergeCells = False
'  .Rows(1).Font.Bold = True
'  .EntireColumn.AutoFit
'End With
'/// end change 221105_09
wsNew.Name = "Data " & Format(Now, "yymmdd_hhmmss")   'name sheet
Application.Goto wsNew.Range("A1"), True
With ActiveWindow
  .Split = False
  .SplitColumn = 2
  .SplitRow = 1
  .FreezePanes = True
End With
wbNew.SaveAs Application.DefaultFilePath & Application.PathSeparator & wsNew.Name & ".xlsx", FileFormat:=51

end_here:
  With Application
    .ScreenUpdating = True
'    .Calculation = lngCalcMeth
    .Calculation = xlCalculationAutomatic
  End With
 
  Set rng2Copy = Nothing
  Set wsNew = Nothing
  Set wbNew = Nothing
  Set wbWork = Nothing
 
  Exit Sub

err_here:

  If Not ws Is Nothing Then
    Debug.Print "Worksheet: " & ws.Name
    Debug.Print "ErrNum: " & Err.Number
    Debug.Print "ErrTest: " & Err.Description
    Debug.Print "lngCounter: " & lngCounter
    Debug.Print "strColumn: " & strColumn
    Debug.Print "strStartCol: " & strStartCol
    Debug.Print "strEndCol: " & strEndCol
    Debug.Print "strDestCol: " & strDestCol
    Debug.Print "lngLastRow: " & lngLastRow
    Debug.Print "lngEmptyRows: " & lngEmptyRows
    Debug.Print "strSC: " & strSC
    If Not rng2Copy Is Nothing Then Debug.Print "rng2Copy: " & rng2Copy.Address
    If Not rngCell Is Nothing Then Debug.Print "rngCell Add: " & rngCell.Address
    If Not rngCell Is Nothing Then Debug.Print "rngCell Val: " & rngCell.Value
  End If
  Err.Clear
  Resume end_here

End Sub

Function fncGetLastFilledRow(ws As Worksheet, strCol As String) As Long
  Dim varReturn As Variant
 
  On Error GoTo err_here
  With ws.Range(strCol)
    varReturn = .Find(What:="*", _
          After:=.Cells(1), _
          LookIn:=xlValues, _
          LookAt:=xlWhole, _
          SearchOrder:=xlByRows, _
          SearchDirection:=xlPrevious, _
          MatchCase:=False).Row
  End With
  fncGetLastFilledRow = varReturn
  Exit Function
 
err_here:
  If Err <> 0 Then
    Err.Clear
    On Error GoTo 0
  End If
End Function

Ciao,
Holger
Holger,

This code is working fine in the sample sheet that I shared with you however It is not copying data on my main file. I've copied your above code in my main workbook and have changed the name too in the code as per the workbook but still im getting the below output, what could be the issue?
1667652735098.png
 
Upvote 0
Holger,

This code is working fine in the sample sheet that I shared with you however It is not copying data on my main file. I've copied your above code in my main workbook and have changed the name too in the code as per the workbook but still im getting the below output, what could be the issue?
View attachment 77908
If you can share your email, I'll share my master file with you.

Regards,
Danish
 
Upvote 0
Hi Dinesh,

is there any information in the Immediate Window?

What you have posted as picture is done when the first segment is done: setting an object to the workbook, add a new workbook, copy over the information for the headers. Maybe the worksheets have different names as stated as that would go to Case Else part leaving no information or doing any copying.

Adjust the code for the proper workbook (this one was the workbook you shared) and then post what has been put into the Immediate window:

VBA Code:
Sub CheckSheetsWorkbook()
Dim wbWork    As Workbook
Dim ws        As Worksheet

Set wbWork = Workbooks("sdhasan Taxi Master Attendance_Sample Formeln.xlsx")

Debug.Print wbWork.Name
For Each ws In wbWork.Worksheets
  Select Case ws.Name
    Case "Airport Taxi", "Airport Taxi (Temp. driver)", "Karwa White", "Karwa White (Temporary)", _
          "Revenue Share Scheme", "Annual Leave", "Resign & Termination", "Incident"
      Debug.Print "Proceed on: " & ws.Name & " | " & ws.CodeName
    Case Else
      Debug.Print "No action on: " & ws.Name & " | " & ws.CodeName
  End Select
Next ws

Set wbWork = Nothing
End Sub

Result for me looks like
Rich (BB code):
sdhasan Taxi Master Attendance_Sample Formeln.xlsx
Proceed on: Airport Taxi | Sheet2
Proceed on: Airport Taxi (Temp. driver) | Sheet12
Proceed on: Revenue Share Scheme | Sheet3
Proceed on: Karwa White | Sheet4
Proceed on: Karwa White (Temporary) | Sheet14
Proceed on: Annual Leave | Sheet5
Proceed on: Resign & Termination | Sheet7
Proceed on: Incident | Sheet10
No action on: Driver Status | Sheet1
No action on: Temp. Duty Drivers  | Sheet9
No action on: Return From Leave | Sheet6
No action on: Driver Meeting  | Sheet21
No action on: Vacation  | Sheet15

Ciao,
Holger
 
Upvote 0
Solution
Hi Dinesh,

I hope you could solve the problem as I haven't received any more information.

If not: is it a shared workbook, placed on OneDrive, located anywhere on a Network? I can only test on my computer as no network is available for me.

Holger
 
Upvote 0
Hi Dinesh,

I hope you could solve the problem as I haven't received any more information.

If not: is it a shared workbook, placed on OneDrive, located anywhere on a Network? I can only test on my computer as no network is available for me.

Holger
Hi Holger!

Yes I managed to solve my problem. Needed to change your code a lil bit but it worked. Thank you so much for your help :)

Regards,
Danish
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top