Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1
- Sub SelectionsAndData()
- 'This is a comment.
- 'Select a cell.
- 'Range("C2").Select
- 'Select a range.
- 'Range("A1:C2").Select
- 'Navigate to another worksheet.
- 'Sheets("Sheet2").Activate
- 'Offset the active cell.
- 'ActiveCell.Offset(1, 2).Select
- 'ActiveCell.Offset(-1, -2).Select
- 'Put data into cells.
- 'ActiveCell.Value = "Hi!"
- 'Range("B2").Value = "Hey there!"
- 'Sheets("Sheet2").Range("C5").Value = "More Text!"
- ActiveCell.Offset(2, 2).Value = "Offset Baby!"
- End Sub
- 2
- Sub GetAndOutput()
- 'Get the value and set a variable equal to it.
- 'abcValue = Range("A2").Value
- abcValue = Sheets("Sheet1").Range("A2").Value
- 'Display value in a pop-up window.
- 'MsgBox abcValue
- 'Put values into specific cells.
- 'Sheets("Sheet1").Range("D2").Value = abcValue
- 'Sheets("Sheet1").Range("E6").Value = Sheets("Sheet2").Range("A2").Value
- 'Put value into active cell.
- ActiveCell.Value = abcValue
- End Sub
- 3
- Sub Cell_Looping()
- '###### TeachExcel.com ######'
- 'Declare some variables (mentioned at end of tutorial)
- Dim c As Range
- Dim myRange As Range
- 'how to set a variable equal to a range
- Set myRange = Range("A2:A4")
- 'Next line goes through a pre-determined range
- 'For Each c In myRange
- 'Next line goes through a user-made selection
- For Each c In Selection
- 'do something in the loop
- 'visible value from the cell
- 'MsgBox c.Value
- 'formula from the cell
- 'MsgBox c.Formula
- 'cell row
- 'MsgBox c.Row
- 'cell column
- 'MsgBox c.Column
- 'full cell reference (absolute like $A$1)
- 'MsgBox c.Address
- 'Check a condition
- If c.Value = "green" Then
- 'what happens when true
- MsgBox "Cell " & c.Address & " equals green."
- 'output the quantity - offset two cells to the right
- MsgBox c.Offset(0, 2).Value
- 'Found what we wanted - now exit the loop.
- Exit For
- 'if above condition is false, check this one
- ElseIf c.Value = "red" Then
- MsgBox "Cell " & c.Address & " equals red."
- 'Found what we wanted - now exit the loop.
- Exit For
- Else 'what happens if all above conditions are false
- 'MsgBox "Not green!"
- 'needed to end the If statement
- End If
- 'end part of the For Each Next loop
- Next c
- Sub Move_Stuff()
- ' TeachExcel.com
- 'Get the last row of the import worksheet
- 'import_last_row = Sheets("Import").Range("A" & Rows.Count).End(xlUp).Row
- import_last_row = Sheets("Import").Cells(Rows.Count, 1).End(xlUp).Row
- 'Output last row from Import table
- 'MsgBox import_last_row
- For i = import_last_row To 2 Step -1
- 'MsgBox i
- 'Range("A1") = Cells(1,1)
- 'MsgBox Cells(i, 2).Value
- 'Get last row for destination sheet
- raw_last_row = Sheets("Raw").Cells(Rows.Count, 1).End(xlUp).Row
- 'Copy only certain records.
- If Sheets("Import").Cells(i, 2).Value = "Thanos" Then
- 'Code goes here
- 'Copy data to new worksheet
- Sheets("Import").Cells(i, 1).EntireRow.Copy Sheets("Raw").Cells(raw_last_row + 1, 1)
- 'Delete the copied data.
- Sheets("Import").Cells(i, 1).EntireRow.Delete
- End If
- Next i
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement