首页 > 解决方案 > vba 输入公式引用另一个动态工作表中的单元格

问题描述

我正在尝试根据一年中的月份将公式输入到单元格中。我现在面临的问题是用于命名工作表的相同字符串无法很好地填充到公式中。我得到的是=Jan-'19'!N876而不是='Jan-19'!N876在牢房里。感谢是否有人可以提供帮助。谢谢!

Sub Summary()

Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim row As String
Dim newmth As String
newmth = Format(DateAdd("m", -1, Date), "mmm-yy")

row = 4
Set wb = ThisWorkbook
Set sht1 = wb.Sheets(newmth)
Set sht2 = wb.Sheets("Summary")

Application.ScreenUpdating = False

Do While sht2.Range("B" & (row)) <> ""

If sht2.Range("B" & (row)).Text = newmth Then

sht2.Range("D" & (row)).Formula = "=" & sht1.Name & "!" & "N876"

End If
row = row + 1
Loop
Application.ScreenUpdating = True

End Sub

标签: excelvbaloops

解决方案


尝试sht2.Range("D" & (row)).Formula = "= '" & sht1.Name & "'!" & "N876"


推荐阅读