范围选择没有得到我需要的范围
Range Selection not getting range I need
我对VBA还是很陌生。我有一个包含多个工作表的工作簿,每月更新一次。在其中 2 张纸上,我需要复制包含公式的最后 5 列,将它们(使用数字格式)复制到最后一个空白列,然后仅将原始范围复制为值。 A 列和 I 列之间有一个空列,因此在我的代码中,我试图启动 xlToLeft 的范围以从 I 列开始查找。我在尝试设置范围时感到非常困惑,但似乎没有任何效果。这是我从其他代码拼凑而成的代码。请帮忙。
Sub AgentReports()
Dim ws As Worksheet
Dim ArrayOne() As Variant
Dim wsName As Variant
Dim rngcopy As Range
Dim InTheList As Boolean
ArrayOne = Array("Sheet2", "Sheet3")
For Each ws In ThisWorkbook.Worksheets
InTheList = Not (IsError(Application.Match(ws.CodeName, ArrayOne, 0)))
If InTheList Then
With ws
Range("I3").Select
Set rngcopy = .Range(.Cells(3, Columns.Count).End(xlToLeft).Offset(, -4), .Cells(Rows.Count, Columns.Count).End(xlToLeft))
rngcopy.Copy rngcopy.Offset(, 5)
rngcopy.Copy
rngcopy.PasteSpecial Paste:=xlPasteValues
End With
End If
Next ws
End Sub
试试这个:
Sub AgentReports()
Dim ws As Worksheet, c As Range
Dim ArrayOne() As Variant
Dim wsName As Variant, lr As Long
ArrayOne = Array("Sheet2", "Sheet3")
For Each ws In ThisWorkbook.Worksheets
If Not IsError(Application.Match(ws.CodeName, ArrayOne, 0)) Then
'find the last used row
lr = ws.Cells.Find(What:="*", After:=ws.Range("A1"), LookAt:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'find the last-occupied cell in Row 3
Set c = ws.Cells(3, Columns.Count).End(xlToLeft)
If c.Column >= 5 Then 'need 5 columns to copy
With ws.Range(c.Offset(0, -4), ws.Cells(lr, c.Column))
.Select 'for testing only
.Copy .Offset(0, 5) 'copy 5 cols over
.Value = .Value 'convert to values
End With
End If
End If
Next ws
End Sub
我对VBA还是很陌生。我有一个包含多个工作表的工作簿,每月更新一次。在其中 2 张纸上,我需要复制包含公式的最后 5 列,将它们(使用数字格式)复制到最后一个空白列,然后仅将原始范围复制为值。 A 列和 I 列之间有一个空列,因此在我的代码中,我试图启动 xlToLeft 的范围以从 I 列开始查找。我在尝试设置范围时感到非常困惑,但似乎没有任何效果。这是我从其他代码拼凑而成的代码。请帮忙。
Sub AgentReports()
Dim ws As Worksheet
Dim ArrayOne() As Variant
Dim wsName As Variant
Dim rngcopy As Range
Dim InTheList As Boolean
ArrayOne = Array("Sheet2", "Sheet3")
For Each ws In ThisWorkbook.Worksheets
InTheList = Not (IsError(Application.Match(ws.CodeName, ArrayOne, 0)))
If InTheList Then
With ws
Range("I3").Select
Set rngcopy = .Range(.Cells(3, Columns.Count).End(xlToLeft).Offset(, -4), .Cells(Rows.Count, Columns.Count).End(xlToLeft))
rngcopy.Copy rngcopy.Offset(, 5)
rngcopy.Copy
rngcopy.PasteSpecial Paste:=xlPasteValues
End With
End If
Next ws
End Sub
试试这个:
Sub AgentReports()
Dim ws As Worksheet, c As Range
Dim ArrayOne() As Variant
Dim wsName As Variant, lr As Long
ArrayOne = Array("Sheet2", "Sheet3")
For Each ws In ThisWorkbook.Worksheets
If Not IsError(Application.Match(ws.CodeName, ArrayOne, 0)) Then
'find the last used row
lr = ws.Cells.Find(What:="*", After:=ws.Range("A1"), LookAt:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'find the last-occupied cell in Row 3
Set c = ws.Cells(3, Columns.Count).End(xlToLeft)
If c.Column >= 5 Then 'need 5 columns to copy
With ws.Range(c.Offset(0, -4), ws.Cells(lr, c.Column))
.Select 'for testing only
.Copy .Offset(0, 5) 'copy 5 cols over
.Value = .Value 'convert to values
End With
End If
End If
Next ws
End Sub