Replacing a formula with it's result with vba

andy72685

Board Regular
Joined
Oct 20, 2016
Messages
72
I am creating a spreadsheet that pulls information from a named range, averages it, rounds it and puts it in a cell. The data will change daily, so when it closes I need it to save the current value in the cell. I have tried to set the value to a variable, and then input that variable to replace the calculation, but it says ‘Object required’ When it does, it highlights the first variable, but the yellow debug line is at the top, where the sub starts. Can you see where I’m going wrong?

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh5 As Worksheet
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Set sh5 = ThisWorkbook.Sheets(“CHANGING AVERAGES”)
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
If Range(“DATE!B2”).Value = 0 Then
If MsgBox(“You must enter 1 in cell B2 on the DATE worksheet before closing.”, _
vbCritical + vbOKOnly, “WARNING!!!!”) = vbOK Then
Cancel = True
End If
Else: Cancel = False
End If
Set a = sh5.Cells(2, Columns.Count).Value
Set b = sh5.Cells(3, Columns.Count).Value
Set c = sh5.Cells(4, Columns.Count).Value
Set d = sh5.Cells(5, Columns.Count).Value
Set e = sh5.Cells(6, Columns.Count).Value
Set f = sh5.Cells(7, Columns.Count).Value
sh5.Cells(2, Columns.Count) = a
sh5.Cells(3, Columns.Count) = b
sh5.Cells(4, Columns.Count) = c
sh5.Cells(5, Columns.Count) = d
sh5.Cells(6, Columns.Count) = e
sh5.Cells(7, Columns.Count) = f
End Sub

I posted this question on another forum and got this as an answer

Please find the the updated code.





































Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  
Dim sh5 As Worksheet
 Dim a As Integer, b As Integer, c As Integer
 Dim d As Integer, e As Integer, f As Integer
  
Set sh5 = ThisWorkbook.Sheets("CHANGING AVERAGES")
a = 0: b = 0: c = 0
 d = 0: e = 0: f = 0
  
If Range("DATE!B2").Value = 0 Then
    If MsgBox("You must enter 1 in cell B2 on the DATE worksheet before closing.", _
          vbCritical + vbOKOnly, "WARNING!!!!") = vbOK Then
                  Cancel = True
      End If
 Else: Cancel = False
 End If
  
 a = sh5.Cells(2, Columns.Count).Value
 b = sh5.Cells(3, Columns.Count).Value
 c = sh5.Cells(4, Columns.Count).Value
 d = sh5.Cells(5, Columns.Count).Value
 e = sh5.Cells(6, Columns.Count).Value
 f = sh5.Cells(7, Columns.Count).Value
  
With sh5
     .Activate
     .Cells(2, Columns.Count) = a
     .Cells(3, Columns.Count) = b
     .Cells(4, Columns.Count) = c
     .Cells(5, Columns.Count) = d
     .Cells(6, Columns.Count) = e
     .Cells(7, Columns.Count) = f
 End With
End Sub

<tbody> </tbody>

This followed:
With dimensioning the variables as integers, it should be telling the system to accept ONLY integers, right? I ask because the formula RETURNS an integer type (the actual value of the cell), but is not, strictly speaking, an integer. I input the code as stated and saved the program. When I closed it, it asked if I wanted to save the changes, which indicates it DID store a value in the variable and changed each cell accordingly, but when I reopened it, the formula still remained in the cells, rather than the result.
I even resorted to selecting EVERY column after the last and deleted them, in case there was formatting across those cells that were causing problems. and still nothing.
I’m stumped…

At this point I assumed that the variable was storing a ‘value’ beyond the end of the active columns

I edited a bit and came up with the following:

As you can see below, I abandoned the variable method, and have changed to a simple cut / paste method. I used ‘Record Macro’ to get the correct syntax. However now when I close it the Columns.Count Selects Column XFD. I copied and pasted my used range into a new sheet, so there is no entire row formatting, and no reason that I can tell that it should count all the way out there.



















































Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh5 As Worksheet
Set sh5 = ThisWorkbook.Sheets(“CHANGING AVERAGES”)
If Range(“DATE!B2”).Value = 1 Then
If MsgBox(“You must enter 1 in cell B2 on the DATE worksheet before closing.”, _
vbCritical + vbOKOnly, “WARNING!!!!”) = vbOK Then
Cancel = True
End If
Else: Cancel = False
End If
With sh5
.Activate
.Cells(2, Columns.Count).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
.Cells(3, Columns.Count).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
.Cells(4, Columns.Count).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
.Cells(5, Columns.Count).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
.Cells(6, Columns.Count).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
.Cells(7, Columns.Count).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End With
End Sub

<tbody>
</tbody>
I realize this was a bit drawn out, but I wanted to be sure the issue was fully explained. It’s been almost two weeks since I got a response on the other forum and would like some insight into this.
Thanks in advance
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Upvote 0
Thanks for the tip, but I actually meant an ENTIRELY different forum, on a different site. Figured it was time to try my luck with a new group of people. :)
 
Upvote 0
Thanks for the tip, but I actually meant an ENTIRELY different forum, on a different site. Figured it was time to try my luck with a new group of people.
Yes, the same rule actually applies (and it was written more for that situation).

The reason for it is that many people will go out there with a question and post it on numerous sites (kind of like fishing, I guess). And sometimes they will get the answer they need on one site, but not update all their other posts on the other sites. And someone on one of the others sites spends considerable time coming up with a solution, and post it. Then the person says something like "Thanks, but I already got an answer". Then the other person becomes upset he wasted his time on a question that has already been answered elsewhere.

Many forums do not allow Cross-Posting at all. We allow it, but you must provide links to the other sites where you posted the question so anyone interested in answering can look and see what has already been done. And note that many of the helpers here are active on multiple Excel sites - so word gets around!
 
Upvote 0
I think you want to have a "Save" step at the end of your code, or else it might be making all those changes, but then closing without saving them (hence, you would not see the changes the next time you opened the file since they were not saved!).
 
Upvote 0
Hi Andy,

I'm not sure if I correctly understand the problem you are facing, but if it is simply replacing the formulas in specific cells with the actual result you can use the following syntax:

Code:
ThisWorkbook.Sheets(1).Range("A1").Value = ThisWorkbook.Sheets(1).Range("A1").Value

Of course, the sheet and range are just examples to be changed according to your structure.

I hope this helps.

Cheers,
Catalin
 
Upvote 0

Forum statistics

Threads
1,217,057
Messages
6,134,335
Members
449,867
Latest member
akk3

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