excel - 引用一个数字生成单元格中 "(x,y)"数据的个数

标签 excel vba

(例如:1=(x1,y1), 3=(x1,y1,x2,y2,x3,y3)

我如何删除如下所示的不必要的“(,)”并引用可靠性失败标题下的数字放置可靠性失败的x,y坐标的位置数?
例如:设备 WLR8~LW~VBD~MNW 中的可靠性故障计数 =2 应该给我该故障计数在与 columnX 的设备相同的行的位置。无论如何,请忽略我图片中 V 和 W 列下的数据。

当前输出基于我的代码
enter image description here

我真正想要的
enter image description here

目前的问题
enter image description here

当前问题2
enter image description here
它应该在哪里
enter image description here

Dim output As Variant
Dim outputrow As Integer
output = ""
outputrow = 0
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets(1)

Dim ia As Long
Dim lastrow2 As Long
lastrow2 = ws1.Range("U2:U" & ws1.Rows.Count).Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row

If ws1.Range("U1") = "Reliability Fail" Then
 For ia = 2 To lastrow2
    If ws1.Cells(ia, "U").Value = 0 Then
    output = output & "(" & ws1.Cells(ia, "Y").Value & "," & ws1.Cells(ia, "Z").Value & "),"
    ElseIf output = "(,)," Then 'if there are no x and y values in Y and Z column stop showing "(,),"
    output = ""
    End If
    If ws1.Cells(ia, "U").Value > 0 Then
    ws1.Cells(ia, "U").Offset(0, 3).Value = Left(output, Len(output) - 1) 'extract the x and y values obtain in (x,y) format
                    'if there is "value" under reliability fails(column U), put the x y position at the same row as the "value" at column X
    End If
    Next
End If

最佳答案

我建议使用内部循环,这样一开始就不会添加额外的括号

Option Explicit

Sub test()


Dim output As Variant
Dim outputrow As Integer
Dim valueCount As Long, ib As Long
output = ""
outputrow = 0
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets(1)

Dim ia As Long
Dim lastrow2 As Long
lastrow2 = ws1.Range("U2:U" & ws1.Rows.Count).Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row

If ws1.Range("U1") = "Reliability Fail" Then

' Outer loop over all rows
 For ia = 2 To lastrow2
  valueCount = ws1.Cells(ia, "U").Value
  output = ""

  ' Inner loop to process repeated rows
  For ib = 1 To valueCount
    output = output & "(" & ws1.Cells(ia + ib - 1, "Y").Value & "," & ws1.Cells(ia + ib - 1, "Z").Value & ")"
    If ib < valueCount Then output = output & ","
   Next ib
   ws1.Cells(ia, "U").Offset(0, 3).Value = output
 Next ia
End If

End Sub

编辑

以下是根据 OP 后面的示例修改后的代码:
Option Explicit

Sub test()

Dim output As Variant
Dim outputrow As Integer
Dim valueCount As Long, ib As Long, rowPointer As Long
output = ""
outputrow = 0
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets(1)

Dim ia As Long
Dim lastrow2 As Long
lastrow2 = ws1.Range("U2:U" & ws1.Rows.Count).Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row

If ws1.Range("U1") = "Reliability Fail" Then

rowPointer = 2
' Outer loop over all rows
 For ia = 2 To lastrow2
  valueCount = ws1.Cells(ia, "U").Value
  output = ""

  ' Inner loop to process repeated rows
  For ib = 1 To valueCount
    output = output & "(" & ws1.Cells(rowPointer, "Y").Value & "," & ws1.Cells(rowPointer, "Z").Value & ")"
    If ib < valueCount Then output = output & ","
    rowPointer = rowPointer + 1
  Next ib
  ws1.Cells(ia, "U").Offset(0, 3).Value = output
 Next ia
End If

End Sub

enter image description here

关于excel - 引用一个数字生成单元格中 "(x,y)"数据的个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60350499/

相关文章:

python - 在从 Excel 范围填充的嵌套元组上使用 join()

vba - 检查外部封闭工作簿中是否存在工作表

c# - 为 VBA 添加引用对话框公开时,为 .NET (COM) tlb 库选择自定义引用名称

excel - 我正在尝试更改多个工作簿的事件过程?

excel - 使用 =VLOOKUP 显示单元格地址

vba - ThisWorkbook 不工作

excel - 跨工作簿共享 VBA 模块

excel - 从一个单元格中查找值,从该单元格附近的单元格复制值并将其粘贴到另一张表

vba - 在单元格更改时循环遍历一系列单元格,以将序列中的下一个数字显示为单元格的新值

vba - Excel VBA - WorkbookBeforeSave 事件执行