Vba 如何将特定单元格从其他工作表复制/粘贴到当前工作表?
我使用这段代码获取用户名,并从多个交易中搜索他们的所有关联信息。然后将它们粘贴到当前工作表中。它似乎在运行,因为它不会抛出任何错误并执行最终的“Select”命令,但不会返回任何粘贴的数据Vba 如何将特定单元格从其他工作表复制/粘贴到当前工作表?,vba,excel,Vba,Excel,我使用这段代码获取用户名,并从多个交易中搜索他们的所有关联信息。然后将它们粘贴到当前工作表中。它似乎在运行,因为它不会抛出任何错误并执行最终的“Select”命令,但不会返回任何粘贴的数据 Option Explicit Sub InvestorReport() Dim investorname As String Dim finalrow As Integer Dim i As Integer 'row counter Sheets("Sheet1").Range("D6:K50").Cl
Option Explicit
Sub InvestorReport()
Dim investorname As String
Dim finalrow As Integer
Dim i As Integer 'row counter
Sheets("Sheet1").Range("D6:K50").ClearContents
investorname = Sheets("Sheet1").Range("B3").Value
finalrow = Sheets("Investments").Range("I1000").End(xlUp).Row
For i = 2 To finalrow
If Sheets("Investments").Cells(i, 1) = investorname Then
MsgBox ("Works")
Range(Cells(i, 2), Cells(i, 12)).Copy
Sheets("Sheet1").Range("D100").End(xlUp).Offset(1, 0).PasteSpecial
End If
Next i
Range("B3").Select
End Sub
这是正确参考图纸的代码:
Option Explicit
Sub InvestorReport()
Dim investorname As String
Dim finalrow As Long
Dim i As Long 'row counter
With Sheets("Sheet001") '<--| refer to "Sheet1" sheet
.Range("D6:K50").ClearContents '<--| with every initial "dot" you keep referencing the object after the "With" keyword ("Sheet1" sheet, in this case)
investorname = .Range("B3").value
End With
With Sheets("Investments") '<--| refer to "Investments" sheet
finalrow = .Cells(.Rows.Count, "I").End(xlUp).row '<--| .Cells and .Rows refer to "Investments" sheet
For i = 2 To finalrow
If .Cells(i, 1) = investorname Then
.Range(.Cells(i, 2), .Cells(i, 12)).Copy '<--| .Range and .Cells refer to "Investments" sheet
Sheets("Sheet001").Range("D100").End(xlUp).Offset(1, 0).PasteSpecial '<--| here all references are explicitly made to "Sheet1" sheet
End If
Next i
End With
Sheets("Sheet001").Range("B3").Select
End Sub
选项显式
次级投资报告()
Dim investorname作为字符串
暗淡的最后一行
昏暗的长排柜台
对于图纸(“Sheet001”)“我强烈建议不要使用复制
和粘贴
,而是使用原始范围设置新范围的值。另外,我认为代码的问题在于没有指定要从中复制的工作表。更改以下行:范围(单元格(i,2),单元格(i,12))。将复制到表格(“投资”)。范围(表格(“投资”)。单元格(i,2),表格(“投资”)。单元格(i,12))。复制
为什么循环?你看到了吗?你确定要把MsgBox放在循环过程中吗?循环是因为一个用户名可能有多个条目,我需要从中获取信息。Msgbox只是我试着测试它,忘了注释掉/删除。Jordan是对的。您需要指定在.Copy中引用的工作表command@SWiM当前位置你熬过了吗?
Sub InvestorReport2()
Dim investorname As String
Dim finalrow As Long
Dim i As Long 'row counter
With Sheets("Sheet001") '<--| refer to "Sheet1" sheet
.Range("D6:K50").ClearContents '<--| with every initial "dot" you keep referencing the object after the "With" keyword ("Sheet1" sheet, in this case)
investorname = .Range("B3").value
End With
With Sheets("Investments") '<--| refer to "Investments" sheet
With .Range("A1", .Cells(.Rows.Count, "L").End(xlUp)) '<--| refer to "Investments" sheet columns A to I fom row 1 (header) down to last non empty one in column I
.AutoFilter field:=1, Criteria1:=investorname '<--| filter data on first column values matching "investorname"
If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell has been filtered...
.Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).SpecialCells(xlCellTypeVisible).Copy '<--| ... copy filtered cells skipping headers and first column...
Sheets("Sheet001").Range("D100").End(xlUp).Offset(1, 0).PasteSpecial '<--| ...and paste them
End If
End With
.AutoFilterMode = False '<--| .. show all rows back...
End With
Sheets("Sheet001").Range("B3").Select
End Sub