gpt4 book ai didi

excel - VBA复制单元格值和格式

转载 作者:行者123 更新时间:2023-12-01 17:45:13 24 4
gpt4 key购买 nike

如何修改以下代码以便不仅复制值而且复制字体样式,例如大胆或不大胆。谢谢

Private Sub CommandButton1_Click()
Dim i As Integer
Dim a As Integer
a = 15
For i = 11 To 32
If Worksheets(1).Cells(i, 3) <> "" Then
Worksheets(2).Cells(a, 15) = Worksheets(1).Cells(i, 3).Value
Worksheets(2).Cells(a, 17) = Worksheets(1).Cells(i, 5).Value
Worksheets(2).Cells(a, 18) = Worksheets(1).Cells(i, 6).Value
Worksheets(2).Cells(a, 19) = Worksheets(1).Cells(i, 7).Value
Worksheets(2).Cells(a, 20) = Worksheets(1).Cells(i, 8).Value
Worksheets(2).Cells(a, 21) = Worksheets(1).Cells(i, 9).Value
a = a + 1
End If
Next i

最佳答案

您可以尝试使用复制/粘贴,而不是直接设置值,因此:

Worksheets(2).Cells(a, 15) = Worksheets(1).Cells(i, 3).Value

试试这个:

Worksheets(1).Cells(i, 3).Copy
Worksheets(2).Cells(a, 15).PasteSpecial Paste:=xlPasteFormats
Worksheets(2).Cells(a, 15).PasteSpecial Paste:=xlPasteValues

要将字体设置为粗体,您可以保留现有的作业并添加以下内容:

If Worksheets(1).Cells(i, 3).Font.Bold = True Then
Worksheets(2).Cells(a, 15).Font.Bold = True
End If

关于excel - VBA复制单元格值和格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25461314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com