我的Excel代码遇到了一些麻烦。我想要做的是按文本标准搜索行,按列按特定条件对这些行进行过滤/排序,并能够复制并保存剪贴板中的所有值,以便自动化软件从该点接管。
到目前为止,我已经能够按指定的条件(文本字符串)对行进行排序,但我似乎无法找出仅复制列范围(到行的末尾)的代码。我可以复制行,但我不确定复制单个列的代码是什么(在这种情况下,这些都是Web地址,要复制的列将是C)。我正在使用Excel 2010。
Sub USPS_Select2()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long
Dim strSearch As String
Dim rng As Range
Dim cl As Object
Dim strMatch As String
Dim filterRange As Range
Dim copyRange As Range
Dim lastRow As Long
Set wb1 = Application.Workbooks.Open("\\S51\CompanyFolder\Employee Folders\Jason\TrackingDeliveryStatus.xls")
Set ws1 = wb1.Worksheets("TrackingDeliveryStatusResults")
strSearch = "usps.com"
With ws1
.AutoFilterMode = False
lRow = .Range("C" & .Rows.Count).End(xlUp).Row
With .Range("C2:C" & lRow)
.AutoFilter field:=1, Criteria1:="=*" & strSearch & "*"
Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With
.AutoFilterMode = False
End With
Set wb2 = Application.Workbooks.Open("C:\Users\CompanyFolder\Desktop\Excel_Test.xls")
Set ws2 = wb2.Worksheets("Sheet1")
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 = 1
End If
copyFrom.Copy .Rows(lRow)
End With
'wb2.Save
'wb2.close
End Sub
答案 0 :(得分:0)
由于您已在此声明中使用
With .Range("C2:C" & lRow)
您可以从此声明中删除EntireRow
属性
Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
它会将范围设置为C 列中的可见单元格,如下所示:
Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible)
该行:
copyFrom.Copy .Rows(lRow)
是否要将数据粘贴到该行的每列中。因此,将此行仅复制到A列。
copyFrom.Copy .Cells(lRow,1)