简体   繁体   English

使用VBA根据另一个工作表中的值填充单元格

[英]Populating Cells using VBA based on Values from another sheet

I am trying to figure out a tidy way to write the following scenario. 我试图找出一种编写以下情况的整洁方法。

Worksheet A Has a list that, from project to project will vary in the number of rows in it. 工作表A具有一个列表,该列表从项目到项目在行数上将有所不同。

Example of information in Worksheet A. Data to be used in Calendar Month column. 工作表A中的信息示例。“日历月”列中使用的数据。

工作表A

Worksheet B Tracking sheet that is using the list from Worksheet A, to populate the titles of columns going across the sheet. 工作表B正在使用工作表A中的列表的跟踪表,以填充工作表中的列标题。

工作表B-手动创建,尝试自动执行

I want the script to use the value in each of the rows (3 times each before moving to the next row) from Worksheet A, across the top of worksheet B to create the titles for a table. 我希望脚本使用工作表B顶部的工作表A的每一行中的值(在移至下一行之前,每行3次)为表创建标题。


Sub TrackingDays()

Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Tracking (DAYS)"

Sheets("Tracking (DAYS)").Select
Sheets("Tracking (DAYS)").Range("A3").Select
ActiveCell.Value = "Ref." & Chr(10) & "#"

ActiveCell.Offset(1, 0).Select
ActiveCell.Value = 1

Do Until ActiveCell.Value = 100
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + 1
Loop

Sheets("Tracking (DAYS)").Range("B3").Select
ActiveCell.Value = "Resource Name"

Sheets("Tracking (DAYS)").Range("C3").Select
ActiveCell.Value = "Resource" & Chr(10) & "Status"

Sheets("Tracking (DAYS)").Range("D3").Select
ActiveCell.Value = "Days Per" & Chr(10) & "Week"

Sheets("Tracking (DAYS)").Range("E3").Select
ActiveCell.Value = "Whole" & Chr(10) & "Contract" & Chr(10) & "Summary" & Chr(10) & "(Forecast)" & Chr(10) & "Calendar"
With ActiveCell.Characters(Start:=1, Length:=20).Font
    .Name = "Calibri"
    .FontStyle = "Regular"
    .Size = 11
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With

With ActiveCell.Characters(Start:=23, Length:=38).Font
    .Name = "Calibri"
    .FontStyle = "Regular"
    .Size = 9
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With

Sheets("Tracking (DAYS)").Range("F3").Select
ActiveCell.Value = "Whole" & Chr(10) & "Contract" & Chr(10) & "Summary" & Chr(10) & "(Forecast)" & Chr(10) & "PSA"
With ActiveCell.Characters(Start:=1, Length:=20).Font
    .Name = "Calibri"
    .FontStyle = "Regular"
    .Size = 11
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With

With ActiveCell.Characters(Start:=23, Length:=38).Font
    .Name = "Calibri"
    .FontStyle = "Regular"
    .Size = 9
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With

Sheets("Tracking (DAYS)").Range("G3").Select
ActiveCell.Value = "Whole" & Chr(10) & "Contract" & Chr(10) & "Summary" & Chr(10) & "(Actual)" & Chr(10) & "Calendar"
With ActiveCell.Characters(Start:=1, Length:=20).Font
    .Name = "Calibri"
    .FontStyle = "Regular"
    .Size = 11
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With

With ActiveCell.Characters(Start:=23, Length:=38).Font
    .Name = "Calibri"
    .FontStyle = "Regular"
    .Size = 9
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With

Dim MonthTitle As Variant
MonthTitle = ActiveCell.Value

Sheets("Project Information & Setup").Select
Sheets("Project Information & Setup").Range("N4").Select
Sheets("Tracking (DAYS)").Select
Sheets("Tracking (DAYS)").Range("H3").Select
Sheets("Project Information & Setup").Select

Do Until IsEmpty(ActiveCell)

    MonthTitle = ActiveCell.Value
    MonthTitle = Format(MonthTitle, "MMM-yy")
    Sheets("Tracking (DAYS)").Select

    ActiveCell.Value = MonthTitle & Chr(10) & "(Forecast)" & Chr(10) & "Calendar"
    ActiveCell.Offset(0, 1).Select
    With ActiveCell.Characters(Start:=1, Length:=7).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With ActiveCell.Characters(Start:=8, Length:=19).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 9
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With

    ActiveCell.Value = MonthTitle & Chr(10) & "(Forecast)" & Chr(10) & "PSA"
    ActiveCell.Offset(0, 1).Select
    With ActiveCell.Characters(Start:=1, Length:=7).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With ActiveCell.Characters(Start:=8, Length:=14).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 9
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With

    ActiveCell.Value = MonthTitle & Chr(10) & "(Actual)" & Chr(10) & "Calendar"
    ActiveCell.Offset(0, 1).Select
    With ActiveCell.Characters(Start:=1, Length:=7).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With ActiveCell.Characters(Start:=8, Length:=17).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 9
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With

Sheets("Project Information & Setup").Select
ActiveCell.Offset(1, 0).Select

Loop

I believe the following code will do what you expect, it will loop through Sheet A, format the date accordingly and then write your headers in Sheet B as illustrated in the image starting in the Column A in Sheet B: 我相信以下代码将达到您的期望,它将遍历工作表A,相应地设置日期格式,然后按照工作表B中列A的图像所示,在工作表B中写入标题:

Sub foo()
Dim wsA As Worksheet: Set wsA = Sheets("A")
Dim wsB As Worksheet: Set wsB = Sheets("B")
'above declare and set the worksheet you are working with, amend as required
LastRow = wsA.Cells(wsA.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A in Sheet A
LastCol = 1
For i = 3 To LastRow 'loop through Sheet A starting in Row 3 to last row with data
    Data = Format(wsA.Cells(i, 1).Value, "mmm-yy") 'format date appropriately
    For x = 1 To 3 'loop 3 times as mentioned
        Select Case x
            Case 1
                wsB.Cells(1, LastCol).Value = Data & " (Forecast) Calendar"
            Case 2
                wsB.Cells(1, LastCol).Value = Data & " (Forecast) PSA"
            Case 3
                wsB.Cells(1, LastCol).Value = Data & " (Actual) Calendar"
        End Select
        LastCol = wsB.Cells(1, wsB.Columns.Count).End(xlToLeft).Column + 1
        'get the next free column
    Next x
Next i
End Sub

Just an idea how to do it more efficient. 只是一个想法,如何使其更有效。 You might want to improve the formatting part, just tried to illustrate how it could be done. 您可能想改进格式化部分,只是试图说明如何完成。

Option Explicit

Public Sub GenerateHeaders() 
    Dim WsSource As Worksheet
    Set WsSource = ThisWorkbook.Worksheets("SheetA") 'define source worksheet

    Dim WsDestination As Worksheet
    Set WsDestination = ThisWorkbook.Worksheets("SheetB") 'define destination worksheet

    Dim LastRow As Long
    LastRow = WsSource.Cells(WsSource.Rows.Count, "A").End(xlUp).Row 'find last used row in column A

    Const FirstRow As Long = 2 'first row with data (because of header)

    Dim ActColumn As Long

    Dim iRow As Long
    For iRow = FirstRow To LastRow
        ActColumn = (iRow - (FirstRow - 1)) * 3 - 2
        With WsDestination.Cells(1, ActColumn)
            .Value = WsSource.Cells(iRow, "A").Text & vbLf & "(Forecast)" & vbLf & "Calendar"
            .Font.Size = 9
            .Characters(Start:=1, Length:=Len(WsSource.Cells(iRow, "A").Text)).Font.Size = 11
        End With

        With WsDestination.Cells(1, ActColumn).Offset(0, 1)
            .Value = WsSource.Cells(iRow, "A").Text & vbLf & "(Forecast)" & vbLf & "PSA"
            .Font.Size = 9
            .Characters(Start:=1, Length:=Len(WsSource.Cells(iRow, "A").Text)).Font.Size = 11
        End With

        With WsDestination.Cells(1, ActColumn).Offset(0, 2)
            .Value = WsSource.Cells(iRow, "A").Text & vbLf & "(Actual)" & vbLf & "Calendar"
            .Font.Size = 9
            .Characters(Start:=1, Length:=Len(WsSource.Cells(iRow, "A").Text)).Font.Size = 11
        End With
    Next iRow

End Sub

Note that I used WsSource.Cells(iRow, "A").Text to get the date of the source sheet exactly in the format as it is formatted there. 请注意,我使用WsSource.Cells(iRow, "A").Text来获取源工作表的日期,其格式必须与在此格式的格式完全相同。 If you want to change that you would need something like Format(WsSource.Cells(iRow, "A").Value, "mmm-yy") 如果要更改,则需要类似Format(WsSource.Cells(iRow, "A").Value, "mmm-yy")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在VBA中填充不同工作表中的单元格 - Populating cells from a different sheet in VBA vba / excel-根据用户输入到sheet1的单元格中,从sheet2的值填充sheet1中的单元格 - vba/excel - populate cells in sheet1 from values in sheet2 based on user input into cells on sheet1 VBA-根据多个条件从另一张纸复制单元格 - VBA - copy cells from another sheet based on multiple criteria VBA格式的单元格基于另一个工作表 - VBA Format Cells based on another sheet 根据另一个工作表中的值在多个单元格中绘制圆圈 - Draw circles in multiple cells based on values from another sheet 使用相邻单元格的值填充一列单元格(Excel VBA) - Populating a Column of Cells with Values from Adjacent Cells (Excel VBA) 根据与VBA在Excel中匹配的值从另一张表中获取数据 - Grab data from another sheet based on values matching in Excel with VBA VBA“=活动单元格”来自另一个工作表 - VBA "=active cells" from another work sheet Excel VBA - 根据 3 个条件(复杂的 IF AND 相关 VBA 与通配符)使用另一张工作表中的值填充一张工作表上的列 - Excel VBA - Populate a column on one sheet with values from another sheet based on 3 criteria (complicated IF AND related VBA with wildcards) 如何使用XlsxWriter根据另一个工作表中的相应单元格值来格式化一个工作表中的所有单元格? - How to format all cells in one sheet based on corresponding cell values in another sheet using XlsxWriter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM