我的 header 一直消失,而且没有固定到正确的行

My header keep disappearing and it not fix to the correct row

此页面应该显示所选政策 number/plan 的详细信息。就像不同的组件这样。但它根据保单编号而有所不同。有些可以有 1 个,有些可以有更多。我设法让 plan/component 根据每个策略编号的组件数量工作。但是我的funds invested header 和component header 不能显示保单号的值。

显示问题: 我尝试计算基金投资价值,但我的组件 header 将消失。如果不是,我的成分价值将与投资的资金一起放置 header。有什么问题吗?

代码:

Sub LinkName()

    Dim i As Long, k As Long, b As Long, c As Long, f As Long  'For loop variable
    Dim ShtUsedRange, ShtUsedRangeCol
    Dim rw As Variant
    Dim policynum As Variant
    Dim lookup_range As Range
    Dim box As Variant
    Dim counter As Long, DetailsCounter As Long, FundInfoCounter As Long, FundAllocationCounter As Long
    Dim a As Long, d As Long, e As Long, p As Long, m As Long

    ShtUsedRange = ActiveSheet.UsedRange.Rows.Count
    'Count the used rows in the Activesheet
    ShtUsedRangeCol = ActiveSheet.UsedRange.Columns.Count
    'Count the used Column in the Activesheet

    rw = ActiveCell.Row
    'Row number of the Selected Cell

    policynum = ActiveSheet.Cells(rw, 3).Value
    'policynum of the row selected

    Set lookup_range = ThisWorkbook.Sheets("PolicyComponents").Range("c4:iv30000")
    'set the range of the Policy details to search from

    box = Application.VLookup(policynum, lookup_range, 1, False)
    'to match the policy num to the policy details

    counter = ThisWorkbook.Sheets("PolicyComponents").UsedRange.Rows.Count
    'MsgBox counter

    k = 16
    Do While (ThisWorkbook.Sheets("Policy Viewer").Range("B" & k) <> "")
    ThisWorkbook.Sheets("Policy Viewer").Rows(k & ":" & k).Delete
    Loop
    'delete the previous set of components

    a = 16 'Start of Plan/Component
    d = 21 'Start of Funds Invested

    'Based On PolicyComponents Table
    For i = 4 To counter Step 1

        If ThisWorkbook.Sheets("PolicyComponents").Cells(i, 3).Value = box Then

            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 2).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 4).Value
            'Policy Components
            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 3).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 5).Value
            'Status
            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 4).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 6).Value
            'Life Assured
            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 5).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 7).Value
            'Benefit Amt/ Sum Assured
            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 6).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 8).Value
            'Premium
            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 7).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 9).Value
            'Single Premium
            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 8).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 10).Value
            'Premium Cessation Date
            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 9).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 11).Value
            'Risk Commencement Date
            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 10).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 12).Value
            'Risk Cessation Date
            ThisWorkbook.Sheets("Policy Viewer").Cells(a, 11).Value = ThisWorkbook.Sheets("PolicyComponents").Cells(i, 13).Value
            'Est. Total Paid

            ThisWorkbook.Sheets("Policy Viewer").Rows(a + 1 & ":" & a + 1).Insert
            'insert blank lines
            a = a + 1


    End If

    Next i
    'End of Plan/Component Table

    'For Policy Details
    DetailsCounter = ThisWorkbook.Sheets("PolicyDetails").UsedRange.Rows.Count

    For b = 4 To DetailsCounter Step 1

    If ThisWorkbook.Sheets("PolicyDetails").Cells(b, 3).Value = box Then
    'POLICY VIEWER
            ThisWorkbook.Sheets("Policy Viewer").Cells(2, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 1).Value
            'Policy Owner Name
            ThisWorkbook.Sheets("Policy Viewer").Cells(3, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 2).Value
            'Plan Type
            ThisWorkbook.Sheets("Policy Viewer").Cells(4, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 3).Value
            'Policy Num***
            ThisWorkbook.Sheets("Policy Viewer").Cells(5, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 4).Value
            'Policy Status
            ThisWorkbook.Sheets("Policy Viewer").Cells(6, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 5).Value
            'Payment Method
            ThisWorkbook.Sheets("Policy Viewer").Cells(7, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 6).Value
            'Paid-To-Date
            ThisWorkbook.Sheets("Policy Viewer").Cells(8, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 7).Value
            'Frequency
            ThisWorkbook.Sheets("Policy Viewer").Cells(9, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 8).Value
            'Premium $
            ThisWorkbook.Sheets("Policy Viewer").Cells(10, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 9).Value
            'Amt In Suspense
            ThisWorkbook.Sheets("Policy Viewer").Cells(11, 3).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 16).Value
            'Servicing Agent
            ThisWorkbook.Sheets("Policy Viewer").Cells(7, 9).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 10).Value
            'Life Assured
            ThisWorkbook.Sheets("Policy Viewer").Cells(8, 9).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 11).Value
            'Joint Life Assured
            ThisWorkbook.Sheets("Policy Viewer").Cells(9, 9).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 12).Value
            '2nd Joint Life Assured
            ThisWorkbook.Sheets("Policy Viewer").Cells(10, 9).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 13).Value
            'Joint PolicyOwner
            ThisWorkbook.Sheets("Policy Viewer").Cells(11, 9).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 14).Value
            'Assignees
            ThisWorkbook.Sheets("Policy Viewer").Cells(12, 9).Value = ThisWorkbook.Sheets("PolicyDetails").Cells(b, 15).Value
            'Trustees
            ThisWorkbook.Sheets("Policy Viewer").Cells(2, 9).Value = ThisWorkbook.Sheets("PolicyValues").Cells(b, 4).Value
            'Est ILP Surrender Value
            ThisWorkbook.Sheets("Policy Viewer").Cells(3, 9).Value = ThisWorkbook.Sheets("PolicyValues").Cells(b, 5).Value
            'Gross Surrender Value
            ThisWorkbook.Sheets("Policy Viewer").Cells(4, 9).Value = ThisWorkbook.Sheets("PolicyValues").Cells(b, 6).Value
            'Gross Cash Bonus
            ThisWorkbook.Sheets("Policy Viewer").Cells(5, 9).Value = ThisWorkbook.Sheets("PolicyValues").Cells(b, 7).Value
            'Total Cashback
            ThisWorkbook.Sheets("Policy Viewer").Cells(6, 9).Value = ThisWorkbook.Sheets("PolicyValues").Cells(b, 8).Value
            'Est Total Paid
    End If
    Next b
    'End of Policy Details (B2:I12)
    =====================================================================
    xxxxxxxxxxxxxxxxxxxxxI think problem lies herexxxxxxxxxxxxxxxxxxxxxxx
    =====================================================================
    p = 21
    'For Funds Invested table
    Do While (ThisWorkbook.Sheets("Policy Viewer").Range("B" & p) <> "")
    ThisWorkbook.Sheets("Policy Viewer").Rows(p & ":" & p).Delete
    Loop

    FundInfoCounter = ThisWorkbook.Sheets("Fund Information").UsedRange.Rows.Count

    For c = 4 To FundInfoCounter Step 1

    If ThisWorkbook.Sheets("Fund Information").Cells(c, 3).Value = box Then

    ThisWorkbook.Sheets("Policy Viewer").Cells(d, 2).Value = ThisWorkbook.Sheets("Fund Information").Cells(c, 4).Value
    'Funds Invested
    ThisWorkbook.Sheets("Policy Viewer").Cells(d, 3).Value = ThisWorkbook.Sheets("Fund Information").Cells(c, 5).Value
    'Unit Balance
    ThisWorkbook.Sheets("Policy Viewer").Cells(d, 4).Value = ThisWorkbook.Sheets("Fund Information").Cells(c, 6).Value
    'Bid Price
    ThisWorkbook.Sheets("Policy Viewer").Cells(d, 5).Value = ThisWorkbook.Sheets("Fund Information").Cells(c, 7).Value
    'Unit Value
    ThisWorkbook.Sheets("Policy Viewer").Cells(d, 6).Value = ThisWorkbook.Sheets("Fund Information").Cells(c, 8).Value
    'Unit Value as at

    ThisWorkbook.Sheets("Policy Viewer").Rows(d + 1 & ":" & d + 1).Insert
    'insert blank lines
    d = d + 1

    End If
    Next c
    'End of Fund Invested Table

    m = p + 3
    e = 24 ' start of component 
    'For Components Table
    Do While (ThisWorkbook.Sheets("Policy Viewer").Range("B" & m) <> "")
    ThisWorkbook.Sheets("Policy Viewer").Rows(m & ":" & m).Delete
    Loop

    FundAllocationCounter = ThisWorkbook.Sheets("Fund Allocation").UsedRange.Rows.Count

    For f = 4 To FundAllocationCounter Step 1

    If ThisWorkbook.Sheets("Fund Allocation").Cells(f, 3).Value = box Then

    ThisWorkbook.Sheets("Policy Viewer").Cells(e, 2).Value = ThisWorkbook.Sheets("Fund Allocation").Cells(f, 4).Value
    'Component
    ThisWorkbook.Sheets("Policy Viewer").Cells(e, 3).Value = ThisWorkbook.Sheets("Fund Allocation").Cells(f, 5).Value
    'Fund Invested
    ThisWorkbook.Sheets("Policy Viewer").Cells(e, 6).Value = ThisWorkbook.Sheets("Fund Allocation").Cells(f, 6).Value
    'Percentage

    ThisWorkbook.Sheets("Policy Viewer").Rows(e + 1 & ":" & e + 1).Insert
    'insert blank lines


    End If
    Next f
    'End of Components Table

    =====================================================================
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    =====================================================================

    End Sub

我不确定这个问题是否属于代码审查部分,但我会试一试。

您的问题是您忘记了上次使用的行号。您开始添加行然后删除行而没有.....我不知道您在想什么:D

检查这一行:

p = 21
'For Funds Invested table
Do While (ThisWorkbook.Sheets("Policy Viewer").Range("B" & p) <> "")
    ThisWorkbook.Sheets("Policy Viewer").Rows(p & ":" & p).Delete
Loop

你在盲目地告诉你投资的资金 table 从第 21 行开始,不管你的计划摘要有多大,然后开始删除行!!!!。您的计划摘要最终可能有 100 行。 >> "you need to know/take a note of your last row"

检查这段代码(同样的问题):

只要下一个 sheet.

中有足够的可用数据,您就会在投资资金部分插入新行
    ThisWorkbook.Sheets("Policy Viewer").Cells(d, 6).Value = ThisWorkbook.Sheets("Fund Information").Cells(c, 8).Value
'Unit Value as at

ThisWorkbook.Sheets("Policy Viewer").Rows(d + 1 & ":" & d + 1).Insert
'insert blank lines
d = d + 1

End If
Next c
'End of Fund Invested Table

然后你告诉 vba 你的 "component" 部分从第 24 行开始,不管你在 "funds invested section" 中插入了多少行 绝对不行

m = p + 3
e = 24 ' start of component 
'For Components Table
Do While (ThisWorkbook.Sheets("Policy Viewer").Range("B" & m) <> "")
ThisWorkbook.Sheets("Policy Viewer").Rows(m & ":" & m).Delete
Loop

m 和 e 应该至少大于或等于 d,因为 d 是你 using/inserting 的最后一个行号。它不能是 p 因为 p 仍然是 21.

你有三个选择。

  • 为您的 tables/Sections 创建命名范围。获取行号并在范围后插入行。这样您就不会使用盲行号,而是动态获取部分。
  • 或知道您最后使用了哪一行。不要将所有的 A~Z 字母用作可变和松散的轨道。只需保留一个变量作为行号,然后您就知道您在哪里以及在哪一行 deleting/inserting.
  • 学习调试使用逐步行 (f8) 并检查变量的值。检查哪些行受到影响。可能会帮助你理解更多的逻辑