vba - 将公式放入单元格,错误地插入值而不是公式?
问题描述
我在此工作簿中的所有其他工作表上都有此代码,但在 Sheet1 上,公式计算值被插入,这使得无法填写所需的公式。
Sub Change()
Sheet1.Range("A:A").NumberFormat = "General"
Sheet1.Range("A11:A" & SOneLastRow + 1).Clear
SOneLastRow = Sheet1.Cells.Find("*", searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row + 1
Sheet1.Range("A11").Formula = "=IF(ISBLANK(B11),"""",1)"
Sheet1.Range("A12").Formula = "=IF(ISBLANK(B12),"""",A11+1)"
Sheet1.Range("A12:A" & SOneLastRow).FillDown
Sheet2.Range("A10:K" & STwoLastRow).Clear
Sheet2.Range("A10:K10").Formula = STwoForm
Sheet2.Range("A10:K" & SOneLastRow - 1).FillDown
Sheet3.Range("A2:X" & SThreeLastRow).Clear
Sheet3.Range("A2:X2").Formula = SThreeForm
Sheet3.Range("A2:X" & SOneLastRow - 9).FillDown
Sheet1.Range("A:A").Interior.ColorIndex = 15
End Sub
这里也是公式数组。
Sub TestForCorrect()
SOneLastRow = Sheet1.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
STwoLastRow = Sheet2.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
SThreeLastRow = Sheet3.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
If STwoLastRow + 1 <> SOneLastRow Or SThreeLastRow <> SOneLastRow Then
Call Change
End If
End Sub
Sub RefreshInit()
STwoForm(1) = "=DataRecords!G2"
STwoForm(2) = "=DataRecords!F2"
STwoForm(3) = "=DataRecords!K2"
STwoForm(4) = "=DataRecords!H2"
STwoForm(5) = "=DataRecords!I2"
STwoForm(6) = "=DataRecords!J2"
STwoForm(7) = "=DataRecords!L2"
STwoForm(8) = "=CONCATENATE(DataRecords!M2, """",DataRecords!O2)"
STwoForm(9) = "=DataRecords!N2"
STwoForm(10) = "=DataRecords!P2"
STwoForm(11) = "=DataRecords!R2"
SThreeForm(1) = "=IF(DataEntry!A11="""","""",DataEntry!$H$1)"
SThreeForm(2) = "=IF(DataEntry!A11="""","""",DataEntry!$H$3)"
SThreeForm(3) = "=IF(DataEntry!A11="""","""",DataEntry!$H$4)"
SThreeForm(4) = "=IF(DataEntry!A11="""","""",DataEntry!$H$6)"
SThreeForm(5) = "=IF(DataEntry!A11="""","""",DataEntry!$H$7)"
SThreeForm(6) = "=IF(DataEntry!A11="""","""",DataEntry!E11)"
SThreeForm(7) = "=DataEntry!A11"
SThreeForm(8) = "=IF(DataEntry!A11="""","""",DataEntry!B11)"
SThreeForm(9) = "=IF(DataEntry!A11="""","""",DataEntry!C11)"
SThreeForm(10) = "=IF(DataEntry!A11="""","""",DataEntry!D11)"
SThreeForm(11) = "=IF(DataEntry!A11="""","""",DataEntry!F11)"
SThreeForm(12) = "=IF(DataEntry!G11="""","""",DataEntry!G11)"
SThreeForm(13) = "=IF(DataEntry!H11="""","""",DataEntry!H11)"
SThreeForm(14) = "=IF(DataEntry!I11="""","""",DataEntry!I11)"
SThreeForm(15) = "=IF(DataEntry!J11="""","""",DataEntry!J11)"
SThreeForm(16) = "=IF(DataEntry!K11="""","""",DataEntry!K11)"
SThreeForm(17) = "=IF(DataEntry!L11="""","""",DataEntry!L11)"
SThreeForm(18) = "=IF(DataEntry!M11="""","""",DataEntry!M11)"
SThreeForm(19) = "=IF(DataEntry!N11="""","""",DataEntry!N11)"
SThreeForm(20) = "=IF(DataEntry!O11="""","""",DataEntry!O11)"
SThreeForm(21) = "=IF(DataEntry!P11="""","""",DataEntry!P11)"
SThreeForm(22) = "=IF(DataEntry!Q11="""","""",DataEntry!Q11)"
SThreeForm(23) = "=IF(DataEntry!R11="""","""",DataEntry!R11)"
SThreeForm(24) = "=IF(DataEntry!S11="""","""",DataEntry!S11)"
Call TestForCorrect
End Sub
解决方案
看起来您尝试将公式写入的单元格被格式化为文本,因此它忽略了公式。在编写公式之前,您需要手动或使用 VBA将其格式更改为“常规” :
Range("A11").NumberFormat = "General"
推荐阅读
- javascript - 我在 JavaScript 中的繁忙函数之外声明了一个变量,是否提高了性能?
- java - 添加 HAL 浏览器时出错:找不到依赖项 'org.springframework.data:spring-data-rest-hal-browser:'
- google-apps-script - 尝试根据变量值将单元格值复制到不同的列
- scala - 数据框中的空列
- javascript - 在 Vue 中过滤 json 响应
- python - 我想在函数 .Python 中访问 if 语句的变量
- c++ - 使用 TagLib 从 char* 缓冲区解析 ID3v2
- sql-server - 在 T-SQL 中对最多 2 列进行子查询的最佳方法是什么
- typo3 - 为来自 CLI 的图像生成 TYPO3 cropVariants
- python - 如何单击使用 Selenium 隐藏的下拉菜单?