2017-09-01 52 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

奏效的名字!谢谢! –

相关问题