Advertisement
KC358287

Untitled

Aug 10th, 2022 (edited)
1,721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub CreateSheet()
  2.  
  3.     For i = 1 To Worksheets.Count
  4.         If Worksheets(i).Name = "WYNIK" Then
  5.             MsgBox "WYNIK sheet exists! Please, remove it.", vbCritical, "Error"
  6.             End
  7.         End If
  8.     Next i
  9.  
  10.     With ThisWorkbook
  11.         .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "WYNIK"
  12.     End With
  13.    
  14. End Sub
  15.  
  16. Sub DeleteSheet()
  17.  
  18. Application.DisplayAlerts = False
  19. Worksheets("WYNIK").Delete
  20. Application.DisplayAlerts = True
  21.  
  22. End Sub
  23.  
  24.  
  25. Sub questions_to_new_sheet()
  26.  
  27. Dim ws As Worksheet
  28. Dim ws2 As Worksheet
  29. Dim LastRow As Long
  30. Dim SearchRange As Range
  31. Dim FindCell As Range
  32. Dim ColTitle As String
  33.  
  34.  
  35. Set wbCurrent = ActiveWorkbook
  36. Set ws = wbCurrent.ActiveSheet
  37. Set ChoiceColumn = ws.Range("A1:Z1").Find("WYBÓR")
  38. ColTitle = Replace(ChoiceColumn.Address(True, False), "$1", "")
  39.  
  40. With ws
  41.       LastRow = .Cells(.Rows.Count, ChoiceColumn.Column).End(xlUp).Row
  42. End With
  43.  
  44. Set SearchRange = Range(ColTitle + "1:" + ColTitle & LastRow)
  45. Call CreateSheet
  46. Set ws2 = wbCurrent.Worksheets("WYNIK")
  47.  
  48.  
  49.  
  50. For Each FindCell In SearchRange
  51.     If (FindCell.Value = "X") Or (FindCell.Value = "x") Then
  52.         FindCounter = FindCounter + 1
  53.         If FindCounter = 1 Then
  54.             ws2.Range("A1").Value = "PYTANIE"
  55.         End If
  56.         FindCell.Offset(0, 1).Resize(, 1).Copy
  57.         ws2.Range("A" & ws2.Cells(Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteValues
  58.     End If
  59. Next
  60.  
  61. If FindCounter = 0 Then
  62. Call DeleteSheet
  63. MsgBox "No questions found to copy", vbCritical, "Error"
  64. End
  65. End If
  66.  
  67. ws2.Range("A1").EntireColumn.AutoFit
  68.  
  69. MsgBox FindCounter & " questions have been copied", vbOKOnly, "Succes"
  70. Application.ScreenUpdating = True
  71.  
  72. End Sub
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement