Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在特定列中查找最后使用的单元格,并在其周围加上一个范围_Excel_Vba_Border - Fatal编程技术网

Excel 在特定列中查找最后使用的单元格,并在其周围加上一个范围

Excel 在特定列中查找最后使用的单元格,并在其周围加上一个范围,excel,vba,border,Excel,Vba,Border,我有一个Excel,其值从K3开始(用户将其值从该单元格开始输入,因此我不知道第3行的长度有多少),我想从K3到该行的最后一个值(第3行)从第10行到第3行中使用的最后一个值(对不起,我真的不知道怎么说,我会举个例子让您更好地理解) 这是一张照片 我找到的唯一代码是这个,但它只在静态范围内工作 Sub Sample() Dim rng As Range Set rng = Range("K3:P10") With rng.Borders .LineS

我有一个Excel,其值从K3开始(用户将其值从该单元格开始输入,因此我不知道第3行的长度有多少),我想从K3到该行的最后一个值(第3行)从第10行到第3行中使用的最后一个值(对不起,我真的不知道怎么说,我会举个例子让您更好地理解)

这是一张照片

我找到的唯一代码是这个,但它只在静态范围内工作

Sub Sample()
    Dim rng As Range

    Set rng = Range("K3:P10")

    With rng.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
End Sub
这应该做到:

Sub Sample()

    Dim rng As Range
    Dim LastCol As Long

    With ThisWorkbook.Sheets("MySheet") 'Change MySheet for your sheet name
        LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column 'this will find the last used column on Row 3
        Set rng = Range("K3", .Cells(10, LastCol))
    End With

    With rng
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeLeft).Weight = xlThin
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeTop).Weight = xlThin
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).Weight = xlThin
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeRight).Weight = xlThin
    End With

End Sub

仅举一个动态输入示例(可以改进):


到目前为止,这仅在添加/删除每列时有效…如前所述,可以对其进行改进;)

在我看来,您希望以列而不是行的形式输入数据;)的确。。。我觉得他很困惑。。。我是:/谢谢@JvdV,我编辑了我的answer@SometingNew现在试试,我很困惑,正在计算最后一行,现在正在计算最后一列。是的,对不起,达米安,我知道我的问题不清楚,干杯。。。我能问另一件事吗?它能工作,但它包围了我所有的细胞,你能让它只包围周围吗?像在图像中一样?@SometingNew使用
If
语句,
If.Cells(3,LastCol).MergeArea.Columns.Count>1然后LastCol=LastCol+.Cells(3,LastCol).MergeArea.Columns.Count-1
在计算
LastCol
之后,这必须是动态的吗?我的意思是:当用户从第3行输入/删除数据时,这些边界是否应该更改为“活动”边界?@JvdV否,我只是制作了一个宏来让用户在周围进行边界设置。如果他们愿意,他们将采取删除,值和边框,并重新开始:)哇!这是另一个级别的Excel!JvdV你是一名职业选手!我当然不是;)
Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

Dim rng1 As Range, rng2 As Range
Dim lc As Long

If Not Intersect(Range("3:3"), Target) Is Nothing Then
    lc = Range("K3").CurrentRegion.Columns.Count 'No xlToLeft just in case far to the right there might be other cells
    Set rng1 = Range(Cells(3, 11), Cells(10, 11 + lc))
    Set rng2 = Range(Cells(3, 11), Cells(10, 11 + lc - 1))
    rng1.Borders.LineStyle = xlNone
    rng2.BorderAround ColorIndex:=1
End If

Application.EnableEvents = True

End Sub