Good morning,
I have this VBA code ready. I needed to know the VBA code to apply it only from sheet 7 (inclusive), can you help me?
Private Sub CommandButton1_Click()
Call adjustList
End Sub
Function saveEntry(x As Integer, y As Integer) As Variant
Dim tmpRows()
Dim i As Integer
Dim e As Integer
Dim numOfRowsForEntry As Integer
Dim numOfColumns As Integer
numOfColumns = 11
Dim tmpColumns() As String
ReDim tmpColumns(numOfColumns)
Cells(x, 1).Select
numOfRowsForEntry = 0
Do Until ActiveCell Like "*Total"
Cells(x + numOfRowsForEntry, 1).Select
numOfRowsForEntry = numOfRowsForEntry + 1
Loop
ReDim tmpRows(numOfRowsForEntry - 1)
For i = 0 To UBound(tmpRows) - LBound(tmpRows)
For e = 0 To numOfColumns
tmpColumns(e) = ""
tmpColumns(e) = Cells(x + i, y + e).Text
Cells(x + i, y + e) = ""
Cells(x + i, y + e).Interior.Color = xlNone
Next
tmpRows(i) = tmpColumns
Next
saveEntry = tmpRows
Exit Function
End Function
Sub adjustList()
Dim x As Integer
Dim i As Integer
Dim startRowOfList As Integer
Dim entryList()
Application.ScreenUpdating = False
startRowOfList = 10
NumRows = Cells(Rows.Count, 1).End(xlUp).Row
ReDim Preserve entryList(0)
Cells(startRowOfList, 3).Select
i = 0
For x = startRowOfList To NumRows
Cells(x, 1).Select
If Not IsEmpty(ActiveCell) And Not ActiveCell Like "*Total" Then
entryList(i) = saveEntry(ActiveCell.Row, ActiveCell.Column)
ReDim Preserve entryList(UBound(entryList) - LBound(entryList) +
1)
i = i + 1
End If
Next
Cells(startRowOfList, 1).Select
For x = 0 To UBound(entryList) - LBound(entryList) - 1
For i = 0 To UBound(entryList(x)) - LBound(entryList(x))
If entryList(x)(i)(0) Like "*Total" Then
ActiveCell.Offset(1, 0) = entryList(x)(i)(0)
For e = 0 To 11
ActiveCell.Offset(1, e).Interior.ColorIndex = 15
Next
Else
ActiveCell = entryList(x)(i)(0)
ActiveCell.Offset(0, 1) = entryList(x)(i)(1)
End If
For c = 2 To UBound(entryList(x)(i)) - LBound(entryList(x)(i))
ActiveCell.Offset(1, c) = entryList(x)(i)(c)
Next
ActiveCell.Offset(1, 0).Select
Next
ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True
End Sub
in addition to this code, I had previously applied a code to format each sheet created from sheet 7 (inclusive). However, with the code above, I don't know how to adjust.
Sub macro()
Const ProcName As String = "macro"
On Error GoTo ClearError
Const FirstWorksheetIndex As Long = 7
Application.ScreenUpdating = False
With ThisWorkbook
Dim LastWorksheetIndex As Long: LastWorksheetIndex =
.Worksheets.Count
If LastWorksheetIndex < FirstWorksheetIndex Then Exit Sub
Dim ash As Object: Set ash = .ActiveSheet
Dim n As Long
For n = FirstWorksheetIndex To LastWorksheetIndex
CreateHeaders .Worksheets(n)
Next n
ash.Select
End With
Application.ScreenUpdating = True
ProcExit:
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume ProcExit
End Sub
Sub CreateHeaders(ByVal WS As Worksheet)
Const ProcName As String = "CreateHeaders"
On Error GoTo ClearError
With WS
.Select ' cannot be avoided only because of the following line
ActiveWindow.DisplayGridlines = False
ProcExit:
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume ProcExit
End With
End Sub