-->

VBA to open several workbooks, copy specific data,

2019-03-04 13:50发布

问题:

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:

. 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)

. 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)

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

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?

回答1:

replace

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

with

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

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