2017-09-01 84 views
0

我從我的Excel文件中的多個工作表拉取信息。使用作爲行數據源的工作表的名稱填充單元格

我想填寫一個列,根據哪一頁的信息在該行來自。

例如:

如果片B的數據是從表A截取,SheetB.ColumnE(SourceSheet)的值應爲 「A」 或 「B」

Private Sub Update_Click() 

Application.ScreenUpdating = False 
    Application.Calculation = xlCalculationManual 

    Dim path As String, fileName As String 
    Dim lastRowInput As Long, lastRowOutput As Long, rowCntr As Long, lastColumn As Long 
    Dim inputWS1 As Worksheet, outputWS As Worksheet 

    'set your sheets here 
    Set inputWS1 = ThisWorkbook.Sheets("Universal") 
    Set outputWS = ThisWorkbook.Sheets("Carriers") 
    rowCntr = 1 

    'get last rows from both sheets 
    lastRowInput = inputWS1.Cells(Rows.Count, "A").End(xlUp).Row 
    lastRowOutput = outputWS.Cells(Rows.Count, "A").End(xlUp).Row 
    lastColumn = inputWS1.Cells(1, Columns.Count).End(xlToLeft).Column 

    'copy data from columns A, B, E, G, I, J, L and M 
    inputWS1.Range("A4:A" & lastRowInput).Copy outputWS.Range("B2") 
    inputWS1.Range("B4:B" & lastRowInput).Copy outputWS.Range("C2") 


    Application.ScreenUpdating = True 
    Application.Calculation = xlCalculationAutomatic 
End Sub 

我當前的代碼是從通用表單上拖動信息到表單運營商。我想在E欄上加上「通用」一詞。

我會做更多的牀單,我以爲我就可以使用相同的代碼,爲他們提供在列E.

回答

1
'copy data from columns A, B, E, G, I, J, L and M 
inputWS1.Range("A4:A" & lastRowInput).Copy outputWS.Range("B2") 
inputWS1.Range("B4:B" & lastRowInput).Copy outputWS.Range("C2") 

outputWS.Range("E2:E" & (lastRowInput-2)).Value = inputWS1.Name '<< add name 
+0

奏效的名字!謝謝! –

相關問題