Advertisement
Guest User

Untitled

a guest
May 26th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. ' Replaces all the "=" with "#" or vice versa depending on first cell
  2. ' This is useful for copying cells that contain formulas with cell references that you don't want to change when you move them. For example, transposing, copying between workbooks
  3. '
  4.  
  5. Sub toggleEqualsSign()
  6. '
  7. ' toggleEqualsSign Macro
  8.  
  9. Dim replaceWhat As String
  10. Dim replaceWithThat As String
  11. Dim sel_rng As Range
  12. Dim first_formula As String
  13. Dim lRows As Long
  14. Dim lCol As Long
  15. Dim r As Long
  16. Dim c As Long
  17.  
  18. Dim replaceWith(0 To 1) As String
  19.  
  20.  
  21. Application.ScreenUpdating = False
  22.  
  23. Set sel_rng = Selection
  24. lRows = sel_rng.Rows.count
  25. lCol = sel_rng.Columns.count
  26.  
  27. 'go through each cell
  28. For r = 1 To lRows
  29. For c = 1 To lCol
  30. first_formula = sel_rng(r, c)
  31. replaceWhat = Left(first_formula, 1)
  32. If replaceWhat = "#" Or sel_rng(r, c).HasFormula Then GoTo StartReplace:
  33. Next c
  34. Next r
  35.  
  36. If r = lRows + 1 And c = lCol + 1 Then
  37. Exit Sub
  38. End If
  39.  
  40. StartReplace:
  41. If replaceWhat = "#" Then
  42. replaceWithThat = "="
  43. Else
  44. replaceWhat = "="
  45. replaceWithThat = "#"
  46. End If
  47.  
  48. Selection.Replace What:=replaceWhat, replacement:=replaceWithThat, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
  49. Application.ScreenUpdating = True
  50. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement