【问题标题】:excel VBA to select and paste issueexcel VBA选择和粘贴问题
【发布时间】:2018-07-03 07:40:10
【问题描述】:

我遇到了问题

current.Worksheets("Sheet1").Range("A14").Select

我不知道我做错了什么,甚至粘贴也是目前的问题。

Sub copyall()

Dim lastrow As Long

lastrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
MsgBox (lastrow)

Dim source As Workbook
Dim current As Workbook
Dim x As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False


For i = 1 To lastrow

    x = Sheets("Sheet1").Cells(i, 1)


    Set source = Workbooks.Open(x)
    Set current = ThisWorkbook
    'source.Worksheets("Adjustment").Columns.EntireColumn.Hidden = False
    'source.Worksheets("Adjustment").Rows.EntireRow.Hidden = False

    Dim f As Range
    Set f = source.Worksheets("Adjustment").Cells.Find(what:="Adjustment Type", lookat:=xlWhole, MatchCase:=True)

    Dim lastrow_source As Integer
    y = source.Worksheets("Adjustment").Cells(f.Row + 1, f.Column)

    lastrow_source = Sheets("Adjustment").Cells(Rows.Count, 1).End(xlUp).Row
    MsgBox (y)
    source.Worksheets("Adjustment").Range(source.Worksheets("Adjustment").Cells(f.Row + 1, f.Column), Cells(lastrow_source, 23)).Select
    Application.CutCopyMode = False
    Selection.Copy
    current.Worksheets("Sheet1").Range("A14").Select
    ActiveSheet.Paste
    source.Close
    MsgBox ("Imported")
Next i

End Sub

【问题讨论】:

  • 尝试使用 Selection.PasteSpecial Paste:=xlPasteValues 而不是 Activesheet.Paste
  • 看看如何避免选择,看到这个 q & a 只是其中之一:stackoverflow.com/q/38833596/4961700

标签: vba excel


【解决方案1】:

我将首先在 VBA 的“项目”窗口中命名所有工作表。你左边有窗户。 单击工作表并为其命名。 然后你可以不使用 Sheets("") 或 Worksheets("") 来调用它。

要在你的情况下复制粘贴,我会使用这个:

NameSheet1.Range("A14").Copy _ destination:= NameSheet2.Range("Input Range")

让我知道这是否有效。

【讨论】:

  • 我有很多文件要从中复制,为什么必须将其命名为工作表,但对于我之前的行,当前确实可以正常工作,但复制后不再工作
【解决方案2】:
source.Worksheets("Adjustment").Range(source.Worksheets("Adjustment").Cells(f.Row + 1, 
f.Column), Cells(lastrow_source, 23)).Copy _
        Destination:=current.Sheets("HBA Billings").Range("H" & lastrow_HBA)

【讨论】:

  • 如果您能添加一点解释说明此代码为何以及如何提供问题的答案,那就太好了。
【解决方案3】:

这两行应该可以工作:

Application.CutCopyMode = False
Source.Worksheets("Adjustment").Range(Source.Worksheets("Adjustment").Cells(f.Row + 1, f.Column), Source.Worksheets("Adjustment").Cells(lastrow_source, 23)).Copy current.Worksheets("Sheet1").Range("A14")

代替:

source.Worksheets("Adjustment").Range(source.Worksheets("Adjustment").Cells(f.Row + 1, f.Column), Cells(lastrow_source, 23)).Select
Application.CutCopyMode = False
Selection.Copy
current.Worksheets("Sheet1").Range("A14").Select
ActiveSheet.Paste

以上方式是如何避免SelectSelection的一个很好的例子,非常建议

【讨论】:

    猜你喜欢
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多