Page 1 of 1

ต้องการใส่ค่าในcell แล้วเปลี่ยนสีพื้นหลังในแถวนั้น[VBA]

Posted: Tue Aug 07, 2018 12:35 am
by natthakorn
สวัสดีครับ
ผมต้องการใส่ค่าใน column B แล้วให้แถวที่ใส่ค่า เปลี่ยนสีพื้นหลังในช่วง Column A ถึง Column C ครับ

ผมลองเขียนแล้วกลายเป็น สีพื้นหลังเปลี่ยนทั้งหมด

Code: Select all

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Integer

For i = 2 To 10
If Cells(i, "B").Value = "" Then
Range(Cells(i, "A"), Cells(i, "C")).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.499984740745262
        .PatternTintAndShade = 0
    End With

End If
Next

End Sub

Re: ต้องการใส่ค่าในcell แล้วเปลี่ยนสีพื้นหลังในแถวนั้น[VBA]

Posted: Tue Aug 07, 2018 6:35 am
by snasui
:D กรณีเปลี่ยนสีเมื่อใส่ค่าเลยทันทีดูตัวอย่างตามด้านล่างครับ

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("b:b")) Is Nothing Then Exit Sub
    If Target.Value = "" Then
        Exit Sub
    Else
        Target.Offset(0, -1).Resize(1, 3).Font.Color = vbRed
    End If
End Sub

Re: ต้องการใส่ค่าในcell แล้วเปลี่ยนสีพื้นหลังในแถวนั้น[VBA]

Posted: Wed Aug 08, 2018 1:13 am
by natthakorn
ใช้ได้แล้วครับอาจารย์ คงต้องศึกษาเพิ่มเติมอีกเยอะ

ขอบคุณครับ

Re: ต้องการใส่ค่าในcell แล้วเปลี่ยนสีพื้นหลังในแถวนั้น[VBA]

Posted: Thu Aug 09, 2018 3:06 pm
by natthakorn
ผมต้องการให้คำสั่งนี้มีผลทุก worksheet ใน workbook นั้นๆ ถึงแม้ว่าจะ add worksheet ขึ้นมาใหม่
ผมเลยเข้าไปใส่คำสั่งใน this workbook แทน แต่ไม่คำสั่งไม่มีผลกับชีทอื่นไม่แน่ใจว่า ผมใส่คำสั่งผิดตรงไหนครับ

Code: Select all

Private Sub Workbook_Open(ByVal Target As Range)
If Intersect(Target, Range("h:h")) Is Nothing Then Exit Sub
    If Target.Value = "" Then
        Exit Sub
    Else
         Target.Offset(0, -7).Resize(1, 14).Interior.ThemeColor = xlThemeColorAccent1
     
    End If
End Sub

Re: ต้องการใส่ค่าในcell แล้วเปลี่ยนสีพื้นหลังในแถวนั้น[VBA]

Posted: Thu Aug 09, 2018 6:15 pm
by natthakorn
ลองศึกษาเพิ่มแล้วสามารถทำได้แล้วครับ โดยเปลี่ยนเป็นดังนี้

Code: Select all

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Intersect(Target, Range("h:h")) Is Nothing Then Exit Sub
    If Target.Value = "" Then
        Exit Sub
    Else
         Target.Offset(0, -7).Resize(1, 14).Interior.ThemeColor = xlThemeColorAccent1
     
    End If
End Sub