首页 文章

使用VBA将单元格值保留在excel中从一个单元格保留到另一个单元格

提问于
浏览
8

在excel中,我试图将文本从一个单元格复制到另一个单元格中的另一个单元格 . 源单元格包含格式化文本(粗体,下划线,不同颜色) . 但是当我使用VBA将文本复制到另一个单元格时,格式化将丢失 .

我知道这是因为excel只复制文本值 . 有没有办法可以从单元格中读取 HTML text (而不是纯文本)?

我用Google搜索了这个并没有得到任何答案 . 我知道如果我们使用复制和粘贴方法,我们可以复制格式 . 例如 .

Range("F10").Select
Selection.Copy
Range("I10").Select
ActiveSheet.Paste

But I want to do it without a copy and paste since my destination is a merged cell and not identically sized as my source cell. Is there an option available in excel VBA to do this?

编辑:我能够用以下代码解决它 .

Range("I11").Value = Range("I10").Value
For i = 1 To Range("I10").Characters.Count
    Range("I11").Characters(i, 1).Font.Bold = Range("I10").Characters(i, 1).Font.Bold
    Range("I11").Characters(i, 1).Font.Color = Range("I10").Characters(i, 1).Font.Color
    Range("I11").Characters(i, 1).Font.Italic = Range("I10").Characters(i, 1).Font.Italic
    Range("I11").Characters(i, 1).Font.Underline = Range("I10").Characters(i, 1).Font.Underline
    Range("I11").Characters(i, 1).Font.FontStyle = Range("I10").Characters(i, 1).Font.FontStyle
Next i

5 回答

  • 7

    要复制格式:

    Range("F10").Select
    Selection.Copy
    Range("I10:J10").Select ' note that we select the whole merged cell
    Selection.PasteSpecial Paste:=xlPasteFormats
    

    复制格式将破坏合并的单元格,因此您可以使用它将单元格重新组合在一起

    Range("I10:J10").Select
    Selection.Merge
    

    要复制单元格值而不复制任何其他内容(并且不使用复制/粘贴),您可以直接寻址单元格

    Range("I10").Value = Range("F10").Value
    

    其他属性(字体,颜色,etc)也可以通过以相同方式直接寻址范围对象属性来复制

  • 5

    使用Excel 2010?尝试

    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
  • 3

    我宁愿避免使用select

    With sheets("sheetname").range("I10") 
              .PasteSpecial Paste:=xlPasteValues, _
                      Operation:=xlNone, _
                      SkipBlanks:=False, _
                      Transpose:=False
              .PasteSpecial Paste:=xlPasteFormats, _
                      Operation:=xlNone, _
                      SkipBlanks:=False, _
                      Transpose:=False
              .font.color = sheets("sheetname").range("F10").font.color
          End With
          sheets("sheetname").range("I10:J10").merge
    
  • 1
    Sub CopyValueWithFormatting()
        Sheet1.Range("A1").Copy
        With Sheet2.Range("B1")
            .PasteSpecial xlPasteFormats
            .PasteSpecial xlPasteValues
        End With
    End Sub
    
  • 0

    Copying the Bold Text From one sheet to another sheet in excel By using VBScript '创建实例对象

    Set oXL = CreateObject("Excel.application")
    oXL.Visible = True
    
    Set oWB = oXL.Workbooks.Open("FilePath.xlsx")
    Set oSheet = oWB.Worksheets("Sheet1")         'Source Sheet in workbook
    Set oDestSheet = oWB.Worksheets("Sheet2")       'Destination sheet in workbook
    
    r = oSheet.usedrange.rows.Count
    c = oSheet.usedrange.columns.Count
    
    For i = 1 To r
        For j = 1 To c
            If oSheet.Cells(i,j).font.Bold = True Then
    
                oSheet.cells(i,j).copy
                oDestSheet.Cells(i,j).pastespecial
            End If
        Next
    Next
    
    oWB.Close
    oXL.Quit
    

相关问题