EXCEL VBA宏:使用Range可以简化这个吗?

时间:2015-05-11 10:52:00

标签: excel vba excel-vba

Sub NewPortName ()
      If ThisWorkbook.Sheets("PAR Form").Cells(2, 7).Value = "RJ45" Then
      ThisWorkbook.Sheets("PAR_import").Cells(16, 3).Value = "PCI-" +      ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(2, 13), 7)

      ElseIf ThisWorkbook.Sheets("PAR Form").Cells(2, 7).Value = "LC-LC" Then
      ThisWorkbook.Sheets("PAR_import").Cells(16, 3).Value = "PFI-" + ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(2, 13), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(2, 36) + " to " + Left(ThisWorkbook.Sheets("PAR Form").Cells(2, 14), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(2, 38)
      End If
      If ThisWorkbook.Sheets("PAR Form").Cells(3, 7).Value = "RJ45" Then
      ThisWorkbook.Sheets("PAR_import").Cells(17, 3).Value = "PCI-" + ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(3, 13), 7)

      ElseIf ThisWorkbook.Sheets("PAR Form").Cells(3, 7).Value = "LC-LC" Then
      ThisWorkbook.Sheets("PAR_import").Cells(17, 3).Value = "PFI-" + ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(3, 13), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(3, 36) + " to " + Left(ThisWorkbook.Sheets("PAR Form").Cells(3, 14), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(3, 38)
      End If

End sub

由于多个范围内输出的性质,我目前正逐行修改单个单元格。

我想知道是否可以使用范围简化这一点,7个中有两个范围是(M2:M100)(N2:N100)

我将需要重复此代码并单独更改单元格超过700次以反映700个单独的单元格,如果我不能使这个抽象

2 个答案:

答案 0 :(得分:0)

你总是可以沿着下面的界线尝试循环:

Sub ing()
For i = 2 To 100

  Select Case ThisWorkbook.Sheets("PAR Form").Cells(i, 7).Value
  Case "RJ45"
    ThisWorkbook.Sheets("PAR_import").Cells(i + 14, 3).Value = "PCI-" + ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(i, 13), 7)
  Case "LC-LC"
    ThisWorkbook.Sheets("PAR_import").Cells(i + 14, 3).Value = "PFI-" + ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(i, 13), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(i, 36) + " to " + Left(ThisWorkbook.Sheets("PAR Form").Cells(i, 14), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(i, 38)
  End Select
Next i
End Sub

答案 1 :(得分:0)

检查出来,

config