gpt4 book ai didi

vba - 运行代码时删除工作表中的边框线

转载 作者:行者123 更新时间:2023-12-04 21:08:41 25 4
gpt4 key购买 nike

我有一个代码可以成功查看外部文件并将包含该特定条件的行复制/粘贴到当前工作簿中。例如,我正在搜索 Singapore在名为事件主项目文件的外部工作簿中复制所有包含 Singapore 的行到当前打开的工作簿。

出现的一个问题是,当我两次运行相同的代码时,工作表的最后一行会出现边框线。例如,当我运行代码时,它将复制粘贴包含 Singapore 的信息到名为“新的即将到来的项目”的当前工作表:

enter image description here

但是,当我再次运行代码时,它会在每一列上创建一条边框线,如下图所示:

enter image description here

我现在拥有的代码是:

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

我必须添加任何改进或附加代码以使边界线消失吗?

最佳答案

正如 EyePeaSea 所说,您可以通过 vba 代码删除边框,例如

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

在您的情况下,代码应该是(未经测试)
copyFrom.Borders.LineStyle = xlNone

复制该行之后

关于vba - 运行代码时删除工作表中的边框线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32859310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com