Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Copy_Data_Set() 'Blue button
  2.  
  3.  
  4.  
  5.     'This copies the data from an export that matches the assumptions below
  6.  
  7.     'Assumes the field headings are in the top row and there is 65535 or less rows
  8.  
  9.     '(fits into an old excel document)
  10.  
  11.    
  12.  
  13.     Range("A2", Range("A1").End(xlToRight).Offset(Range("A1", Range("A65535").End(xlUp)).Count - 1)).Copy
  14.  
  15.    
  16.  
  17.    
  18.  
  19. End Sub
  20.  
  21.  
  22.  
  23. Sub Return_Direction()
  24.  
  25. '
  26.  
  27. 'This changes the way that the box moves if you press enter.
  28.  
  29. 'It will jump between moving right and moving down.
  30.  
  31. 'If it is set to something other than right or down (left or up) it will set the direction to down.
  32.  
  33. 'If the box is set to not move then the direction still changes (just the distance is set to 0)
  34.  
  35.  
  36.  
  37. '
  38.  
  39.     If Application.MoveAfterReturnDirection = xlDown Then
  40.  
  41.        
  42.  
  43.     Application.MoveAfterReturnDirection = xlToRight
  44.  
  45.    
  46.  
  47.     Else
  48.  
  49.     Application.MoveAfterReturnDirection = xlDown
  50.  
  51.    
  52.  
  53.     End If
  54.  
  55.    
  56.  
  57.    
  58.  
  59. End Sub
  60.  
  61.  
  62.  
  63. Sub Return_OnOff()
  64.  
  65.  
  66.  
  67. 'Toggles if the selection box will move when pressing enter.
  68.  
  69.  
  70.  
  71.     If Application.MoveAfterReturn = False Then
  72.  
  73.    
  74.  
  75.         Application.MoveAfterReturn = True
  76.  
  77.        
  78.  
  79.     Else
  80.  
  81.         Application.MoveAfterReturn = False
  82.  
  83.        
  84.  
  85.     End If
  86.  
  87.      
  88.  
  89. End Sub
  90.  
  91.  
  92.  
  93. Sub ToggleGetPivot()
  94.  
  95.  
  96.  
  97. 'Toggles if typing "=" then clicking in a pivot table produces a cell refference or a GetPivot formula
  98.  
  99.     If Application.GenerateGetPivotData = True Then
  100.  
  101.         Application.GenerateGetPivotData = False
  102.  
  103.     Else
  104.  
  105.         Application.GenerateGetPivotData = True
  106.  
  107.     End If
  108.  
  109.  
  110.  
  111. End Sub
  112.  
  113.  
  114.  
  115. Sub Toggle_IfError()
  116.  
  117.  
  118.  
  119.     'Adds/Removes the Iferror function from a set of formulas.
  120.  
  121.     'This won't convert =If(iserror(formula),return,formula), instead will wrap the whole thing in _
  122.  
  123.        an IfError function which would probably never return an error anyway.
  124.  
  125.     'It doesn't play nice with multiple selections, but does ignore blank cells.
  126.  
  127.     Dim rng As Range
  128.  
  129.     Set rng = Selection
  130.  
  131.     Dim rows As Integer
  132.  
  133.     Dim columns As Integer
  134.  
  135.     Dim Formula As String
  136.  
  137.    
  138.  
  139.     On Error GoTo Error1
  140.  
  141.    
  142.  
  143.    
  144.  
  145.     rows = rng.rows.Count
  146.  
  147.     columns = rng.columns.Count
  148.  
  149.    
  150.  
  151.     For Each c In rng.Cells
  152.  
  153.        
  154.  
  155.             Formula = c.Formula
  156.  
  157.            
  158.  
  159.             If Left(Formula, 1) = "=" Then
  160.  
  161.            
  162.  
  163.                 If Left(Formula, 9) = "=IFERROR(" Then
  164.  
  165.                     'Remove the outer IFERROR formula
  166.  
  167.                     Dim CommaFromRight As Integer
  168.  
  169.                     CommaFromRight = 0
  170.  
  171.                     Dim CommaFound As Boolean
  172.  
  173.                     CommaFound = False
  174.  
  175.                    
  176.  
  177.                     Do While CommaFound = False ' Find the possition of "," from the right
  178.  
  179.                    
  180.  
  181.                         If Left(Right(Formula, CommaFromRight), 1) = "," Then
  182.  
  183.                             CommaFound = True
  184.  
  185.                         Else
  186.  
  187.                             CommaFromRight = CommaFromRight + 1
  188.  
  189.                         End If
  190.  
  191.                                        
  192.  
  193.                     Loop
  194.  
  195.                    
  196.  
  197.                     Formula = "=" & Mid(Formula, 10, Len(Formula) - (9 + CommaFromRight))
  198.  
  199.                                    
  200.  
  201.                 Else
  202.  
  203.                     Dim ErrorValue As String
  204.  
  205.                    
  206.  
  207.                     If LenB(ErrorValue) = 0 Then
  208.  
  209.                                    
  210.  
  211.                     ErrorValue = InputBox("What would you like to be returned?", "Error Value", 0)
  212.  
  213.                                    
  214.  
  215.                     End If
  216.  
  217.                    
  218.  
  219.                     Formula = "=IFERROR(" & Right(Formula, Len(Formula) - 1) & "," & ErrorValue & ")"
  220.  
  221.                    
  222.  
  223.                 End If
  224.  
  225.                        
  226.  
  227.                 c.Formula = Formula
  228.  
  229.            
  230.  
  231.             End If
  232.  
  233.         Next
  234.  
  235.    
  236.  
  237.     Exit Sub
  238.  
  239.    
  240.  
  241. Error1:
  242.  
  243.    
  244.  
  245. End Sub
  246.  
  247.  
  248.  
  249. Sub RefreshAll()
  250.  
  251. 'Refreshes all pivot tables in a workbook.
  252.  
  253.         ActiveWorkbook.RefreshAll
  254.  
  255. End Sub
  256.  
  257.  
  258.  
  259. Sub ShowAllSheets()
  260.  
  261. 'Unhides every sheet in a workbook
  262.  
  263.     For Each WS In Worksheets
  264.  
  265.  
  266.  
  267.         WS.Visible = xlSheetVisible
  268.  
  269.  
  270.  
  271.     Next WS
  272.  
  273.  
  274.  
  275. End Sub
  276.  
  277.  
  278.  
  279. Sub DragFormulasDown()
  280.  
  281.  
  282.  
  283. Application.ScreenUpdating = False
  284.  
  285.    
  286.  
  287.     'Takes the name of two worksheets in a workbook and makes sure the formulas go down the same number of cells
  288.  
  289.     'Designed to be used when one sheet pulls data through from another worksheet but to save _
  290.  
  291.        processing power the formulas aren't dragged down 10s of thousands of rows in advance.
  292.  
  293.     'It's recommended that if you need to use this then you also paste values higher up. _
  294.  
  295.        (rember though if you change anything major at a later date).
  296.  
  297.     'This assumes that the formulas start in column A and continue without break until the _
  298.  
  299.    last column in.
  300.  
  301.    
  302.  
  303.    
  304.  
  305.     Dim StartSheet As String
  306.  
  307.     Dim EndSheet As String
  308.  
  309.    
  310.  
  311.     StartSheet = InputBox("What is the name of the data sheet?")
  312.  
  313.     EndSheet = InputBox("What sheet needs formulas dragging down?")
  314.  
  315.    
  316.  
  317.    
  318.  
  319.     Dim DataRows As Long
  320.  
  321.     DataRows = Sheets(StartSheet).Range("A" & Sheets(StartSheet).rows.Count).End(xlUp).Row
  322.  
  323.    
  324.  
  325.     Dim FinalRows As Long
  326.  
  327.     FinalRows = Sheets(EndSheet).Range("A" & Sheets(EndSheet).rows.Count).End(xlUp).Row
  328.  
  329.    
  330.  
  331.     Dim CopyRange As Range
  332.  
  333.        
  334.  
  335.     Dim NewRows As Long
  336.  
  337.     NewRows = DataRows - FinalRows
  338.  
  339.    
  340.  
  341.     If NewRows < 0 Then
  342.  
  343.         MsgBox "The " & FinalSheet & " sheet already has more rows than the " & StartSheet & " Sheet"
  344.  
  345.         Exit Sub
  346.  
  347.     End If
  348.  
  349.    
  350.  
  351.     Dim StartPoint As Range
  352.  
  353.     Set StartPoint = Sheets(EndSheet).Range("A" & Sheets(EndSheet).rows.Count).End(xlUp)
  354.  
  355.     Dim EndPoint As Range
  356.  
  357.     Set EndPoint = Sheets(EndSheet).Range("A" & Sheets(EndSheet).rows.Count).End(xlUp).End(xlToRight).Offset(NewRows, 0)
  358.  
  359.     Set CopyRange = Range(StartPoint, StartPoint.End(xlToRight))
  360.  
  361.    
  362.  
  363.     If DataRows > FinalRows Then
  364.  
  365.         Sheets(EndSheet).Select
  366.  
  367.         CopyRange.Select
  368.  
  369.         Selection.AutoFill Destination:=Range(StartPoint.Address, EndPoint.Address), _
  370.  
  371.             Type:=xlFillDefault
  372.  
  373.         Sheets(EndSheet).Range("A" & Sheets(EndSheet).rows.Count).End(xlUp).Select
  374.  
  375.     End If
  376.  
  377.  
  378.  
  379.  
  380.  
  381. Application.ScreenUpdating = True
  382.  
  383.  
  384.  
  385. End Sub
  386.  
  387.  
  388.  
  389. Sub ClearFilters()
  390.  
  391.  
  392.  
  393.     'Leaves the filters applied, but sets them all to blank.
  394.  
  395.     If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
  396.  
  397.         ActiveSheet.ShowAllData
  398.  
  399.     End If
  400.  
  401. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement