VBA: 无法引用另一个 sheet 中的范围
VBA: Unable to reference Range in another sheet
这是我的第一个 post,所以请提供有关我提出问题的方法的任何反馈。
我正在构建一个子程序,(最终)应该将范围从一个 sheet("Sandbox")复制到另一个("Master")。步骤是:
- 识别所选行
- 遍历沙盒行,确定是找到匹配的主行还是添加为主行中的新末行
- 仅将值从每个选定的沙盒行复制到适当的主行
设置 PasteSpecial 函数的范围时会弹出错误。该行始终给出“1004(对象'_Global'的方法'Range'失败”消息。
代码如下:
Sub UpdateMaster()
Dim currentSelection As Range
Set currentSelection = Selection
Dim sheetSB As Worksheet
Set sheetSB = ThisWorkbook.Sheets("Sandbox")
Dim sheetMaster As Worksheet
Set sheetMaster = ThisWorkbook.Sheets("Master")
Dim lastTargetRow As Integer
lastTargetRow = sheetMaster.Range("IDRange").End(xlDown).Row + 1
Dim startingTargetColumn As Integer
startingTargetColumn = sheetMaster.Range("IDRange").Column
Dim thisID As String
Dim thisStatus As String
For Each thisrow In currentSelection.Rows
' Capture the current ID value
thisID = Cells(thisrow.Row, Range("IDRange").Column).Value
' Capture the current Status value
thisStatus = Cells(thisrow.Row, Range("NewRange").Column).Value
' If the row has no ID...
If thisID = "" Then
' ...do nothing
' If the row is flagged as new...
ElseIf thisStatus = "New" Then
'...identify the first blank row, and set all data columns to be copied
Range(Cells(thisrow.Row, Range("IDRange").Column), Cells(thisrow.Row, Range("LastSandboxColumn")).Column).Copy _
Destination:=sheetMaster.Range(lastTargetRow, startingTargetColumn)
' Increment the next available last row by 1
lastTargetRow = lastTargetRow + 1
Else
' Otherwise, find the corresponding row and set the non-ID columns to be copied
Dim sourceColumn1 As Integer, sourceColumn2 As Integer
Dim targetRow As Integer, targetColumn As Integer
Dim matchRow As Integer
sourceColumn1 = Range("IDRange").Column + 1
sourceColumn2 = Range("LastSandboxColumn").Column
targetRow = Application.WorksheetFunction.Match(thisID, sheetMaster.Range("IDRange"), 0)
targetColumn = startingTargetColumn + 1
Range(Cells(thisrow.Row, sourceColumn1), Cells(thisrow.Row, sourceColumn2)).Copy
Range(sheetMaster.Cells(targetRow, targetColumn)).PasteSpecial xlPasteValues
End If
Next
End Sub
错误发生在最后一行:
Range(sheetMaster.Cells(targetRow, targetColumn)).PasteSpecial xlPasteValues
令人费解的是,以下似乎有效:
Range(Cells(thisrow.Row, sourceColumn1), Cells(thisrow.Row, sourceColumn2)).Copy _
Destination:=Range(sheetMaster.Cells(targetRow, targetColumn))
不幸的是,我只想要值;引入公式和格式会搞砸 sheet.
中的其他行为
我已经尝试了很多变体,但本质上,如果我使用 Cells(),它不允许我创建一个引用 Master 的范围。
非常感谢任何帮助。
就这样:
sheetMaster.Cells(targetRow, targetColumn).PasteSpecial xlPasteValues
如果 sheetMaster
在运行时不是 ActiveSheet,则可能会发生错误:
Range(sheetMaster.Cells(targetRow, targetColumn).PasteSpecial) xlPasteValues
另请注意,对于此问题:
Unfortunately, I want only the values; bringing over formulas and formatting will screw up other behavior in the sheet.
您可以获取范围的 .Value
作为数组,并将其直接写入其他 sheet,而无需调用 Copy
或 Paste/PasteSpecial
方法。下面的答案显示了从一个工作簿到另一个工作簿的 copying/pasting 的几种方法,但可以很容易地修改为 sheet-to-sheet 转移。
Copy from one workbook and paste into another
这是我的第一个 post,所以请提供有关我提出问题的方法的任何反馈。
我正在构建一个子程序,(最终)应该将范围从一个 sheet("Sandbox")复制到另一个("Master")。步骤是:
- 识别所选行
- 遍历沙盒行,确定是找到匹配的主行还是添加为主行中的新末行
- 仅将值从每个选定的沙盒行复制到适当的主行
设置 PasteSpecial 函数的范围时会弹出错误。该行始终给出“1004(对象'_Global'的方法'Range'失败”消息。
代码如下:
Sub UpdateMaster()
Dim currentSelection As Range
Set currentSelection = Selection
Dim sheetSB As Worksheet
Set sheetSB = ThisWorkbook.Sheets("Sandbox")
Dim sheetMaster As Worksheet
Set sheetMaster = ThisWorkbook.Sheets("Master")
Dim lastTargetRow As Integer
lastTargetRow = sheetMaster.Range("IDRange").End(xlDown).Row + 1
Dim startingTargetColumn As Integer
startingTargetColumn = sheetMaster.Range("IDRange").Column
Dim thisID As String
Dim thisStatus As String
For Each thisrow In currentSelection.Rows
' Capture the current ID value
thisID = Cells(thisrow.Row, Range("IDRange").Column).Value
' Capture the current Status value
thisStatus = Cells(thisrow.Row, Range("NewRange").Column).Value
' If the row has no ID...
If thisID = "" Then
' ...do nothing
' If the row is flagged as new...
ElseIf thisStatus = "New" Then
'...identify the first blank row, and set all data columns to be copied
Range(Cells(thisrow.Row, Range("IDRange").Column), Cells(thisrow.Row, Range("LastSandboxColumn")).Column).Copy _
Destination:=sheetMaster.Range(lastTargetRow, startingTargetColumn)
' Increment the next available last row by 1
lastTargetRow = lastTargetRow + 1
Else
' Otherwise, find the corresponding row and set the non-ID columns to be copied
Dim sourceColumn1 As Integer, sourceColumn2 As Integer
Dim targetRow As Integer, targetColumn As Integer
Dim matchRow As Integer
sourceColumn1 = Range("IDRange").Column + 1
sourceColumn2 = Range("LastSandboxColumn").Column
targetRow = Application.WorksheetFunction.Match(thisID, sheetMaster.Range("IDRange"), 0)
targetColumn = startingTargetColumn + 1
Range(Cells(thisrow.Row, sourceColumn1), Cells(thisrow.Row, sourceColumn2)).Copy
Range(sheetMaster.Cells(targetRow, targetColumn)).PasteSpecial xlPasteValues
End If
Next
End Sub
错误发生在最后一行:
Range(sheetMaster.Cells(targetRow, targetColumn)).PasteSpecial xlPasteValues
令人费解的是,以下似乎有效:
Range(Cells(thisrow.Row, sourceColumn1), Cells(thisrow.Row, sourceColumn2)).Copy _
Destination:=Range(sheetMaster.Cells(targetRow, targetColumn))
不幸的是,我只想要值;引入公式和格式会搞砸 sheet.
中的其他行为我已经尝试了很多变体,但本质上,如果我使用 Cells(),它不允许我创建一个引用 Master 的范围。
非常感谢任何帮助。
就这样:
sheetMaster.Cells(targetRow, targetColumn).PasteSpecial xlPasteValues
如果 sheetMaster
在运行时不是 ActiveSheet,则可能会发生错误:
Range(sheetMaster.Cells(targetRow, targetColumn).PasteSpecial) xlPasteValues
另请注意,对于此问题:
Unfortunately, I want only the values; bringing over formulas and formatting will screw up other behavior in the sheet.
您可以获取范围的 .Value
作为数组,并将其直接写入其他 sheet,而无需调用 Copy
或 Paste/PasteSpecial
方法。下面的答案显示了从一个工作簿到另一个工作簿的 copying/pasting 的几种方法,但可以很容易地修改为 sheet-to-sheet 转移。
Copy from one workbook and paste into another