Advertisement
HendrixSL

Untitled

May 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1
  2. Sub SelectionsAndData()
  3.  
  4. 'This is a comment.
  5.  
  6. 'Select a cell.
  7. 'Range("C2").Select
  8.  
  9. 'Select a range.
  10. 'Range("A1:C2").Select
  11.  
  12. 'Navigate to another worksheet.
  13. 'Sheets("Sheet2").Activate
  14.  
  15. 'Offset the active cell.
  16. 'ActiveCell.Offset(1, 2).Select
  17. 'ActiveCell.Offset(-1, -2).Select
  18.  
  19. 'Put data into cells.
  20. 'ActiveCell.Value = "Hi!"
  21. 'Range("B2").Value = "Hey there!"
  22. 'Sheets("Sheet2").Range("C5").Value = "More Text!"
  23. ActiveCell.Offset(2, 2).Value = "Offset Baby!"
  24.  
  25. End Sub
  26.  
  27. 2
  28.  
  29. Sub GetAndOutput()
  30.  
  31. 'Get the value and set a variable equal to it.
  32. 'abcValue = Range("A2").Value
  33. abcValue = Sheets("Sheet1").Range("A2").Value
  34.  
  35. 'Display value in a pop-up window.
  36. 'MsgBox abcValue
  37.  
  38. 'Put values into specific cells.
  39. 'Sheets("Sheet1").Range("D2").Value = abcValue
  40. 'Sheets("Sheet1").Range("E6").Value = Sheets("Sheet2").Range("A2").Value
  41.  
  42. 'Put value into active cell.
  43. ActiveCell.Value = abcValue
  44.  
  45. End Sub
  46.  
  47. 3
  48.  
  49. Sub Cell_Looping()
  50.  
  51. '###### TeachExcel.com ######'
  52.  
  53. 'Declare some variables (mentioned at end of tutorial)
  54. Dim c As Range
  55. Dim myRange As Range
  56.  
  57. 'how to set a variable equal to a range
  58. Set myRange = Range("A2:A4")
  59.  
  60. 'Next line goes through a pre-determined range
  61. 'For Each c In myRange
  62.  
  63. 'Next line goes through a user-made selection
  64. For Each c In Selection
  65.  
  66.     'do something in the loop
  67.    
  68.     'visible value from the cell
  69.    'MsgBox c.Value
  70.    
  71.     'formula from the cell
  72.    'MsgBox c.Formula
  73.    
  74.     'cell row
  75.    'MsgBox c.Row
  76.    
  77.     'cell column
  78.    'MsgBox c.Column
  79.    
  80.     'full cell reference (absolute like $A$1)
  81.    'MsgBox c.Address
  82.    
  83.     'Check a condition
  84.    If c.Value = "green" Then
  85.         'what happens when true
  86.        
  87.         MsgBox "Cell " & c.Address & " equals green."
  88.        
  89.         'output the quantity - offset two cells to the right
  90.        MsgBox c.Offset(0, 2).Value
  91.        
  92.         'Found what we wanted - now exit the loop.
  93.        Exit For
  94.        
  95.     'if above condition is false, check this one
  96.    ElseIf c.Value = "red" Then
  97.    
  98.         MsgBox "Cell " & c.Address & " equals red."
  99.        
  100.         'Found what we wanted - now exit the loop.
  101.        Exit For
  102.    
  103.     Else 'what happens if all above conditions are false
  104.        
  105.         'MsgBox "Not green!"
  106.        
  107.     'needed to end the If statement
  108.    End If
  109.    
  110. 'end part of the For Each Next loop
  111. Next c
  112.  
  113. Sub Move_Stuff()
  114. '   TeachExcel.com
  115.  
  116. 'Get the last row of the import worksheet
  117. 'import_last_row = Sheets("Import").Range("A" & Rows.Count).End(xlUp).Row
  118. import_last_row = Sheets("Import").Cells(Rows.Count, 1).End(xlUp).Row
  119.  
  120. 'Output last row from Import table
  121. 'MsgBox import_last_row
  122.  
  123. For i = import_last_row To 2 Step -1
  124.  
  125.     'MsgBox i
  126.    
  127.     'Range("A1") = Cells(1,1)
  128.    'MsgBox Cells(i, 2).Value
  129.    
  130.     'Get last row for destination sheet
  131.    raw_last_row = Sheets("Raw").Cells(Rows.Count, 1).End(xlUp).Row
  132.    
  133.     'Copy only certain records.
  134.    If Sheets("Import").Cells(i, 2).Value = "Thanos" Then
  135.    
  136.         'Code goes here
  137.        
  138.         'Copy data to new worksheet
  139.        Sheets("Import").Cells(i, 1).EntireRow.Copy Sheets("Raw").Cells(raw_last_row + 1, 1)
  140.        
  141.         'Delete the copied data.
  142.        Sheets("Import").Cells(i, 1).EntireRow.Delete
  143.        
  144.     End If
  145.    
  146.    
  147. Next i
  148.  
  149. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement