2017-07-20 2 views
2

Ich löste das Problem, das ich anfangs hatte, teilweise und stellte fest, dass meine Beschreibung des Problems ein wenig zu detailliert war. Ich habe mich entschieden, meine Frage neu zu schreiben, damit es leichter ist, das Problem zu verstehen, und Leute, die nach dem gleichen Ding suchen, können sich schneller verbinden.Dynamischer Verweis auf geschlossene Arbeitsmappe in VBA

Ich habe mehrere Themendateien (jede mit einem anderen Namen) mit 21 Zeilen und 21 Spalten, die in 1 Datei gesammelt werden müssen (Zusammenfassung genannt). In Zusammenfassung möchte ich einen Code, der eine Liste der Themennamen sucht und dann einen Verweis in die Zellen auf die entsprechenden Zellen in der Themendatei platziert. Wie Sie im folgenden Code sehen können, habe ich eine vereinfachte Version davon erstellt. Es betrachtet die Zelle mit dem Namen der ersten Themendatei und erstellt dann eine Referenz für alle Zeilen und Spalten in dieser Datei.


Sub PullValue() 
Dim path, file, sheet 
Dim i As Integer, j As Integer 

Application.ScreenUpdating = False 


path = Worksheets("Settings").Range("B23") 
file = Worksheets("Consolidation").Range("A1") 
sheet = "Overview" 

For i = 2 To 22 
    For j = 1 To 21 
     Cells(i, j).Formula = "='" & path & "[" & file & ".xlsm]" & _ 
     sheet & "'!" & Cells(i - 1, j).Address & "" 
    Next j 
Next i 

Application.ScreenUpdating = True 

End Sub 


Dies funktioniert, wie es sollte, aber danach hat er dies in diesem Thema Namentabelle für alle Dateien zu tun. Ich werde es weiter versuchen, aber Hilfe wäre sehr willkommen, danke.

Wenn weitere Informationen erforderlich sind, zögern Sie nicht zu fragen.

Danke! Bart

Antwort

0

Nach einer Menge von Forschung und Versuch & Fehler, kam ich mit meiner eigenen Lösung. Ich werde es hier teilen, damit Leute, die sich mit dem gleichen Problem befassen, hier etwas einholen können.

Ich habe dem Code Kommentare hinzugefügt, damit er leichter zu verstehen ist.

Sub PullValue() 
Dim path, file, sheet 
Dim LastRow As Long, TopicCount As Long 
Dim i As Integer, j As Integer, a As Integer 

Application.ScreenUpdating = False 

'1. We count how many topics are written in the Topics table to decide the amount of loops 
'I do this by checking the total rows in the table and subtract the empty ones 
With Worksheets("Settings").ListObjects("Topics") 

    TopicCount = .DataBodyRange.Rows.Count - _ 
    Application.CountBlank(.DataBodyRange) 

End With 

'2. We loop the code for the amount of times we just calculated so it does it for all topics 
'I'll make a note where we can find that a in the code 
For a = 1 To TopicCount 

    '3. In the consolidation sheet where all the data will be, we want to check what the 
    'LastRow is in column A to get the starting point of where the data is entered 
    With Worksheets("Consolidation") 

     LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 

    End With 

    '4. This If function puts a spacing between the blocks of data if the LastRow is not the 
    'first row. This is only to make it visually look better. 
    If LastRow <> 1 Then 

     LastRow = LastRow + 2 

    End If 

    '5. The first thing we want to do is put the name of the topic below the LastRow so it's 
    'easy to check afterwards what topic the block of data belongs to. Here you can find the 
    'a from the loop we have in the beginning. 
    Cells(LastRow, "A").Value = [Topics].Cells(a, [Topics[Topics]].Column) 

    '6. Locate where the path, file and sheet names are located in the document. Don't 
    'forget to put the/at the end if it's a website or the \ if it's on your computer. 
    'If you look to the code at comment number 7, we already have the .xlsm in the formula 
    'so we don't need to enter that for the file. 
    path = Worksheets("Settings").Range("D2") 
    file = Worksheets("Consolidation").Cells(LastRow, 1) 
    sheet = "Overview" 

    'This is the core of the code and will the right reference in the right cell. This loops 
    'for all the 21 rows and columns. 
    For i = LastRow + 1 To LastRow + 21 

     For j = 1 To 21 

      Cells(i, j).Formula = "='" & path & "[" & file & ".xlsm]" & _ 
      sheet & "'!" & Cells(i - LastRow, j).Address & "" 

     Next j 

    Next i 

Next a 

Application.ScreenUpdating = True 

End Sub 

Haben Sie Fragen zum Code, lassen Sie es mich wissen. Ich hoffe, dass dies einigen Menschen helfen könnte. Verbesserungen sind natürlich auch willkommen.

Bart

Verwandte Themen