简体   繁体   English

运行代码后删除工作表中的边界线

[英]Removing the border lines in a worksheet when code has been run

I have a code that successfully looks into an external file and copy/pastes the rows that contain that particular condition into the current workbook. 我有一个代码,可以成功地查看外部文件并将包含特定条件的行复制/粘贴到当前工作簿中。 For example I am searching for Singapore in the external workbook called Active master project file and copy all the rows containing Singapore to the current workbook that is open. 例如,我正在名为Active master project file的外部工作簿中搜索Singapore ,并将包含Singapore所有行复制到当前打开的工作簿中。

A problem that occurs is that when I run the same code twice, a border line will exist on the last row of the worksheet. 发生的问题是,当我两次运行相同的代码时,工作表的最后一行将存在一条边框线。 For example when I run the code, it will copy paste the information containing Singapore to the current worksheet called "New Upcoming Projects": 例如,当我运行代码时,它将把包含Singapore的信息复制粘贴到当前工作表中,称为“新的即将来临的项目”:

在此处输入图片说明

However, when I run the code again it will create a border line on each column such as the image shown below: 但是,当我再次运行代码时,它将在每列上创建一条边界线,如下图所示:

在此处输入图片说明

And the code that I have for now is: 我现在拥有的代码是:

Sub UpdateNewUpcomingProj()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim copyFrom As Range
    Dim lRow As Long '<~~ Not Integer. Might give you error in higher versions of excel
    Dim strSearch As String

    Set wb1 = Application.Workbooks.Open("U:\Active Master Project.xlsm")
    Set ws1 = wb1.Worksheets("New Upcoming Projects")

    strSearch = "Singapore"
    With ws1

        '~~> Remove any filters
        .AutoFilterMode = False

        '~~> I am assuming that the names are in Col A
        '~~> if not then change A below to whatever column letter
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        With .Range("A1:A" & lRow)
            .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
            Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
        End With

        .AutoFilterMode = False
    End With

    '~~> Destination File
    Set wb2 = ThisWorkbook
    Set ws2 = wb2.Worksheets("New Upcoming Projects")
     With ws2
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lRow = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row
        Else
            lRow = 2
        End If

       copyFrom.Copy .Rows(lRow)
      .Rows.RemoveDuplicates Array(2), xlNo

    End With
End Sub

Is there any improvement or additional codes that I have to add in so that the border line would disappear? 我是否需要添加任何改进或其他代码以使边界线消失?

As EyePeaSea said you can remove the border by vba code, eg 正如EyePeaSea所说,您可以通过vba代码删除边框,例如

ThisWorkbook.Worksheets("XY").Range("A1", "Z99").Borders.LineStyle = xlNone

In your case the code should be (untested) 在您的情况下,代码应(未试用)

copyFrom.Borders.LineStyle = xlNone

after you copied the row 复制行之后

I assume this formatting is coming from the source worksheet. 我认为这种格式来自源工作表。 If so, you could PasteSpecial to just paste values, keeping the destination formatting. 如果是这样,则可以使用PasteSpecial粘贴值,并保持目标格式。 To do so, simply replace 为此,只需更换

copyFrom.Copy .Rows(lRow)

with

copyFrom.Copy
.Rows(lRow).PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False

If you do need some formatting from the source sheet, you can use xlPasteAllExceptBorders instead of xlPasteValues . 如果确实需要从源工作表进行某种格式设置,则可以使用xlPasteAllExceptBorders代替xlPasteValues

Paste Special, this will paste to the first empty cell in column A 选择性粘贴,它将粘贴到列A中的第一个空单元格

copyfrom.Copy
ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = 0

You can add this line after removing the duplicates 您可以在删除重复项后添加此行

.UsedRange.Offset(lRow).Borders.Value = 0

This will remove any borders from the inserted rows 这将从插入的行中删除所有边框

ps: I still dont understand where these borders came from, most probably from the original worksheet.. :) ps:我仍然不明白这些边界的来源,最有可能是来自原始工作表的.. :)

At the end of the code, please add a new line to format paint of the 3rd row. 在代码末尾,请添加新行以格式化第三行的绘画。

So basically before the last two lines wb1.Select ' please make sure you select the correct one wb1 or wb2 here and try again Rows("3:3").Select Selection.Copy Rows("4:10000").Select Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False end with end sub 'This is the last line of your code 所以基本上在最后两行wb1.Select'之前,请确保在此处选择正确的wb1或wb2,然后重试Rows(“ 3:3”)。Select Selection.Copy Rows(“ 4:10000”)。Select Selection .PasteSpecial Paste:= xlPasteFormats,Operation:= xlNone,_ SkipBlanks:= False,Transpose:= False Application.CutCopyMode = False结尾为end sub'这是代码的最后一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM