Thanks for your rash help, Mike!
Unfortunately I can't use ActiveWorkbook because it's too specific, but your reply pushed me into the right direction.
To the task: my source table is a pivot table that calculates monthly costs which are sorted by cost centre row-wise and by cost description column-wise. The destination table has many sheets, each of them is named after a cost centre. I have to pull the right data from the source table and transport it to the destination table - right cost description into the right sheet (=cost centre)!
Now my code is nearly complete, but I'm getting runtime error: types icompatible.
Why???
Here's the entire code ("glued" to the UserForm):
Code:
Dim strSheet As String
Dim strKst As String
Dim lSpalte
Dim z, s, t
Dim WBQuelle, WBZiel As Workbook
'RefEdit Box on the UserForm, where user inputs the cost centre
strSheet = RefEdit2.Value
'and the column, where the costs should be imported into
lSpalte = RefEdit3.Value
'sets source WB
Set WBQuelle = Workbooks("Versuch November 2008.xls")
'and destination
Set WBZiel = Workbooks("Versuch November 2007.xls")
'calculates last row and last column:
LetzteZeile = WBQuelle.Sheets("Tabelle1").Range("A1").SpecialCells(xlCellTypeLastCell).Row
LetzteSpalte = WBQuelle.Sheets("Tabelle1").Range("A1").SpecialCells(xlCellTypeLastCell).Column
LetzteZeile1 = WBZiel.Sheets("Tabelle3").Range("A1").SpecialCells(xlCellTypeLastCell).Row
Sheets(strSheet).Activate
If RefEdit3.Value = "a" Then
lSpalte = 1
ElseIf RefEdit3.Value = "b" Then
lSpalte = 2
ElseIf RefEdit3.Value = "c" Then
lSpalte = 3
Else
lSpalte = 4
End If
'checks for matching data
'check the destination row
For s = 1 To LetzteZeile1
'loops the source table, rows
For z = 1 To LetzteZeile
'now look for matches columnwise:
For t = 1 To LetzteSpalte
'the first row of the source table are cost centers; store them in the string
'error comes here:
strKst = WBQuelle.Sheets("Tabelle1").Cells(1, t).Value
If WBQuelle.Sheets("Tabelle1").Cells(z, 1).Value = WBZiel.Sheets(strSheet).Cells(s, 1).Value Then
'determine the right cost centre:
If strKst = strSheet Then
WBQuelle.Sheets("Tabelle1").Cells(z, 4).Copy Destination:=WBZiel.Sheets(strSheet).Cells(s, lSpalte)
MsgBox "z = " & z & " s = " & s, vbOKCancel, Ausgabe
End If
End If
Next t
Next z
Next s
Thanks in advance for your help!
Best,
OfficeDummy