使用作为行数据源的工作表名称填充单元格

时间:2017-09-01 22:39:35

标签: excel vba excel-vba

我从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 个答案:

答案 0 :(得分: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