cjvdg
cjvdg

Reputation: 533

Add border when adding data

How can I add a border to data added to my sheet?

The code for adding data. The button that loads the UserForm is located in the HOME sheet. The added data is saved in the STUDENTS_INFO sheet.

Sub add_stud()
    With Worksheets("STUDENTS_INFO")
        r = .Range("C" & Rows.Count).End(xlUp).Offset(1).Row
        .Cells(r, 3).value = txtBox_LRN.Text
        .Cells(r, 4).value = txtBox_lname.Text
        .Cells(r, 5).value = txtBox_fname.Text
        .Cells(r, 6).value = txtBox_ext.Text
        .Cells(r, 7).value = txtBox_mname.Text
    End With
End Sub

I saw this code in this site, but I don't know how to use it.

Dim iRange As Range
Dim iCells As Range

Set iRange = ThisWorkbook.ActiveSheet.UsedRange

For Each iCells In iRange
    If Not IsEmpty(iCells) Then
    iCells.BorderAround _
            LineStyle:=xlContinuous, _
            Weight:=xlThin
    End If
Next iCells

Upvotes: 0

Views: 44

Answers (1)

Dy.Lee
Dy.Lee

Reputation: 7567

Try,

Sub add_stud()
    Dim rngDB As Range
    With Worksheets("STUDENTS_INFO")
        r = .Range("C" & Rows.Count).End(xlUp).Offset(1).Row
        .Cells(r, 3).Value = txtBox_LRN.Text
        .Cells(r, 4).Value = txtBox_lname.Text
        .Cells(r, 5).Value = txtBox_fname.Text
        .Cells(r, 6).Value = txtBox_ext.Text
        .Cells(r, 7).Value = txtBox_mname.Text
        Set rngDB = .Range("c" & r).Resize(1, 5)
        With rngDB.Borders
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        
        
    End With
End Sub

Upvotes: 1

Related Questions