vba - VBA:将数据从多张excel表复制到一个目标文件/表
问题描述
我正在尝试从特定 Excel 的多个工作表中复制数据并粘贴到主工作表中。我编写了以下代码;尽管它没有抛出任何错误,但它不会返回到主工作表(代码中的“DestFile”),也不会粘贴数据。在这里感谢任何支持。
Sub Monthly_Balance_Fetcher_Click()
Dim DestFile As Workbook, SourceFile As String, GetBook As String, SourceBook As String, myNum As Long, LatestDate As Long, SelectedDate As Long
LatestDate = Range("D1").Value
Set DestFile = ThisWorkbook 'ThisWorkbook is always the workbook that has the code (as opposed to ActiveWorkbook)
GetBook = ActiveWorkbook.Name
SourceFile = Application.GetOpenFilename(Title:="Please browse for the latest monthly TB file, prefer if you save it to your C first")
Workbooks.Open (SourceFile)
SourceBook = ActiveWorkbook.Name
Sheets("Group").Select
myNum = Application.InputBox("Please enter the column number you want to copy from")
Cells(3, myNum).Select
SelectedDate = Cells(3, myNum).Value
If SelectedDate > LatestDate Then
For i = 1 To totalsheets
If Worksheets(i).Name <> "Summary" And Worksheets(i).Name <> "Process Steps" And Worksheets(i).Name <> "Sheet1" And Worksheets(i).Name <> "Adjustments" And Worksheets(i).Name <> "Targets" Then
Worksheets(i).Activate
Range(Cells(6, myNum)).Copy
Range(Cells(13, myNum)).PasteSpecial Paste:=xlPasteValues
DestFile.Activate
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
Cells(lastrow + 1, 3) = Worksheets(i).Name
Range(Cells("G" & lastrow + 1)).PasteSpecial Paste:=xlPasteValues
Cells(lastrow + 1, 8) = SelectedDate
End If
Next
Else
MsgBox "Data from the selected date already exists! This macro will now stop", vbExclamation
DestFile.Activate
Cells(3, 5) = SelectedDate
Cells(2, 4) = LatestDate
End If
End Sub
解决方案
推荐阅读
- oracle - 同一台服务器上的 Oracle XE 和 Standard
- javascript - data-turbolinks false 不适用于正常的锚标记
- php - 如何使用 htaccess 在 codeignator 中重定向 URL?
- eclipse-plugin - Eclipse IDE 变量、范围和存储
- javascript - 带有“if else”指令和日期的未处理承诺拒绝
- java - 单击 ListView 项目并在另一个活动中显示其数据库内容
- python - sorted() 自动按元组的第二个元素排序
- encryption - 在 OutSystems 应用中实现 AES/RES 加密解密
- python - Appending specific indices from zipped lists
- jsse - Tomcat8不断显示https-jsse-nio-443-exec日志