有没有办法将这三行组合得更短?我对不同的列索引多次使用这些行,因此有很多重复。我想知道是否有更好的方法来实现这种方法。对于sysnum
,它是在我没有包含的代码中定义的,因为它看起来不相关,但它是一个数字文本字符串。
这是我的代码:
Dim lastrow As Long, sysnum as String
lastrow = wb.Worksheets(sysnum).Cells(Rows.Count, 1).End(xlUp).row
wb.Worksheets(sysnum).Rows(lastrow + 1).Insert
wb.Worksheets(sysnum).Cells(lastrow + 1, 2).Value = sysnum
wb.Worksheets(sysnum).Cells(lastrow + 1, 2).Font.Bold = True
wb.Worksheets(sysnum).Cells(lastrow + 1, 3).Value = "Passed"
wb.Worksheets(sysnum).Cells(lastrow + 1, 3).Font.Bold = True
wb.Worksheets(sysnum).Cells(lastrow + 1, 3).Interior.Color = vbGreen
End If
最佳答案
创建重复代码的方法
使用
Sub CallSysNumInsert()
' These two are already declared somewhere in your code!
' They are here only for the code to be able to compile.
Dim wb As Workbook
Dim sysnum As String
' Call the method.
SysNumInsert wb, sysnum
End Sub
方法
- 适本地重命名该方法。
Sub SysNumInsert(ByVal wb As Workbook, ByVal WorksheetName As String)
With wb.Worksheets(WorksheetName)
Dim dRow As Long: dRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Rows(dRow).Insert
With .Cells(dRow, "B")
.Value = WorksheetName
.Font.Bold = True
End With
With .Cells(dRow, "C")
.Value = "Passed"
.Font.Bold = True
.Interior.Color = vbGreen
End With
End With
End Sub
关于excel - 合并单元格文本详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74240692/