Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Sub letsmakecallsheets_and_detectphonecolumn()
- 'Set up variables, disable screenupdating to speed up
- Dim thisWb As Workbook
- Dim cWb As Workbook
- Dim cSheet As Worksheet
- Dim cTeam As Worksheet
- Dim lastRow As Long
- Dim logCol As Long
- Dim cSheetCol As Long
- Dim colLetter As String
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set thisWb = ActiveWorkbook
- Set cTeam = thisWb.ActiveSheet
- Set cWb = Workbooks.Add
- Set cSheet = cWb.Worksheets(1)
- lastRow = cTeam.Cells(Rows.Count, 1).End(xlUp).Row
- cSheetCol = 5
- 'unprotect with password if needed
- 'cTeam.Unprotect "password"
- ' loop through row 8 to find columns with name,gender,course,domicile,phones and copy + paste them
- With cTeam
- For logCol = 1 To 52 Step 1
- colLetter = Split(.Cells(1, logCol).Address, "$")(1)
- If cTeam.Cells(8, logCol).Value Like "*" & "Applicant Name" & "*" Then
- .Range(colLetter & "8" & ":" & colLetter & .Cells(.Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
- cSheet.Range("A1").PasteSpecial Paste:=xlPasteValues
- ElseIf cTeam.Cells(8, logCol).Value Like "*" & "Gender" & "*" Then
- .Range(colLetter & "8" & ":" & colLetter & .Cells(.Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
- cSheet.Range("B1").PasteSpecial Paste:=xlPasteValues
- ElseIf cTeam.Cells(8, logCol).Value Like "*" & "Programme" & "*" Then
- .Range(colLetter & "8" & ":" & colLetter & .Cells(.Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
- cSheet.Range("C1").PasteSpecial Paste:=xlPasteValues
- ElseIf cTeam.Cells(8, logCol).Value Like "*" & "Domicile" & "*" Then
- .Range(colLetter & "8" & ":" & colLetter & .Cells(.Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
- cSheet.Range("D1").PasteSpecial Paste:=xlPasteValues
- ElseIf cTeam.Cells(8, logCol).Value Like "*" & "Tel" & "*" Then
- .Range(colLetter & "8" & ":" & colLetter & .Cells(.Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
- cSheet.Cells(1, cSheetCol).PasteSpecial Paste:=xlPasteValues
- cSheetCol = cSheetCol + 1
- End If
- Next logCol
- End With
- 'remove formatting
- cSheet.Cells.ClearFormats
- 'replace + in phone numbers with 00 never used VBA to replace stuff so just left whatever the macro recorder put out - at some point go back and make this a bit neater
- Columns("E:J").Replace What:="+", Replacement:="00"
- Columns("E:J").Replace What:="-", Replacement:=""
- Columns("E:J").Replace What:="(", Replacement:=""
- Columns("E:J").Replace What:=")", Replacement:=""
- Columns("E:J").Replace What:=" ", Replacement:=""
- 'add a formula in adjacent column to join all the phone numbers separated by line breaks
- 'used LEN instead of = "" because sometimes the empty phone number will show up as 0 or and have a leading space etc
- 'this way this should exclude those cases
- cSheet.Range("K2").FormulaR1C1 = "=CONCATENATE(IF(AND(RC[-6]="""",RC[-5]="""",RC[-4]="""",RC[-3]="""",RC[-2]="""",RC[-1]=""""), ""No Number"",""""),IF(OR(LEN(RC[-6])<3,RIGHT(RC[-6],6)=RIGHT(RC[-5],6),RIGHT(RC[-6],6)=RIGHT(RC[-4],6),RIGHT(RC[-6],6)=RIGHT(RC[-3],6),RIGHT(RC[-6],6)=RIGHT(RC[-2],6),RIGHT(RC[-6],6)=RIGHT(RC[-2],6)),"""",RC[-6]&CHAR(10)),IF(OR(LEN(RC[-5])<3,RIGHT(RC[-5],6)=RIGHT(RC[-4],6), RIGHT(RC[-5], 6)=RIGHT(RC[-3],6),RIGHT(RC[-5],6)=RIGHT(RC[-2],6),RIGHT(RC[-5],6)=RIGHT(RC[-1],6)),"""",RC[-5]&CHAR(10)), IF(OR(LEN(RC[-4])<3,RIGHT(RC[-4],6)=RIGHT(RC[-3],6),RIGHT(RC[-4],6)=RIGHT(RC[-2],6),RIGHT(RC[-4],6)=RIGHT(RC[-1],6)),"""",RC[-4]&CHAR(10)),IF(OR(LEN(RC[-3])<3,RIGHT(RC[-3],6)=RIGHT(RC[-2],6),RIGHT(RC[-3],6)=RIGHT(RC[-1],6)),"""",RC[-3]&CHAR(10)),IF(OR(LEN(RC[-2])<3,RIGHT(RC[-2],6)=RIGHT(RC[-1],6)),"""",RC[-2]&CHAR(10)),IF(LEN(RC[-1])<3,"""",RC[-1]&CHAR(10)))"
- cSheet.Range("K2").AutoFill Destination:=Range("K2:K" & Cells(Rows.Count, 1).End(xlUp).Row)
- 'copy the new phone numbers into one column as values
- cSheet.Range("K:K").Copy
- cSheet.Range("E:E").PasteSpecial Paste:=xlPasteValues
- 'clear leftover things after putting all phone numbers in 1 column and add/adjust titles for columns
- 'too lazy to do another count of the last used row etc so just clearing the 10k rows and some columns to the right of whats used for callsheet
- cSheet.Range("F1:Z10000").Clear
- cSheet.Range("F1").Value = "Notes"
- cSheet.Range("E1").Value = "Phone Numbers"
- cSheet.Range("B1").Value = "M/F"
- 'autofit everything
- cSheet.Cells.Columns.AutoFit
- cSheet.Cells.Rows.AutoFit
- cSheet.Cells.HorizontalAlignment = xlLeft
- cSheet.Cells.VerticalAlignment = xlTop
- 'add borders, adjust row and column sizes, allow wrap text
- cSheet.Range("A2:F" & Cells(Rows.Count, 1).End(xlUp).Row).Borders.LineStyle = xlContinuous
- cSheet.Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).RowHeight = 56.6
- cSheet.Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).ColumnWidth = 14
- cSheet.Range("C2:C" & Cells(Rows.Count, 1).End(xlUp).Row).ColumnWidth = 20
- cSheet.Range("D2:D" & Cells(Rows.Count, 1).End(xlUp).Row).ColumnWidth = 12
- cSheet.Range("F2:F" & Cells(Rows.Count, 1).End(xlUp).Row).ColumnWidth = 60
- cSheet.Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).WrapText = True
- cSheet.Range("C2:C" & Cells(Rows.Count, 1).End(xlUp).Row).WrapText = True
- cSheet.Range("E2:E" & Cells(Rows.Count, 1).End(xlUp).Row).WrapText = True
- cSheet.Range("D2:D" & Cells(Rows.Count, 1).End(xlUp).Row).WrapText = True
- cSheet.Range("E:E").Columns.AutoFit
- 'add title in merged 1st row cols A to F + todays date
- Dim title As String
- Dim today As String
- today = Day(Date) & "/" & Month(Date) & "/" & Year(Date) ' bind todays date to variable
- title = InputBox("Please put in a description of the call sheet that would appear on the top of the 1st page of the call sheet and is optional. Todays date will be automatically added to the end of the description." & vbCrLf & "Otherwise leave blank")
- If title <> "" Then
- Range("A1").EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
- Range("A1:F1").Merge
- With Range("A1") 'not sure how VBA records merged cells so just gonna stick with selection rather than referencing a range until i get around to looking this up
- .Value = title & " " & today ' add todays date to the title
- .HorizontalAlignment = xlLeft 'align title left
- .Font.Bold = True
- .Font.Size = 12
- End With
- cSheet.Rows(2).AutoFit
- End If
- 'set up print area, change orientation to landscape and make margins narrow
- With cSheet.PageSetup
- .PrintArea = cSheet.Range("A1:F" & Cells(Rows.Count, 1).End(xlUp).Row).Address
- .Orientation = xlLandscape
- .LeftMargin = Application.InchesToPoints(0.25)
- .RightMargin = Application.InchesToPoints(0.25)
- .TopMargin = Application.InchesToPoints(0.75)
- .BottomMargin = Application.InchesToPoints(0.75)
- .HeaderMargin = Application.InchesToPoints(0.3)
- .FooterMargin = Application.InchesToPoints(0.3)
- End With
- 'exit cutcopymode and enable screenupdating again
- Application.CutCopyMode = False
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- cSheet.Range("A1").Select
- 'protect with password if needed
- 'cTeam.Protect "password", DrawingObjects:=True, Contents:=True, Scenarios:=True _
- ' , AllowFormattingCells:=True, AllowFormattingColumns:=True, _
- ' AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
- ' :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
- ' AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
- ' AllowUsingPivotTables:=True
- ' cTeam.EnableSelection = xlUnlockedCells
- End Sub
- Sub replace_text()
- Dim str As String
- Dim c As Range
- str = InputBox("Please input text you would like added to the end of the selected cells")
- For Each c In Selection
- If c.Value = "" And c.EntireRow.Hidden = False Then
- c.Value = str
- ElseIf c.Value <> "" And c.EntireRow.Hidden = False Then
- c.Value = c.Value & " " & str
- End If
- Next c
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement