excel - 合并单元格文本详细信息

标签 excel vba

有没有办法将这三行组合得更短?我对不同的列索引多次使用这些行,因此有很多重复。我想知道是否有更好的方法来实现这种方法。对于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/

相关文章:

vba - 在VBA排序中设置范围

vba - Excel VBA 函数根据函数名称生成#REF?

vba - 需要在 Excel 中刷新数据时运行 VBA 宏

excel - 打开 CSV 文件而不将文本转换为公式

excel - 将用户窗体与 Excel 中的单元格对齐

windows - 使用 VBA 从具有 SAML 身份验证的网站下载文件

excel - 突出显示连续正数的最大数量

vba - 如何在Excel VBA中锁定行之间

c# - 在读/写模式下从 SharePoint 站点以 C# 编程方式打开 xls 电子表格

excel - 来自动态范围的 VBA 范围值