- Back to Home »
- VBA »
- [VBA] WorkSheet_Cell_Range
Friday, November 29, 2019
1.Basic (select)
・Rows
Rows(123) 'row 123 only
Rows("2:5") 'range of rows from row 2 to 5
・Columns
Columns(123) / Columns("B") '1 comlumn only
Columns("B:D") 'range of columns form column B to D
・Cells
Cells(2, 4) / Cells(2, "D")
・Range
Range("A2") 'Cell A2 only
Range("A2:B10") 'Cell from A2 to B10
Range("A:A") 'Column A
Range("A:D") 'Column A to column D
Range("2:2") 'Row 2
Range("2:4") 'Row 2 to row 4
Option Explicit
Private Const NAME_COL As String = "B"
Private Const AGE_COL As String = "C"
Private Const START_ROW As Integer = 2
Sub range_select()
Dim sColum As Range, sRow As Range
Set sColum = Columns("B")
Set sRow = Rows(2)
End Sub
Sub cell_scan_column()
Dim i As Integer
With ThisWorkbook.Worksheets("cell_scan")
For i = START_ROW To 10
If .Cells(i, NAME_COL).Value <> "" Then
'by Cells
Debug.Print .Cells(i, NAME_COL).Value, .Cells(i, AGE_COL).Value
'or by Range
Debug.Print .Range(NAME_COL & i).Value, .Range(AGE_COL & i).Value
Else
Exit For
End If
Next
End With
End Sub
Sub worksheet_EndxlDown()
' Neu Cells(startRow, "B") co data thi End(xlDown) se tra ve cell cuoi cung co data (cell tiep theo se la mot empty cell)
' maxrow > startRow
'
' Neu Cells(startRow, "B") la empty cell thi End(xlDown) se tra ve cell dau tien co data tinh startRow
' maxrow > startRow
'
'NOTE:
' - vi tri cell tra ve cung tren column "B" (duyet theo column B)
Dim startRow As Long, maxrow As Long
startRow = 1
maxrow = Cells(startRow, "B").End(xlDown).Row
Debug.Print maxrow
End Sub
Sub worksheet_EndxlUp()
' nguyen tac giong voi xlDown, chi khac la chieu tu duoi len tren
' maxrow < startRow
Dim startRow As Long, LastRow As Long
startRow = 65000
' startRow = ActiveSheet.Rows.Count 'dung de xac dinh cell cuoi cung co data tren B column
LastRow = Cells(startRow, "B").End(xlUp).Row
Debug.Print maxrow
End Sub
3.Find text
Sub worksheet_findText()
'Note: other option
' LookIn:=xlValues -> looks at the cell values
' LookAt:=xlWhole -> full match or xlPart means the search value only has to match part of the cell
' SearchDirection:=xlNext or xlPrevious
' After:=Range("A2") -> tim kiem tu vi tri cell tiep theo A3
Dim cell As Range
Dim Addr As String
'---------------------------------------------------------------------
' tim kiem trong khoang A1:B6, tu vi tri cell A3 (After:=Range("A2") )
' NOTE: After cell phai trong pham vi Range("A1:B6")
Set cell = Range("A1:B6").Find("Drucilla", After:=Range("A2"))
If cell Is Nothing Then
Debug.Print "Not found"
Else
Addr = cell.Address
Debug.Print "Addr: " & Addr
End If
'---------------------------------------------------------------------
' tim kiem o column A, tu vi tri cell A3 (After:=Range("A2") )
Set cell = Columns("A").Find("Drucilla", After:=Range("A2"))
If cell Is Nothing Then
Debug.Print "Not found"
Else
Addr = cell.Address
Debug.Print "Addr: " & Addr
End If
'---------------------------------------------------------------------
' tim kiem tren toan bo cells
Set cell = Cells.Find("Drucilla", After:=Range("A2"))
If cell Is Nothing Then
Debug.Print "Not found"
Else
Addr = cell.Address
Debug.Print "Addr: " & Addr
End If
End Sub
4.MergeCells
Sub merge_test()
Dim i As Long, k As Long
Dim tmpStr As String
Dim edgeTop As Long, edgeBottom As Long
For i = 1 To 10
If Cells(i, "B").MergeCells Then
Debug.Print i, Range("B" & Range(Cells(i, "B").MergeArea.Address).Row).Value
Else
'find edgeTop and edgeBottom
edgeTop = i
While Cells(edgeTop, "B").Borders(xlEdgeTop).LineStyle = xlNone
edgeTop = edgeTop - 1
Wend
edgeBottom = i
While Cells(edgeBottom, "B").Borders(xlEdgeBottom).LineStyle = xlNone
edgeBottom = edgeBottom + 1
Wend
tmpStr = ""
For k = edgeTop To edgeBottom
If Trim(Cells(k, "B").Value) <> "" Then
tmpStr = tmpStr & " " & Cells(k, "B").Value
End If
Next
Debug.Print i, tmpStr
End If
Next
End Sub
Result:
1 1
2 2
3 2
4 3
5 3
6 3
7 4
8 4
9 4
10 4
5. Clear
※https://www.automateexcel.com/vba/clearcontents-clear
Sub clear_test()
Range("a1").Clear 'Everything
Range("a1").ClearComments 'Comments
Range("a1").ClearContents 'Contents
Range("a1").ClearFormats 'Formats
Range("a1").ClearHyperlinks 'Hyperlinks
Range("a1").ClearNotes 'Notes
Range("a1").ClearOutline 'Outline
End Sub
6.Cell Font
6.1 Text
※https://www.automateexcel.com/vba/cell-font-color-size/
'vbBlack, vbBlue, vbCyan, vbGreen, vbMagenta, vbRed, vbWhite, vbYellow
Range("a1").Font.Color = vbRed
Range("a1").Font.Color = RGB(255,255,0)
Range("a1").Font.Size = 12
Range("A1").Font.Bold = True
Range("A1").Font.Name = "Arial"
6.2 Background
Sub checkBackgroundColor_test()
Dim i As Integer
For i = 1 To 10
If Cells(i, "A").Interior.Color <> vbWhite Then
Debug.Print "row:", i, Cells(i, "A").Interior.Color
End If
Next
End Sub