获取当前选取的格式信息(可选存放位置)

经验创意 · 71 次浏览
Ulookperfect... 创建于 2025-02-05 17:17

 

代码:Sub GetSelectedCellFormat()
    ' 定义变量
    Dim rng As Range
    Dim cell As Range
    Dim result As String
    Dim rgbColor As String
    Dim targetRange As Range
    
    ' 获取当前选定的区域
    Set rng = Selection
    
    ' 初始化结果字符串
    result = "选中的区域格式信息:" & vbCrLf
    
    ' 获取选定区域的第一个单元格的格式信息
    With rng.Cells(1, 1)
        result = result & "字体: " & .Font.Name & vbCrLf
        result = result & "字号: " & .Font.Size & vbCrLf
        result = result & "颜色: RGB(" & .Font.Color & ")" & vbCrLf
        result = result & "背景色: RGB(" & .Interior.Color & ")" & vbCrLf
        result = result & "框线类型: " & .Borders.LineStyle & vbCrLf
        
        ' 将框线颜色转换为RGB格式
        rgbColor = "RGB(" & .Borders.Color & ")"
        result = result & "框线颜色: " & rgbColor & vbCrLf
    End With
    
    ' 让用户选择放置结果的目标单元格
    Set targetRange = Application.InputBox(Prompt:="请选择放置结果的单元格:", _
                                          Title:="选择目标单元格", Type:=8)
    
    ' 检查用户是否选择了有效的范围
    If targetRange Is Nothing Then
        MsgBox "未选择有效范围,操作已取消。"
        Exit Sub
    End If
    
    ' 将结果放置在用户选定的单元格中
    targetRange.Value = result
End Sub

 


回复内容
暂无回复
回复主贴