need to copy text(formula) to cells

BobtBuilder

New Member
Joined
Sep 1, 2023
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Hi there,
I can't seem to figure these two things out. I have a dashboard that serves as temporary worksheet. In this area I pull data from other worksheets manipulate them and then copy them back to their original worksheet.
After that I would need to copy the original formulas that were in the cells so I can pull data from other sheets.
I found a long way around the copy but what I thought would be the easy part was the reincorporating the formulas.
I set up a variable "Sh" that gives me the worksheet name.
1) I would like to use that variable to shorten the script and
2) be able to copy the formulas back to the cells.
Here is the code I currently have

Private Sub UpdateButton_Click()
'Create and set variables for the Call Tracking & Call Log worksheets

Dim Sh As Range
Set Sh = Range("A2") ' A2 is the cell where the worksheet name is located it is a drop down menu

If Sh = "IPC_CAD" Then
Range("D12").Copy Worksheets("IPC_CAD").Range("N7")
Range("D13").Copy Worksheets("IPC_CAD").Range("N8")
Range("D14").Copy Worksheets("IPC_CAD").Range("N9")
Range("D15").Copy Worksheets("IPC_CAD").Range("N10")

' This is where I tried to copy the formulas back but kept getting errors
'Range("D12").Formula = "=INDIRECT("" & A2 & " ! " & "n8")

' Copy "=INDIRECT(""&A2&"!"&"n9")" ("D13")
' Copy "=INDIRECT(""&A2&"!"&"n10")" ("D14")
' Copy "=INDIRECT(""&A2&"!"&"n11")" ("D13")
End If

If Sh = "IPC_USD" Then
Range("D12").Copy Worksheets("IPC_USD").Range("N7")
Range("D13").Copy Worksheets("IPC_USD").Range("N8")
Range("D14").Copy Worksheets("IPC_USD").Range("N9")
Range("D15").Copy Worksheets("IPC_USd").Range("N10")
End If

If Sh = "Bcon_CAD" Then
Range("D12").Copy Worksheets("Bcon_CAD").Range("N7")
Range("D13").Copy Worksheets("Bcon_CAD").Range("N8")
Range("D14").Copy Worksheets("Bcon_CAD").Range("N9")
Range("D15").Copy Worksheets("Bcon_CAD").Range("N10")
End If

If Sh = "Bcon_USD" Then
Range("D12").Copy Worksheets("Bcon_USD").Range("N7")
Range("D13").Copy Worksheets("Bcon_USD").Range("N8")
Range("D14").Copy Worksheets("Bcon_USD").Range("N9")
Range("D15").Copy Worksheets("Bcon_USD").Range("N10")
End If

If Sh = "Best_Flex_CAD" Then
Range("D12").Copy Worksheets("Best_Flex_CAD").Range("N7")
Range("D13").Copy Worksheets("Best_flex_CAD").Range("N8")
Range("D14").Copy Worksheets("Best_flex_CAD").Range("N9")
Range("D15").Copy Worksheets("Best_flex_CAD").Range("N10")
End If

If Sh = "Best_Flex_USD" Then
Range("D12").Copy Worksheets("Best_Flex_USD").Range("N7")
Range("D13").Copy Worksheets("Best_flex_USD").Range("N8")
Range("D14").Copy Worksheets("Best_flex_USD").Range("N9")
Range("D15").Copy Worksheets("Best_flex_USD").Range("N10")
End If

If Sh = "Bc_chk" Then
Range("D12").Copy Worksheets("Bc_chk").Range("N7")
Range("D13").Copy Worksheets("Bc_chk").Range("N8")
Range("D14").Copy Worksheets("Bc_chk").Range("N9")
Range("D15").Copy Worksheets("Bc_chk").Range("N10")
End If

If Sh = "Bc_USD" Then
Range("D12").Copy Worksheets("Bc_usd").Range("N7")
Range("D13").Copy Worksheets("Bc_usd").Range("N8")
Range("D14").Copy Worksheets("Bc_usd").Range("N9")
Range("D15").Copy Worksheets("Bc_usd").Range("N10")
End If

If Sh = "Bc_esave" Then
Range("D12").Copy Worksheets("Bc_esave").Range("N7")
Range("D13").Copy Worksheets("Bc_esave").Range("N8")
Range("D14").Copy Worksheets("Bc_esave").Range("N9")
Range("D15").Copy Worksheets("Bc_esave").Range("N10")
End If


End Sub

Thank you
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Could you provide a copy of your sheet with the data you're copying using the XL2BB add in as I'm struggling to understand exactly what formula you're trying to "copy back". In the meantime, here's a shortened script using the sh variable.
VBA Code:
Option Explicit
Sub BobtBuilder()
    Dim ws As Worksheet, sh As String, i As Long, exists As Boolean
    Set ws = Worksheets("Sheet1")       '<-- *** Change name to sheet with original data ***
    sh = ws.Range("A2").Value2
    
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = sh Then exists = True
    Next i
    If Not exists Then MsgBox sh & " does not exist in this workbook": Exit Sub
    
    ws.Range("D12:D15").Copy Worksheets(sh).Range("N7")
End Sub
 
Upvote 0
i
Could you provide a copy of your sheet with the data you're copying using the XL2BB add in as I'm struggling to understand exactly what formula you're trying to "copy back". In the meantime, here's a shortened script using the sh variable.
VBA Code:
Option Explicit
Sub BobtBuilder()
    Dim ws As Worksheet, sh As String, i As Long, exists As Boolean
    Set ws = Worksheets("Sheet1")       '<-- *** Change name to sheet with original data ***
    sh = ws.Range("A2").Value2
   
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = sh Then exists = True
    Next i
    If Not exists Then MsgBox sh & " does not exist in this workbook": Exit Sub
   
    ws.Range("D12:D15").Copy Worksheets(sh).Range("N7")
End Sub
in essence i am using "A2" as a drop down menu that shows the worksheets, when chosen i have 5 cells that pick up information from that sheet. I then change some ofthe cells with information. from there , when i hit a button, i need that new information returned to its prospective sheet and copy the indirect formula back to the cells i had manually overridden so i can once again choose new info from another sheet.
 
Upvote 0
Try the following on a copy of your workbook:
VBA Code:
Option Explicit
Sub BobtBuilder2()
    Dim ws As Worksheet, sh As String, i As Long, exists As Boolean
    Set ws = Worksheets("Sheet1")       '<-- *** Change name to sheet with original data ***
    sh = ws.Range("A2").Value2
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = sh Then exists = True
    Next i
    If Not exists Then MsgBox sh & " does not exist in this workbook": Exit Sub
    
    With ws
        .Range("D12:D15").Copy
        Worksheets(sh).Range("N7").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        .Range("D12").Formula2R1C1 = "=INDIRECT(TEXT(R2C1,""'@'!"")&""N7"")"
        .Range("D13").Formula2R1C1 = "=INDIRECT(TEXT(R2C1,""'@'!"")&""N8"")"
        .Range("D14").Formula2R1C1 = "=INDIRECT(TEXT(R2C1,""'@'!"")&""N9"")"
        .Range("D15").Formula2R1C1 = "=INDIRECT(TEXT(R2C1,""'@'!"")&""N10"")"
    End With
End Sub
 
Upvote 0
Solution
Try the following on a copy of your workbook:
VBA Code:
Option Explicit
Sub BobtBuilder2()
    Dim ws As Worksheet, sh As String, i As Long, exists As Boolean
    Set ws = Worksheets("Sheet1")       '<-- *** Change name to sheet with original data ***
    sh = ws.Range("A2").Value2
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = sh Then exists = True
    Next i
    If Not exists Then MsgBox sh & " does not exist in this workbook": Exit Sub
   
    With ws
        .Range("D12:D15").Copy
        Worksheets(sh).Range("N7").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        .Range("D12").Formula2R1C1 = "=INDIRECT(TEXT(R2C1,""'@'!"")&""N7"")"
        .Range("D13").Formula2R1C1 = "=INDIRECT(TEXT(R2C1,""'@'!"")&""N8"")"
        .Range("D14").Formula2R1C1 = "=INDIRECT(TEXT(R2C1,""'@'!"")&""N9"")"
        .Range("D15").Formula2R1C1 = "=INDIRECT(TEXT(R2C1,""'@'!"")&""N10"")"
    End With
End Sub
I pasted that code into the worksheet, and the =INDIRECT(TEXT(R2C1,""'@'!"")&""N7"") was pasted, but I am not sure what the text was for, maybe I was unclear, i simply wanted to copy the indirect statement ( =INDIRECT(""&A2&"!"&"n7") ) into the cells (A2 being R2C1), but kept getting a error on the "!"
 
Upvote 0
but kept getting a error on the "!"
The code works fine for me. This is what I get when I run the code (ignore the values - look at the formulas)
BobtBuilder.xlsm
ACD
2Bcon_CAD
3
4
5
6
7
8
9
10
11
121
132
143
154
Sheet1
Cell Formulas
RangeFormula
D12D12=INDIRECT(TEXT($A$2,"'@'!")&"N7")
D13D13=INDIRECT(TEXT($A$2,"'@'!")&"N8")
D14D14=INDIRECT(TEXT($A$2,"'@'!")&"N9")
D15D15=INDIRECT(TEXT($A$2,"'@'!")&"N10")
Cells with Data Validation
CellAllowCriteria
A2List=$B$2:$B$10


This is what is on the Bcon_CAD sheet:
BobtBuilder.xlsm
N
71
82
93
104
Bcon_CAD


If you provide a copy of your sheet using the XL2BB add in, or share your file via Google Drive, Dropbox or similar file sharing platform, then I'll be happy to look at it again - otherwise I'll step back and hope other volunteers will step forward and assist you with this issue.
 
Upvote 0
The code works fine for me. This is what I get when I run the code (ignore the values - look at the formulas)
BobtBuilder.xlsm
ACD
2Bcon_CAD
3
4
5
6
7
8
9
10
11
121
132
143
154
Sheet1
Cell Formulas
RangeFormula
D12D12=INDIRECT(TEXT($A$2,"'@'!")&"N7")
D13D13=INDIRECT(TEXT($A$2,"'@'!")&"N8")
D14D14=INDIRECT(TEXT($A$2,"'@'!")&"N9")
D15D15=INDIRECT(TEXT($A$2,"'@'!")&"N10")
Cells with Data Validation
CellAllowCriteria
A2List=$B$2:$B$10


This is what is on the Bcon_CAD sheet:
BobtBuilder.xlsm
N
71
82
93
104
Bcon_CAD


If you provide a copy of your sheet using the XL2BB add in, or share your file via Google Drive, Dropbox or similar file sharing platform, then I'll be happy to look at it again - otherwise I'll step back and hope other volunteers will step forward and assist you with this issue.
Thank you, it was an excel issue, but it works much appreciated
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top