【发布时间】:2019-04-17 05:22:51
【问题描述】:
我对房子周围的 vba 项目很着迷,并帮助我的妻子将她的报告提升到一个新的水平。我很难将我的想法归结为 VBA。如果有人感受到我的痛苦,请阐明可以帮助我克服这个困难的实际脚本。摘要可能正在使用 InStr 比较某些文本的单元格值,如果不存在则将附加字符添加到右端。我可以追加并运行循环的一个周期,但对尝试编写我正在考虑的逻辑感到困惑。
报告的小背景:一行等于一项预订。在该行中有一个标有“Nights”的列。此列会针对超过“1”晚的任何预订进行过滤。示例:可能是 3 晚、6 晚和 10 晚都没有关系。我有一个宏,可以对这些预订进行排序,并将一个预订分成多行,总计“Nights”列中的数值。基本上,复制并插入彼此相邻的行。虽然仍应用此过滤(仅适用于 SpecialVisibleCells)。现在我有另一列标有“ResNumber”。拆分为 3、6 或 10 行时,“ResNumber”列是相同的数字。我的任务是遍历这个“ResNumber”列并为第一行附加一个“-1”。 “-2”表示第二个保留 “-3”表示第三个,可能还有第四个“-4” 直到该保留组复制的最后一行。然后循环(循环)在下一组或下一行块上再次开始。相同的程序。
Dim lnrow 为整数 暗淡为字符串 Dim rownuml As Integer '行检查器 Dim colnuml As String '列检查器 Dim count As Integer Dim total As String '预订的“Nights”列的值 Offset(,17) Dim startnum As Integer '计数器的起始编号 Dim actcell As String 'Activecell 起始编号 = 1 带嘘 llrow = .Cells(.Rows.count, 2).End(xlUp).row If llrow = "" Then Exit Sub .Cells(2, 2).Resize(llrow - 1).SpecialCells(xlCellTypeVisible).Select
For lnrow = 2 To llrow
rownuml = ActiveCell.row
colnuml = ActiveCell.Column
total = ActiveCell.offset(, 17).Value
For count = 1 To total
rownuml = ActiveCell.row
colnuml = ActiveCell.Column
actcell = ActiveCell.Value
'Compares row 1 and checks resNumber value for "-1" if none exist it appends.
If InStr(ActiveCell.Value, "-1") = 0 Then
ActiveCell.Value = ActiveCell.Value & "-1"
Else
GoTo nexrow
End If
'Compares row 2 and checks resNumber value of above cell.
If InStr(ActiveCell.offset(-1, 0).Value, "-1") = 0 Then
Resume Next
If InStr(ActiveCell.Value, "-2") = 0 Then
ActiveCell.Value = ActiveCell.Value & "-2"
GoTo nexrow
End If
'跳出循环nexrow 'ActiveCell 向下移动一排。 ActiveCell.offset(1, 0).SpecialCells(xlCellTypeVisible).Select rownuml = ActiveCell.row '只检查行号 colnuml = ActiveCell.Column '只检查列号
'因为第一个保留已经在数据库中 startnum 从 #1 开始。计数器 起始编号 = 起始编号 + 计数 下一个计数 下一个 结束于
【问题讨论】:
标签: excel loops if-statement