Пожалуйста, покажите нам свою первую попытку. Не стесняйтесь добавлять комментарии, как
' I need this to do XXXX here, but I don't know how
Вот несколько подсказок:
Чтобы просмотреть все листы в книге, используйте:
For each aSheet in MyWorkbook.Sheets
Чтобы пропустить некоторые конкретные листы, скажите:
If aSheet.Name <> "Investments" And aSheet.Name <> "Funds"
Чтобы скопировать из aSheet в MasterSheet, начните с установки начальных адресатов:
set rSource = aSheet.range("F9")
set rDestin = MasterSheet.range("C4")
Затем в вашем цикле вы делаете копию:
rDestin.Value = rSource.Value
... и настройте следующий набор местоположений
set rSource = rSource.offset(1,0)
set rDestin = rDestin.offset(1,0)
Это помогает?
РЕДАКТИРОВАТЬ: Вкратце глядя на вашу версию, я думаю, что эта часть не будет работать:
If ws.Name <> "Funds" And ws.Name <> "Investments" Then
Next ws
Разве вы не хотите удалить эту последнюю строку?
РЕДАКТИРОВАТЬ 2: Вы часто используете это:
wb.Worksheets.<something>
Но это не относится к конкретному рабочему листу. Вы хотите использовать "ws", например так:
ws.Range("F9")
БОЛЬШОЕ РЕДАКТИРОВАНИЕ:
Внимательно изучите эту версию и посмотрите, как она работает:
Sub ImportInformation()
WorksheetLoop
End Sub
Function WorksheetLoop()
Dim wb As Workbook
Dim ws As Worksheet
Dim foundCell As Range
Dim strFind As String
Dim fRow, fCol As Integer
'*** Adding Dims:
Dim wf, FilePicker
Dim NOI As Worksheet
Dim myPath As String
Dim PasteRow As Long
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
' This allows you to use excel functions by typing wf.<function name>
Set wf = WorksheetFunction
'Set the name of your output file, I assume its fixed in the Master File
'Please note that I am running this out of the master file and I want it all in the Noi tab
Set NOI = ThisWorkbook.Worksheets("NOI")
'Retrieve Target File Path From User
' Set FilePicker = Application.FileDialog(msoFileDialogFolderPicker)
'This only selects a folder, however I would like it to select a SPECIFIC FILE
' With FilePicker
' .Title = "Select A Target Folder"
' .AllowMultiSelect = False
' If .Show <> -1 Then GoTo NextCode
' myPath = .SelectedItems(1) & "\"
' End With
Dim WorkbookName As Variant
' This runs the "Open" dialog box for user to choose a file
WorkbookName = Application.GetOpenFilename( _
FileFilter:="Excel Workbooks, *.xl*", Title:="Open Workbook")
Set wb = Workbooks.Open(WorkbookName)
' initialize the starting cell for the output file
PasteRow = 4
'I need this to be referring to the file that I choose
For Each ws In wb.Worksheets
If ws.Name <> "Funds" And ws.Name <> "Investments" Then
' **** Leave this out: Next ws
ws.Range("F9").Copy '<--- You mean this, not wb.Worksheets.Range.("F9").Copy
NOI.Range("C" & PasteRow).PasteSpecial xlPasteValues, Transpose:=False
'Get find String
strFind = NOI.Range("C2").Value
'Find string in Row 16 of each row of current ACTIVE worksheet
Set foundCell = ws.Range("A16:IT16").Find(strFind, LookIn:=xlValues)
'If match cell is found
If Not foundCell Is Nothing Then
'Get row and column
fRow = foundCell.Row
fCol = foundCell.Column
'Copy data from active data worksheet “data” and copy over 300 columns (15 years).
' This is needed to find what specific date to start at. This portion works, I just need it to loop through each worksheet.
ws.Range(Cells(fRow + 1, fCol).Address & ":" & Cells(fRow + 1, fCol + 299).Address).Copy
'Paste in NOI tab of mater portfolio
NOI.Range("E" & PasteRow).PasteSpecial xlPasteValues, Transpose:=False
'*** Move PasteRow down by one
PasteRow = PasteRow + 1
wb.Application.CutCopyMode = False
Else
Call MsgBox("Try Again!", vbExclamation, "Finding String")
End If
End If
Next ws
wb.Close SaveChanges:=False
End Function