VBA 打开多个工作簿,复制特定数据,删除重复行并将信息粘贴到新工作簿中 [英] VBA to open several workbooks, copy specific data, remove duplicate rows and paste the information in a new workbook

查看:146
本文介绍了VBA 打开多个工作簿,复制特定数据,删除重复行并将信息粘贴到新工作簿中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道标题不是很清楚,但我希望我能在这个描述中更好地解释它.我是 VBA 新手,我需要编写一些代码来执行以下操作:

I know the title is not that clear but I hope I can explain it better in this description. I'm new to VBA and I need to write some code that does the following:

.打开特定文件夹中的多个工作簿,并将信息从源工作表(只有一个活动)中间的表格复制到新工作簿中的目标 Sheet1.问题 1:表的列数相同但行数不同(最初它们从 A42 到 L##(?)每个源文件中的隐藏工作表都有第一个带有 1 和 0 的 A 列,因此我可以知道我的副本的范围并预格式化"我想要传输到目标文件的信息)

. Opens several workbooks in a specific folder and copies information from a table in the middle of the source sheets (only one actives) to a target Sheet1 in a new workbook. Problem 1: the tables have the same number of columns but different number of rows (originally they vary from A42 to L##(?) because users can add or remove rows, or leave them blank) so what I did was create a new hidden sheet in each of the source files that has a first A column with 1s and 0s so I could know the range of my copy and "pre-format" the information I want to transfer to the target file)

.将每个源文件的隐藏工作表中的信息复制到目标工作簿,从目标工作簿的Sheet1的第二行开始(对于被复制的表)——第一行会有预先写好的表头——并不断粘贴来自目标工作表第一个可用空白行中的下一个文件的信息

. Copies the information from the hidden sheet of each source file to a target workbook, starting at the second row of Sheet1 of the target workbook (for the table being copied) - first row will have a pre-written header in advance - and keeps pasting information from the next files in the first available blank row of the target sheet

.删除重复行:如果用户多次运行宏,将看不到重复多次的原始表(还没有到这一步)

. Removes duplicate rows: in case the user runs the macro more than one time, won't see the original tables replicated several times (haven't got this far yet)

我对 VBA 知之甚少,所以这是我复制粘贴我在网上搜索的不同内容的程度(顺便说一句,代码未按预期工作):

I know very little about VBA so this is how far I've come copy-pasting different stuff I searched online (btw, code is not working as intended):

Sub ImportWorksheets()
Dim sFile As String           'file to process
Dim wsTarget As Worksheet
Dim wbSource As Workbook
Dim wsSource As Worksheet

'check the folder exists
If Not FileFolderExists(FOLDER_PATH) Then
  MsgBox "Specified folder does not exist, exiting!"
  Exit Sub
End If

'reset application settings in event of error
On Error GoTo errHandler
Application.ScreenUpdating = False

'set up the target worksheet
Set wsTarget = Sheets("Sheet1")

Dim NextRow0 As Long
NextRow0 = 2
'using NextRow0 to paste the new tables in in target sheet

'loop through the Excel files in the folder
sFile = Dir(FOLDER_PATH & "*.xls*")
Do Until sFile = ""

  'open the source file and set the source worksheet - ASSUMED WORKSHEET(1)
  Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
  Set wsSource = wbSource.Worksheets(2) 'EDIT IF NECESSARY

  Dim lRow As Long

  lRow = wsSource.Columns("A").Find(1, SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Row
  wsSource.Range("B1:N" & lRow).Copy Destination:=wsTarget.Range("A" & NextRow0)
  NextRow0 = wsTarget.Range("A100000").End(xlUp).Row

  'close the source workbook, increment the output row and get the next file
  wbSource.Close SaveChanges:=False
  sFile = Dir()
Loop

errHandler:
On Error Resume Next
Application.ScreenUpdating = True


'tidy up
Set wsSource = Nothing
Set wbSource = Nothing
Set wsTarget = Nothing
End Sub

现在代码不是将信息粘贴为值而是路径,并且没有复制正确的信息(第二列返回#REF).你能帮我弄清楚如何更正错误并结束代码吗?

Right now the code is not pasting the information as values but rather paths and is not copying the right information (second column is returning #REF). Can you help me figure out how to correct what's wrong and end the code?

推荐答案

replace

  wsSource.Range("B1:N" & lRow).Copy Destination:=wsTarget.Range("A" & NextRow0)

 wsSource.Range("B1:N" & lRow).Copy 
 wsTarget.Range("A" & NextRow0).pastespecial xlpastevalues

将您的数据从公式(即#REF)转换为值.假设您的其余代码可以解决问题

to turn your data from formulas (ie #REF) into values. Assuming the rest of your code works that ought to fix things

这篇关于VBA 打开多个工作簿,复制特定数据,删除重复行并将信息粘贴到新工作簿中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆