VBA deletes formulas in range

Trixafett

New Member
Joined
Oct 9, 2023
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
How do i retain it from changing my formulas into Values.
The only value change should be (i, 14) or Column N
VBA Code:
Option Explicit

Sub btnStk_Click()
    Dim rData As Range, arrData
    Dim i As Long
    Dim sName As Long
    Dim sNote As String
    Set rData = Sheets("Inventory").Range("A3").CurrentRegion
    arrData = rData.Value
    For i = 2 To UBound(arrData)
        If Len(arrData(i, 14)) > 0 And VBA.IsNumeric(arrData(i, 14)) Then
            arrData(i, 7) = arrData(i, 7) + arrData(i, 14)
            If Val(arrData(i, 14)) > 0 Then
                sNote = "Stock Added"
            Else
                sNote = "Stock Removed"
            End If
            ' Clear column C
            Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(1, 5).Value _
                = Array(Now(), Environ("UserName"), sNote, arrData(i, 3), arrData(i, 14))
            arrData(i, 14) = ""
        End If
    Next
    rData.Value = arrData
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi,

Try like this:
VBA Code:
Option Explicit
Sub btnStk_Click()
    Dim rData As Range, arrData
    Dim i As Long
    Dim sName As Long
    Dim sNote As String
    Set rData = Sheets("Inventory").Range("A3").CurrentRegion
    arrData = rData.Formula
    For i = 2 To UBound(arrData)
        If Len(arrData(i, 14)) > 0 And VBA.IsNumeric(Evaluate(arrData(i, 14))) Then
            arrData(i, 7) = Evaluate(arrData(i, 7)) + Evaluate(arrData(i, 14)) 'This must stay as Value since you are casting to a value directly.
            If Val(Evaluate(aarrData(i, 14))) > 0 Then
                sNote = "Stock Added"
            Else
                sNote = "Stock Removed"
            End If
            ' Clear column C
            Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(1, 5).Value _
                = Array(Now(), Environ("UserName"), sNote, Evaluate(arrData(i, 3)), Evaluate(arrData(i, 14)))
            arrData(i, 14) = ""
        End If
    Next
    rData.Formula = arrData
End Sub
 
Upvote 0
Hi,

Try like this:
VBA Code:
Option Explicit
Sub btnStk_Click()
    Dim rData As Range, arrData
    Dim i As Long
    Dim sName As Long
    Dim sNote As String
    Set rData = Sheets("Inventory").Range("A3").CurrentRegion
    arrData = rData.Formula
    For i = 2 To UBound(arrData)
        If Len(arrData(i, 14)) > 0 And VBA.IsNumeric(Evaluate(arrData(i, 14))) Then
            arrData(i, 7) = Evaluate(arrData(i, 7)) + Evaluate(arrData(i, 14)) 'This must stay as Value since you are casting to a value directly.
            If Val(Evaluate(aarrData(i, 14))) > 0 Then
                sNote = "Stock Added"
            Else
                sNote = "Stock Removed"
            End If
            ' Clear column C
            Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(1, 5).Value _
                = Array(Now(), Environ("UserName"), sNote, Evaluate(arrData(i, 3)), Evaluate(arrData(i, 14)))
            arrData(i, 14) = ""
        End If
    Next
    rData.Formula = arrData
End Sub

Hi thanks for the quick response, the ones i need to add together are values, the rest in the array are formulas that need not be touched.
so it does not work with .Formula
 
Upvote 0
Is there any chance you just mean you only want to write back column 14 ?
VBA Code:
rData.Columns(14).Value = Application.Index(arrData, 0, 14)

Note: You also seem to be updating column 7, so you would want to do the same thing for that column.
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,973
Members
452,540
Latest member
haasro02

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