Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ซอฟต์แวร์ >> Office

แมสเตอร์ VBA: 4 ตัวอย่างปฏิบัติจริง ใช้ Range แถวตัวแปรใน Excel

บทความนี้สอนการใช้ VBA Range กับ หมายเลขแถวตัวแปร ใน Excel: เลือกช่วง, ใส่ตัวเลข, คำนวณคณิตศาสตร์, และระบายสี ทั้งหมดด้วยจำนวนแถวที่เปลี่ยนแปลงได้

โค้ด VBA ตั้งค่า Range ด้วยแถวตัวแปร (ภาพรวม)

Sub Range_with_Variable_Row_Number() 
    First_Cell = InputBox("Enter the First Cell of the Range: ") 
    Row_Number = Str(Range(First_Cell).Row) 
    Number_of_Rows = InputBox("Enter the Total Number of Rows of the Range: ") 
    Set Rng = Range(First_Cell & ":" & Mid(First_Cell, 1, Len(First_Cell) - Len(Row_Number) + 1) & Mid(Str(Int(Number_of_Rows) + Int(Row_Number) - 1), 2, 10)) 
End Sub

คำอธิบาย:

  • รับ Input 2 ค่า: เซลล์แรก (First_Cell) และจำนวนแถว (Number_of_Rows)
  • สร้าง Range เริ่มจาก First_Cell จนครบจำนวนแถวที่กำหนด
  • ตัวอย่าง: First_Cell = B4, Number_of_Rows = 10 → Range = B4:B13
  • ใช้ Relative Reference เท่านั้น (B4 ไม่ใช่ $B$4)

4 ตัวอย่างการใช้ VBA Range กับแถวตัวแปร

ข้อมูลตัวอย่าง: ชื่อและเงินเดือนพนักงานบริษัท Jupyter Group

1. เลือกช่วงด้วยแถวตัวแปร

Sub Select_Range() 
    First_Cell = InputBox("Enter the First Cell to Select: ") 
    Row_Number = Str(Range(First_Cell).Row) 
    Number_of_Rows = InputBox("Enter the Number of Rows to Select: ") 
    Rng = First_Cell & ":" & Mid(First_Cell, 1, Len(First_Cell) - Len(Row_Number) + 1) & Mid(Str(Int(Number_of_Rows) + Int(Row_Number) - 1), 2, 10) 
    Range(Rng).Select 
End Sub

รัน Macro → กรอกเซลล์แรก (B4) → จำนวนแถว (5) → ได้ช่วง B4:B8 ตรง 5 คนแรก

2. ใส่ตัวเลขในช่วงแถวตัวแปร

ใส่เลขลำดับ 1-5 หน้าชื่อพนักงาน 5 คนแรก (A4:A8)

Sub Insert_Numbers() 
    First_Cell = InputBox("Enter the First Cell to Insert Number: ") 
    Row_Number = Str(Range(First_Cell).Row) 
    Number_of_Rows = InputBox("Enter the Total Number of Rows to Insert Numbers: ") 
    Set Rng = Range(First_Cell & ":" & Mid(First_Cell, 1, Len(First_Cell) - Len(Row_Number) + 1) & Mid(Str(Int(Number_of_Rows) + Int(Row_Number) - 1), 2, 10)) 
    Series_or_Fixed = Int(InputBox("Enter 1 for Series" & vbNewLine & "Enter 2 for Fixed Number: ")) 
    If Series_or_Fixed = 1 Then 
        First_Number = Int(InputBox("Enter the First Number: ")) 
        Increment = Int(InputBox("Enter the Increment: ")) 
        For i = 1 To Rng.Rows.Count 
            Rng.Cells(i, 1) = First_Number + (i - 1) * Increment 
        Next i 
    ElseIf Series_or_Fixed = 2 Then 
        Number = Int(InputBox("Enter the Fixed Number: ")) 
        For i = 1 To Rng.Rows.Count 
            Rng.Cells(i, 1) = Number 
        Next i 
    End If 
End Sub

3. คำนวณคณิตศาสตร์บนช่วงแถวตัวแปร

เพิ่มเงินเดือน 5 คนแรก ขึ้น 10,000 บาท

Sub Mathematical_Operation() 
    First_Cell = InputBox("Enter the First Cell to Perform Operation: ") 
    Row_Number = Str(Range(First_Cell).Row) 
    Number_of_Rows = InputBox("Enter the Total Number of Rows to Perform Operation: ") 
    Set Rng = Range(First_Cell & ":" & Mid(First_Cell, 1, Len(First_Cell) - Len(Row_Number) + 1) & Mid(Str(Int(Number_of_Rows) + Int(Row_Number) - 1), 2, 10)) 
    Operation = Int(InputBox("1=Add, 2=Subtract, 3=Multiply, 4=Divide: ")) 
    Operations = Array("Add", "Subtract", "Multiply", "Divide") 
    Number = Int(InputBox("Enter the Number to " & Operations(Operation - 1) & ": ")) 
    For i = 1 To Rng.Rows.Count 
        If Operation = 1 Then Rng.Cells(i, 1) = Rng.Cells(i, 1).Value + Number 
        If Operation = 2 Then Rng.Cells(i, 1) = Rng.Cells(i, 1).Value - Number 
        If Operation = 3 Then Rng.Cells(i, 1) = Rng.Cells(i, 1).Value * Number 
        If Operation = 4 Then Rng.Cells(i, 1) = Rng.Cells(i, 1).Value / Number 
    Next i 
End Sub

4. ระบายสีเซลล์ในช่วงแถวตัวแปร

ระบายสีชื่อ 5 คนแรก

Sub Color_Range() 
    First_Cell = InputBox("Enter the First Cell to Color: ") 
    Row_Number = Str(Range(First_Cell).Row) 
    Number_of_Rows = InputBox("Enter the Total Number of Rows to Color: ") 
    Set Rng = Range(First_Cell & ":" & Mid(First_Cell, 1, Len(First_Cell) - Len(Row_Number) + 1) & Mid(Str(Int(Number_of_Rows) + Int(Row_Number) - 1), 2, 10)) 
    Color_Code = Int(InputBox("Color Code: 3=Red, 5=Blue, 6=Yellow, 10=Green")) 
    Background_or_Text = Int(InputBox("1=Background, 2=Text Only")) 
    For i = 1 To Rng.Rows.Count 
        If Background_or_Text = 1 Then Rng(i, 1).Interior.ColorIndex = Color_Code 
        ElseIf Background_or_Text = 2 Then Rng.Cells(i, 1).Characters(1, Len(Rng.Cells(i, 1))).Font.ColorIndex = Color_Code 
    Next i 
End Sub

สรุป

4 ตัวอย่างใช้ VBA Range กับแถวตัวแปร: เลือกช่วง, ใส่เลข, คำนวณ, ระบายสี ปรับใช้ได้หลากหลายงาน

บทความเกี่ยวข้อง

  • VBA Range Offset (11 วิธี)
  • VBA For Each Cell in Range (3 วิธี)
  • Loop through Range (คู่มือฉบับสมบูรณ์)
  • Excel VBA Copy Range to Another Sheet (8 วิธี)
  • VBA Find String in Column Return Row Number