【发布时间】:2019-08-12 09:13:56
【问题描述】:
我设置了一个带有 match 函数的循环,因此它会检查是否存在匹配,然后返回结果并重复此操作定义的次数。我也设置了它,所以如果有错误,这意味着如果没有匹配,它会跳到下一个循环。但是,当没有找到匹配项时,它会在输入下一个匹配项之前留下一个空行。这就是我要避免的。
我的代码目前的工作方式是这样的:
ws1 具有多列和多行数据。 A 列中每一行的第一个单元格是标题。标题来自固定选择(它是一个下拉菜单),由 ws2 上的列表确定
ws2 有标题列表,在 LastRow 之前是 h3
ws3 点击按钮后,它会匹配任何与 variable_condition 相关的结果,如果找不到匹配则进入下一个循环,然后从第 4 行开始在多行上打印它
在 ws3 上,它还在每一行插入一个形状,该形状被分配了一个宏(因此成为一个按钮)
实际发生的情况是,如果找不到匹配项,则会在 I 列中出现具有此形状的空行。
我正在尝试使它没有一个带有按钮的空白行,而是插入下一个循环结果
我的代码如下:
Sub CardsCollection()
Set ws1 = Sheets("Database")
Set ws2 = Sheets("Insert")
Set ws3 = Sheets("Sheet1")
Dim myCell As Range
Dim LastRow As Long
LastRow = ws2.Cells(ws2.Rows.Count, "H").End(xlUp).Row
Debug.Print LastRow
Dim test_string As String
test_string = "H" & LastRow
Dim test_range As Range
Set test_range = ws2.Range(test_string)
variable_condition = Range("E2")
NxtRw = 4
On Error Resume Next
For Each myCell In ws2.Range("H3" & ":" & test_string)
row_num2 = Evaluate("MATCH(1,('" & ws1.Name & "'!A:A=""" & myCell & """)*('" & ws1.Name & "'!F:F=""" & variable_condition & """),0)")
ws3.Range("A" & NxtRw).EntireRow.Value = ws1.Range("A" & row_num2).EntireRow.Value
Dim button_cell As String
button_cell = "I" & NxtRw
Dim bc_range As Range
Set bc_range = Range(button_cell)
Dim rect1 As Shape
Dim rngToCheck As Range
Dim clLeft As Double
Dim clTop As Double
Dim clWidth As Double
Dim clHeight As Double
Dim shpRec As Shape
Set cl = Range(button_cell)
With shpRec
clLeft = cl.Left
clTop = cl.Top
clWidth = cl.Width - 5
clHeight = cl.Height - 5
End With
Set shpRec = ActiveSheet.Shapes.AddShape(msoShapeRectangle, clLeft, clTop, clWidth, clHeight)
With shpRec
.Fill.ForeColor.RGB = RGB(242, 177, 135)
.Line.Visible = False 'True
.Line.ForeColor.RGB = RGB(255, 255, 255)
.TextFrame.Characters.Text = "INSERT"
.TextFrame.HorizontalAlignment = xlHAlignCenter
.TextFrame.VerticalAlignment = xlVAlignCenter
.TextFrame.Characters.Font.Size = 24
.TextFrame.Characters.Font.Name = "SF Pro Display Black"
End With
NxtRw = NxtRw + 1
Next
End Sub
任何帮助将不胜感激!谢谢
编辑:更新代码
Sub CardsCollection()
Call last_used_sort
Set ws1 = Sheets("Database")
Set ws2 = Sheets("Insert")
Set ws3 = Sheets("Sheet1")
Dim myCell As Range
Dim LastRow As Long
LastRow = ws2.Cells(ws2.Rows.Count, "H").End(xlUp).Row
Debug.Print LastRow
Dim test_string As String
test_string = "H" & LastRow
Dim test_range As Range
Set test_range = ws2.Range(test_string)
Dim row_num2 As Long
variable_condition = Range("E2")
NxtRw = 4
For Each myCell In ws2.Range("H3" & ":" & test_string)
row_num2 = -1
On Error Resume Next
row_num2 = Evaluate("MATCH(1,('" & ws1.Name & "'!A:A=""" & myCell & """)*('" & ws1.Name & "'!F:F=""" & variable_condition & """),0)")
On Error GoTo 0
If row_num2 <> -1 Then
ws3.Range("A" & NxtRw).EntireRow.Value = ws1.Range("A" & row_num2).EntireRow.Value
Dim button_cell As String
button_cell = "I" & NxtRw
Dim bc_range As Range
Set bc_range = Range(button_cell)
Dim rect1 As Shape
Dim rngToCheck As Range
Dim clLeft As Double
Dim clTop As Double
Dim clWidth As Double
Dim clHeight As Double
Dim shpRec As Shape
Set cl = Range(button_cell)
Dim button_cell As String
button_cell = "I" & NxtRw
Dim bc_range As Range
Set bc_range = Range(button_cell)
Dim rect1 As Shape
Dim rngToCheck As Range
Dim clLeft As Double
Dim clTop As Double
Dim clWidth As Double
Dim clHeight As Double
Dim shpRec As Shape
Set cl = Range(button_cell)
Set shpRec = ActiveSheet.Shapes.AddShape(msoShapeRectangle, clLeft, clTop, clWidth, clHeight)
With shpRec
.Fill.ForeColor.RGB = RGB(242, 177, 135)
.Line.Visible = False 'True
.Line.ForeColor.RGB = RGB(255, 255, 255)
.TextFrame.Characters.Text = "INSERT"
.TextFrame.HorizontalAlignment = xlHAlignCenter
.TextFrame.VerticalAlignment = xlVAlignCenter
.TextFrame.Characters.Font.Size = 24
.TextFrame.Characters.Font.Name = "SF Pro Display Black"
End With
NxtRw = NxtRw + 1
End If
Next
End Sub
【问题讨论】:
-
所以基本上如果
rownum2什么都不是,那么代码应该直接跳到下一次迭代而不执行任何进一步的操作? -
On error resume next 将跳过错误行,而不是整个循环。你会想要这样的东西:
On Error GoTo NextLoop并将NextLoop:放在Next之前的行上 -
尝试将
NxtRw = NxtRw + 1更改为If IsNumeric(row_num2) Then NxtRw = NxtRw + 1。 -
@Mikku,它仍然会插入形状,但是你会得到两个相互重叠的形状,弄得一团糟。
-
是的@Luuklag .. 在这种情况下,您的方法将是最好的。