excel - 如何修复 VBA 复制和过去公式错误
问题描述
我正在尝试使用以下 VBA 代码将下面的公式插入到列的其余部分。它会将巢单元向下更改,但随后我得到一个“#NAME?” 其余单元格的错误。帮助,我做错了什么?
Private Sub CommandButton2_Click()
Dim LstRow As Long
With Sheets("sheet1")
Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("F6:F" & Lastrow).Formula = "=IF(F5=F4,”P=”&ROW(F7)/2,F5)"
.Range("F6:F" & Lastrow).Copy
.Range("F6:F" & Lastrow).PasteSpecial xlValues
Application.CutCopyMode = False
End With
End Sub
解决方案
使用R1C1
符号。更快,更容易实现。一切都与R1C1
编写公式的单元格相关。
Private Sub CommandButton2_Click()
Dim LstRow As Long
With Sheets("sheet1")
Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
With .Range("F6:F" & Lastrow)
.FormulaR1C1 = "=IF(R[-1]C=R[-2]C,""P=""&R[1]/2,R[-1]C)"
.Value = .Value
End With
Application.CutCopyMode = False
End With
End Sub
推荐阅读
- blazor - Blazor:直接将类绑定到输入元素
- performance - 如何对结构进行 GROUP BY 操作
- ios - App Store Connect 操作错误。无效的图像路径。未找到图片
- python - 返回 None 类型列表而不是什么的函数
- node.js - aws-api-gateway-client API POST 方法问题
- directory - 在 Markdown 语法中表示目录和文件结构时,哪种语言适合防护代码块?
- powershell - 如何安排 powershell 脚本从 00:00:30 到 11:59:59 运行,然后在第二天 00:00:30 再次运行
- c++ - 当输入有大量数据时,for循环进行了超长时间(20+分钟)迭代
- node.js - 我有一个来自 api 的数据我如何在反应中以树的形式显示它
- sql-server - 在 SQL Server 中更改排序规则