我有一个脚本,将两个工作表组合成一个全面的工作表:legal_ws
+ cib_ws
---> comp_ws
legal_ws
可以很好地移动:
lrow = legal_ws.Range("A" & Rows.Count).End(xlUp).Row
lcol = legal_ws.Cells(1, legal_ws.Columns.Count).End(xlToLeft).Column
和
Set legal_rng = legal_ws.Range(legal_ws.Cells(2, 1), legal_ws.Cells(lrow, lcol))
还有这个
legal_rng.Copy Destination:=comp_ws.Range("A2")
,但是以相同方式传输cib_ws
的方法无效。 cib_ws
传输需要自动过滤器,并且仅复制可见的单元格减去标题,但不会复制这些单元格。
lrow2 = cib_ws.Range("A" & Rows.Count).End(xlUp).Row
lcol2 = cib_ws.Cells(1, cib_ws.Columns.Count).End(xlToLeft).Column
Set cib_rng = cib_ws.Range(cib_ws.Cells(2, 1), cib_ws.Cells(lrow, lcol)).SpecialCells(xlCellTypeVisible)
With cib_ws.Range("A1" & lcol2)
.AutoFilter
.AutoFilter Field:=61, Criteria1:="Regional Presidents"
End With
cib_rng.Copy Destination:=comp_ws.Range("A" & lrow3 + 1)
为什么cib_ws
传输不起作用,而legal_ws
却有效?
这是整个脚本:
Sub TCR_Update()
'File Paths
Dim Legal As String, CIB As String, Comp_TCR As String
Legal = "M:\Legal-TCR-Template.xlsx"
CIB = "M:\CIB-TCR-Template.xlsx"
Comp_TCR = "M:\Total_TCR.xlsx"
Dim legal_wb As Workbook, cib_wb As Workbook, comp_wb As Workbook
Set legal_wb = Workbooks.Open(Filename:=Legal)
Set cib_wb = Workbooks.Open(Filename:=CIB)
Set comp_wb = Workbooks.Open(Filename:=Comp_TCR)
Dim legal_ws As Worksheet, cib_ws As Worksheet, comp_ws As Worksheet
Set legal_ws = legal_wb.Sheets("ps")
Set cib_ws = cib_wb.Sheets("ps")
Set comp_ws = comp_wb.Sheets("Sheet1")
Dim lrow As Long, lcol As Long, lrow2 As Long, lcol2 As Long, lrow3 As Long
Dim legal_rng As Range
lrow = legal_ws.Range("A" & Rows.Count).End(xlUp).Row
lcol = legal_ws.Cells(1, legal_ws.Columns.Count).End(xlToLeft).Column
lrow2 = cib_ws.Range("A" & Rows.Count).End(xlUp).Row
lcol2 = cib_ws.Cells(1, cib_ws.Columns.Count).End(xlToLeft).Column
lrow3 = comp_ws.Range("A" & Rows.Count).End(xlUp).Row
Set legal_rng = legal_ws.Range(legal_ws.Cells(2, 1), legal_ws.Cells(lrow, lcol))
Set cib_rng = cib_ws.Range(cib_ws.Cells(2, 1), cib_ws.Cells(lrow, lcol)).SpecialCells(xlCellTypeVisible)
legal_rng.Copy Destination:=comp_ws.Range("A2")
With cib_ws.Range("A1" & lcol2)
.AutoFilter
.AutoFilter Field:=61, Criteria1:="Regional Presidents"
End With
cib_rng.Copy Destination:=comp_ws.Range("A" & lrow + 1)
legal_wb.Close SaveChanges:=False
cib_wb.Close SaveChanges:=False
Cells.Select
Cells.Copy
Cells.PasteSpecial Paste:=xlPasteValues
With comp_ws
.Cells.WrapText = False
.Rows.AutoFit
.Columns.AutoFit
End With
答案 0 :(得分:0)
主要问题:
lrow2
和lcol2
代替lrow
和lcol
(必须只是一个简单的疏忽)。With cib_ws.Range("A1" & lcol2)
指的是一个单元格,而不是要自动过滤的整个范围。可能应该更改为With cib_ws.Range("A1", cib_ws.Cells(Lrow2,lcol2))
Set cib_rng
等于生成的可见单元格。请注意,在尝试复制之前,您需要测试是否有可见的单元格,否则cib_rng
将是Nothing
。因此,将您的cib_rng.Copy
语句包装在If Not cib_rng Is Nothing Then...End If
中。 答案 1 :(得分:0)
尝试一下:
Sub TCR_Update()
lrow2 = cib_ws.Range("A" & Rows.Count).End(xlUp).Row
lcol2 = cib_ws.Cells(1, cib_ws.Columns.Count).End(xlToLeft).Column
Set cib_rng = cib_ws.Range(cib_ws.Cells(2, 1), cib_ws.Cells(lrow, lcol)).SpecialCells(xlCellTypeVisible)
cib_ws.AutoFilterMode = False
With cib_ws.Range("A1" & lcol2)
.AutoFilter
.AutoFilter Field:=61, Criteria1:="Regional Presidents"
.SpecialCells(xlCellTypeVisible).Copy Destination:=comp_ws.Range("A" & lrow3 + 1)
End With
'cib_rng.Copy Destination:=comp_ws.Range("A" & lrow + 1)
End Sub