简体   繁体   English

根据单元格计数和 header 值插入指定文本字符串 X 次

[英]Insert specified text string X amount of times based on cell count and header value

UPDATED - Screenshots added / tables added已更新 - 添加了屏幕截图/添加了表格

Newbie to VBA here, so apologies as I am sure this is a simple task but have researched and tested to no avail. VBA 的新手在这里,所以很抱歉,因为我确信这是一个简单的任务,但已经研究和测试无济于事。

I am trying to re-format a standard report into a new file format for uploading.我正在尝试将标准报告重新格式化为新的文件格式以供上传。 I am trying to insert a text value X amount of times based on a header value.我正在尝试根据 header 值插入文本值 X 次。 Each column header differs (Dependent Care; Medical FSA; HSA), but must be spelled out as "Dependent Care FSA" or "Health Savings Plan" etc. and must run X amount of times down the same column (Column E) on a different sheet.每列 header 不同(家属护理;医疗 FSA;HSA),但必须拼写为“家属护理 FSA”或“健康储蓄计划”等,并且必须在同一列(E 列)上运行 X 次不同的表。

Here is a piece of the code I have written for this section so far, but can't seem to continuously find the last row of what was just inserted and continue to run down the column.到目前为止,这是我为此部分编写的一段代码,但似乎无法连续找到刚刚插入的内容的最后一行并继续沿列向下运行。 The number of instances will vary week after week so want this to be dynamic.实例的数量每周都会变化,因此希望这是动态的。 The column headers and values continue to go down from E1 to J1.列标题和值继续从 E1 到 J1 到 go。 The number of values are all the same, but those are what changes week after week.值的数量都是一样的,但这些都是每周都在变化的。 There may be 334 rows one week and 340 the next.一周可能有 334 行,下一周可能有 340 行。

NumToRepeat = wksSource.Range("C" & Rows.Count).End(xlUp).Row NumToRepeat = wksSource.Range("C" & Rows.Count).End(xlUp).Row

    If wksSource.Range("E1").Value = "Pre_Tax_FSA_Dependent_care(DR1)" And wksSource.Range("F1").Value = "Pre_Tax_FSA_Medical(DR1)" Then
                   
        wksDest.Range("E2").Select
        ActiveCell.FormulaR1C1 = "Dependent Care FSA"
        wksDest.Range("E2").Select
        Selection.AutoFill Destination:=wksDest.Range("E2:E" & NumToRepeat)

        wksDest.Range("E" & Cells(Rows.Count, "E").End(xlUp).Row + 1).Select
        ActiveCell.FormulaR1C1 = "Medical FSA"
        wksDest.Range("E" & Cells(Rows.Count, "E").End(xlUp).Row + 1).Select
        Selection.AutoFill Destination:=wksDest.Range("E" & NumToRepeat + 1, "E" & NumToRepeat * 2 - 1)
                   
    End If
    
End With

I can get these two to stack up, but can't get others to stack thereafter.我可以让这两个堆叠起来,但不能让其他人堆叠起来。 Whatever I write just copies over the second instance...无论我写什么,都只是复制第二个实例......

I just need HELP to continuously find the last row and pasting the text based on what is in the column headers.我只需要帮助来不断找到最后一行并根据列标题中的内容粘贴文本。 I apologize if this is all confusing (and elementary) but happy to clarify further and truly appreciate all the help in advance!!如果这一切都令人困惑(和基本),我深表歉意,但很高兴进一步澄清并真正感谢提前提供的所有帮助!

Source Data Example源数据示例

End Result Example最终结果示例

Here they are in table form: Source Table Data Sample它们以表格形式出现:Source Table Data Sample

Paydate付款日期 EE_Code EE_代码 SSN社会保障号 EE Name EE 名称 DepCareFSA DepCareFSA FSAMed FSAMed HSAemp HSAemp HSAer高铁 Parking停車處 Commuter通勤者
05/14/2021 2021 年 5 月 14 日 ABCD A B C D 123456789 123456789 JOHN DOE约翰·多伊 208.33 208.33 0 0 0 0 0 0 0 0 0 0
05/14/2021 2021 年 5 月 14 日 EFGH EFGH 111111111 111111111 JANE DOE简·多伊 0 0 0 0 0 0 38.46 38.46 0 0 0 0
05/14/2021 2021 年 5 月 14 日 IJKL IJKL 222222222 222222222 JERRY DOE杰瑞·多伊 0 0 0 0 0 0 38.46 38.46 0 0 0 0
05/14/2021 2021 年 5 月 14 日 MNOP MNOP 333333333 333333333 JILL DOE吉尔·多伊 115.38 115.38 0 0 190.38 190.38 86.54 86.54 0 0 0 0
05/14/2021 2021 年 5 月 14 日 QRST QRST 444444444 444444444 JIM DOE吉姆·多伊 0 0 0 0 190.38 190.38 86.54 86.54 0 0 0 0
05/14/2021 2021 年 5 月 14 日 UVWX紫外线WX 555555555 555555555 JEN DOE仁多 0 0 0 0 100 100 38.46 38.46 0 0 0 0

Trying to re-format to this... conditional based on what column the values are coming from to fill in column C and column E with column F being standard "Current" all the way down through all rows.尝试重新格式化为此...根据值来自的列来填充 C 列和 E 列,其中 F 列是标准的“当前”,一直到所有行。

EmployeeIdentifier员工标识符 ContributionDate投稿日期 ContributionDescription贡献描述 ContributionAmount贡献金额 PlanName计划名称 PriorTaxYear税前年
123456789 123456789 05142021 05142021 Payroll工资单 208.33 208.33 Dependent Care FSA依赖护理FSA Current当前的
111111111 111111111 05142021 05142021 Payroll工资单 0 0 Dependent Care FSA依赖护理FSA Current当前的
222222222 222222222 05142021 05142021 Payroll工资单 0 0 Dependent Care FSA依赖护理FSA Current当前的
333333333 333333333 05142021 05142021 Payroll工资单 115.38 115.38 Dependent Care FSA依赖护理FSA Current当前的
444444444 444444444 05142021 05142021 Payroll工资单 0 0 Dependent Care FSA依赖护理FSA Current当前的
555555555 555555555 05142021 05142021 Payroll工资单 0 0 Dependent Care FSA依赖护理FSA Current当前的

This would continue on with values from column E, followed by column F stacked underneath, and so on and so forth with the corresponding contribution type listed based on which column it came from.这将继续使用来自 E 列的值,然后是堆叠在下方的 F 列,依此类推,并根据其来自哪一列列出相应的贡献类型。

I am able to get everything setup (repeated SSN down the column, repeated dates... although in the incorrect format, corresponding values), but cannot figure out how to get the dependent names for column C and E to continuously find the last row, stack on top of each other while corresponding to the correct SSN and value... I haven't tried adding the column F where it says "current" all the way down but figure that's pretty easy?我能够进行所有设置(在列中重复 SSN,重复日期......虽然格式不正确,对应值),但无法弄清楚如何获取列 C 和 E 的从属名称以不断找到最后一行,堆叠在一起,同时对应于正确的 SSN 和值...我没有尝试将 F 列添加到它一直显示“当前”的位置,但是这很容易吗?

Any and all help is truly appreciated.任何和所有的帮助都非常感谢。 I am a proficient Excel user, but new to VBA and have been scouring to figure this all out.我是一位精通 Excel 用户,但对 VBA 不熟悉,并且一直在努力解决这一切。 I am about 75% of the way there, but need help with these steps...我已经完成了大约 75% 的路程,但需要这些步骤的帮助......

Thank you!谢谢!

Here's the solution for your case.这是您的情况的解决方案。 Tune sheets' names to ones you need.将工作表的名称调整为您需要的名称。 The helper SetTags function merely selects appropriate names based on header's value.助手SetTags function 仅根据标头的值选择适当的名称。 It uses ByRef in order to change these variables directly (and not use function which would return array).它使用ByRef来直接更改这些变量(而不使用 function 将返回数组)。 Before calling SetTags we need to clear these variables - this will give us a chance to catch an error if you misspell some text in SetTags (in this case there will be empty cells on the final worksheet).在调用SetTags之前,我们需要清除这些变量 - 如果您在SetTags中拼错了一些文本(在这种情况下,最终工作表上将有空单元格),这将使我们有机会发现错误。 If we don't clear them and you have misspelled some text, you will get wrong text on the final worksheet.如果我们不清除它们并且您拼错了一些文本,您将在最终工作表上得到错误的文本。 Also code uses two ranges - one with header ( rngTable ) and one without header ( rngData ).代码还使用两个范围 - 一个带有 header ( rngTable ),另一个没有 header ( rngData )。 rngData lets us easily transfer data to final worksheet without any further calculations. rngData让我们可以轻松地将数据传输到最终工作表,而无需任何进一步的计算。 Finally, since we know that the number of rows being copied is always the same, the next row (to insert to on the final worksheet) is calculated as current next row plus this number of rows.最后,因为我们知道被复制的行数总是相同的,所以下一行(要插入到最终工作表上)计算为当前下一行加上这个行数。

Option Explicit

Sub Transfer()

    Dim wksSource As Worksheet, wksDest As Worksheet
    Dim rngTable As Range, rngData As Range
    Dim rowsCount&, nextRow&, col&
    Dim strContribDesc$, strPlanName$
    
    Set wksSource = Worksheets("SOURCE SAMPLE")
    Set wksDest = Worksheets("FINAL")
    Set rngTable = wksSource.Range("A1").CurrentRegion
    With rngTable: Set rngData = .Offset(1).Resize(.Rows.Count - 1): End With
    nextRow = 2: rowsCount = rngData.Rows.Count
    
    With wksDest
        .[A1].CurrentRegion.Offset(1).EntireRow.Delete '//Delete old data
        For col = 5 To 10
            strContribDesc = vbNullString: strPlanName = vbNullString
            Call SetTags(rngTable.Rows(1).Cells(col), strContribDesc, strPlanName)
            .Cells(nextRow, "A").Resize(rowsCount).Value = rngData.Columns(3).Value   '//EmployeeIdentifier
            .Cells(nextRow, "B").Resize(rowsCount).Value = rngData.Columns(1).Value   '//ContributionDate
            .Cells(nextRow, "C").Resize(rowsCount).Value = strContribDesc             '//ContributionDescription
            .Cells(nextRow, "D").Resize(rowsCount).Value = rngData.Columns(col).Value '//ContributionAmount
            .Cells(nextRow, "E").Resize(rowsCount).Value = strPlanName                '//PlanName
            .Cells(nextRow, "F").Resize(rowsCount).Value = "Current"                  '//PriorTaxYear
            nextRow = nextRow + rowsCount
        Next
    End With
    
    MsgBox "Well done!", vbInformation

End Sub

Private Sub SetTags(strHeader$, ByRef strContribDesc$, ByRef strPlanName$)
    Select Case strHeader
        Case "Pre_Tax_FSA_Dependent_care(DR1)"
            strContribDesc = "Payroll"
            strPlanName = "Dependent Care FSA"
        Case "Pre_Tax_FSA_Medical(DR1)"
            strContribDesc = "Payroll"
            strPlanName = "Medical FSA"
        Case "Pre_Tax_HSA_Employee(DR1)"
            strContribDesc = "Payroll"
            strPlanName = "Health Savings Plan"
        Case "Pre_Tax_HSA_Employer(DR1)"
            strContribDesc = "Employer"
            strPlanName = "Health Savings Plan"
        Case "Parking_Benefit(DR1)"
            strContribDesc = "Payroll"
            strPlanName = "Parking"
        Case "Commuter_Benefit(DR1)"
            strContribDesc = "Payroll"
            strPlanName = "Parking"
    End Select
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM