[英]Excel Macro: Set the formula of a cell
我正在編寫一個宏,以將行插入所有選定的工作表中,然后將某些值設置為等於另一工作表中的值。 我已經設法使用以下代碼插入行,但是在設置值時遇到了麻煩。 如果沒有宏,我只需輸入=InputC7
輸入即可作為工作簿中第一張紙的名稱。
Sub InsertRows()
'
' InsertRows Macro
' Inserts rows into all selected sheets at the same position
'
Dim CurrentSheet As Object
' Loop through all selected sheets.
For Each CurrentSheet In ActiveWindow.SelectedSheets
' Insert 1 row at row 7 of each sheet.
CurrentSheet.Range("a7:a7").EntireRow.Insert
CurrentSheet.Range("c7").Value =Input!C7 'this is not working
Next CurrentSheet
End Sub
如果只需要名為“ Input”的工作表中的值,則可以執行以下操作:
CurrentSheet.Range("C7").Value = Sheets("Input").Range("C7").Value
如果需要公式“ = Input!C7”,可以執行以下操作:
CurrentSheet.Range("C7").Formula = "=Input!C7"
您需要使用“ Formula
而不是“ Value
屬性,並且該文本應加引號:
CurrentSheet.Range("c7").Formula "=Input!C7"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.