[英]Copy and paste after the last row VBA issue
I have a problem with my code.我的代码有问题。 It does the trick of copy/paste correctly.
它可以正确复制/粘贴。 However, I think there is something tricky.
但是,我认为有些棘手。 When I try to update my dynamic table it displays a message that the WB I'm currently working in already has data and if I want to replace it.
当我尝试更新我的动态表时,它会显示一条消息,指出我当前正在使用的 WB 已经有数据,如果我想替换它。 When I choose "Yes/No" it immediately displays another column in my table that says that
81 registers are not been used in UTILITY
.当我选择“是/否”时,它会立即在我的表中显示另一列,表示
81 registers are not been used in UTILITY
。 When I do everything by hand there are no problems.当我手工完成所有事情时,没有任何问题。 So, I guess there´s something wrong with my macro.
所以,我想我的宏有问题。
Option Explicit
Sub DailyTrans_MDM()
Call CopyPaste
End Sub
Sub CopyPaste()
Dim vFile As Variant
Dim folderPath As String
Dim wbCopyTo As Workbook
Dim wsCopyTo As Worksheet
Dim wbCopyFrom As Workbook
Dim wsCopyFrom As Worksheet
vFile = Dir(folderPath & "*.xl*")
Set wbCopyTo = ActiveWorkbook
Set wsCopyTo = ActiveSheet
Do While vFile <> ""
Application.ScreenUpdating = False
vFile = Application.GetOpenFilename("Daily Reports (*.xl*)," & "*.xl*", 1, "Select Report", "Open File", False)
If TypeName(vFile) = "Boolean" Then
Exit Sub
Else
Set wbCopyFrom = Workbooks.Open(vFile)
Set wsCopyFrom = wbCopyFrom.Worksheets("ReporteCifrasControl")
End If
'--------------------------------------------------------------------------------------
wsCopyFrom.Range("A2:M" & wsCopyFrom.Range("A" & Rows.Count).End(xlUp).row).Copy
wsCopyTo.Range("A" & wsCopyTo.Range("A" & Rows.Count).End(xlUp).row + 1).PasteSpecial xlPasteValuesAndNumberFormats
wbCopyFrom.Close SaveChanges:=False
Dim rngCopy As Range, rngPaste As Range
With ActiveSheet
Set rngCopy = .Range(.Range("A2"), Cells(2, Columns.Count).End(xlToLeft))
Set rngPaste = .Range(.Range("A2"), .Cells(Rows.Count, 1).End(xlUp)).Resize(, rngCopy.Columns.Count)
End With
rngCopy.Copy
rngPaste.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
Loop
End Sub
I believe you want to do this我相信你想这样做
Column A to Last Used Column (LC)
Column A to Last Used Column (LC)
复制Column A to Last Used Column (LC)
Non-Used Row (LR)
in Column A
Column A
Non-Used Row (LR)
中Dim LC As Long
Dim LR As Long
With ActiveSheet
LC = .Cells(2, .Columns.Count).End(xlToLeft).Column
LR = .Range("A" & .Rows.Count).End(xlUp).Row
Set rngCopy = .Range(.Cells(1, 2), .Cells(LC, 2))
Set rngPaste = .Range("A" & LR)
End With
rngCopy.Copy
rngPaste.PasteSpecial xlPasteFormats
You missed some objects to be qualified in your code.您错过了一些需要在代码中限定的对象。 No point in using the
With Block
if you are not going to use the With Block
在使用没有点
With Block
,如果你不打算使用的With Block
Just realized you have multiple copy/pastes in your code.刚刚意识到您的代码中有多个复制/粘贴。 If this is the wrong one, use the format here to modify the other one.
如果这是错误的,请使用此处的格式修改另一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.