简体   繁体   中英

Excel Macro: Set the formula of a cell

I'm writing a macro to insert a row into all selected sheets and then set some of the values to equal the values in another sheet. I've managed to insert the row using the following code, but I'm getting stuck trying to set the values. Without a macro, I'd simply enter =InputC7 Input being the name of the first sheet in the workbook.

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

If you just want the value from the sheet named "Input" you can do this:

CurrentSheet.Range("C7").Value = Sheets("Input").Range("C7").Value

If you want the formula "=Input!C7" you can do this:

CurrentSheet.Range("C7").Formula = "=Input!C7"

您需要使用“ Formula而不是“ Value属性,并且该文本应加引号:

CurrentSheet.Range("c7").Formula "=Input!C7" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM