在总行vba代码之后添加边框和删除值

时间:2016-10-19 20:35:16

标签: excel excel-vba vba

需要有关vba代码的帮助。我想让vba代码为第二个数据集添加边框(在R列中)并删除总行数后的2个值(总共4个) 这是我到目前为止所得到的,

Sub Test()
Dim LastRow As Long, x As Long
LastRow = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
x = 2
Rows(LastRow - x + 1 & ":" & LastRow).Delete
Set dat = Sheets("Table F Agencies Combined").Range("M:N")
dat.Replace What:="TRUE", Replacement:=""
End Sub

现在我可以删除最后两行并替换为TRUE,但我不知道如何在第一组之后删除2个值,并将边框添加到第二个数据集的R列。

Data sets

revised data

final data

2 个答案:

答案 0 :(得分:1)

我认为您最好分开处理每个数据块。您可以重复运行此代码而不会丢失数据。

Sub Test2()

  Dim c As Range, r As Range
' 1st block
  Set c = Range("B:B").Find("total", Range("B1"), xlValues, xlWhole, , , False)
'c.Select
  c.Offset(1).Resize(2).EntireRow.ClearContents

' 2nd block
  Set c = Range("B:B").Find("total", c, xlValues, xlWhole, , , False)
'c.Select
  c.Offset(1).Resize(2).EntireRow.ClearContents

  Set r = Intersect(c.EntireRow, Columns("R"))
  Set r = Range(r, r.End(xlUp))
  r.BorderAround xlContinuous, xlThick

End Sub

答案 1 :(得分:0)

您的代码中出现了一些错误(语法错误,逻辑错误)。我已经为你纠正过了。

Sub Test()
    Dim dat As Range: Set dat = Sheets("Table F Agencies Combined").Range("M:N")
    Dim LastRow As Long, x As Long
    Dim myCell As Variant

    LastRow = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    x = 2
    Rows(LastRow - x + 1 & ":" & LastRow).Delete

    For Each myCell In dat
        myCell.Value = Replace(UCase(myCell.Value), "TRUE", "")
    Next myCell
End Sub