我正在尝试编写一个循环遍历排序表的宏,为它找到的每个财政年度创建模板副本,然后将排序表中的数据复制并粘贴到相应工作表上的相应单元格中。
我有一个子程序,它循环遍历数据并为每个财政年度创建工作表的副本。这很好用。我已经编写了循环排序表并返回我需要的变量,以便将付款数据粘贴到正确的工作表:纳税年度(这就是我命名工作表的内容)、指示目标列的财务月份和费用类型决定目标列,现金是我要复制和粘贴的值。复制和粘贴的代码几乎适用于我使用的所有日期。
函数 FY(返回财政年度)和 FM(返回财政月份)来自日期格式为 DD.MM.YYYY 的字符串。 我使用这些函数创建变量,为我想要粘贴调用值的位置提供坐标。代码:
Worksheets(TY).Cells(16, Cashcol).End(xlUp).Offset(1, 0).Value = Cash
TY= 年(这是我用来命名工作表的名称。 16 是我要复制到的范围中的最后一行。 Cashcol是我想要的专栏 xlUp 是一个常量编辑,因为当我第一次问这个问题时我不知道这一点
这适用于 5 月 5 日至 4 月 31 日之间的所有日期。 (月份 1 到 12 的大部分时间) 我遇到的问题是,由于纳税年度为 4 月 6 日至 4 月 5 日,因此 5 月 1 日至 5 月 4 日之间的一段时间属于上一个纳税年度。
这意味着 FM 返回月份为 0。 为了解决这个问题,我添加了以下行:
If Cashmonth = 0 Then Backyear = True
If Backyear = True Then TY = FY(EDP) - 1
If Backyear = True Then Cashcol = 51
这不知何故破坏了我的代码。 例如。 2023 年 5 月 3 日是 2022 年纳税年度的第 12 个月。我想要转到名为 2022 的工作表第 51 列第 16 行上方的第一个空单元格。
但是当我运行代码时,我收到“下标超出范围错误”,当 Backyear=false 时不会发生这种情况。
调试显示所有其他变量都是正确的(TY = 2022,Cashcol = 51,Cash =正确的金额)是xlUp返回-4162 *我现在知道这是正确的*
我不知道出了什么问题,也不知道我是如何在 5 月份的 5 天之外破解有效代码的。
完整代码的稍微编辑版本:
Option Explicit
Public Function FY(Fdate As String) As String
Dim arDMY As Variant
arDMY = Split(Fdate, ".")
If arDMY(1) >= 5 Then
FY = CStr(arDMY(2) + 1)
Else
FY = CStr(arDMY(2))
End If
End Function
Public Function FM(Fdate As String) As String
Dim arDMY As Variant
Dim TM As String
arDMY = Split(Fdate, ".")
If arDMY(1) >= 5 Then
TM = CStr(arDMY(1) - 4)
Else
TM = CStr(arDMY(1) + 8)
End If
If arDMY(0) >= 5 Then
FM = TM
Else
FM = TM - 1
End If
End Function
Sub Move_stuff()
Dim cell As Range
Dim Todate As Range
Dim Charge As Range
Dim Duedate As Range
Dim Sort_Table As Worksheet
Dim Template As Worksheet
Dim C As Variant
Dim D As Variant
Dim Targ As Range
Dim Ddate As Range
Dim Charge_Amount As Range
Dim Targdate As String
Dim EDP As String
Dim Cash As Double
Dim Backyear As Boolean
Dim Pos As Variant
Dim Cashmonth As String
Dim TY As Variant
Dim EDPM As Variant
Dim Cashcol As Long
Dim Year As Variant
Dim Month As Variant
Dim shtcount As Long
Set Todate = Range("Sorttable[To Date]")
Set Charge = Range("Sorttable[Charge Type]")
Set Duedate = Range("Sorttable[Due Date]")
Sheets("Sort_Table").Select 'select worksheet
For Each cell In Charge 'loop to find charge type
If Not IsEmpty(cell) Then
C = cell.Value
D = CType(C)
Set Targ = cell.Offset(, 3)
Set Ddate = cell.Offset(, 2)
Set Charge_Amount = cell.Offset(, 4)
Targdate = Targ.Value
EDP = Ddate.Value
Cash = Charge_Amount.Value
Backyear = False
If D = "Cash" Then
Cashmonth = FM(EDP)
TY = FY(EDP)
EDPM = FM(EDP) + (FM(EDP) - 1)
Cashcol = 28 + EDPM
If Cashmonth = 0 Then Backyear = True
If Backyear = True Then TY = FY(EDP) - 1
If Backyear = True Then Cashcol = 51
Worksheets(TY).Cells(16, Cashcol).End(xlUp).Offset(1, 0).Value = Cash
Worksheets(TY).Cells(16, Cashcol - 1).End(xlUp).Offset(1, 0).Value = EDP
‘Elseif
‘Another bit of unrelated code that works
‘Elseif
‘Another bit of unrelated code that works
‘Elseif
‘Another bit of unrelated code that works
‘Else
‘Another bit of unrelated code that works
Worksheets("Sort_Table").Select
End if
End if
Next Cell
End Sub
只需将 TY 定义为 String 而不是 Variant。蒂姆解释了发生了什么事。
TY As String
TY = FY(EDP) - 1 ' TY will stay String and accept a new value which will be converted to String