excel macro variable as a formula

mcrandall99

New Member
Joined
Apr 13, 2021
Messages
3
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
i have a excel vba macro in which I have a complicated formula, which I have managed to put together using various variables. I have combined them and I can see the formula in msgbox... but I can't paste it into excel sheet - it doesn't show a formula.

could someone tell me where I am wrong with this?

VBA Code:
Sub dated()
Dim TheDate As Date
Dim msg As String
Dim string1 As String
Dim string2 As String
Dim string3 As String
Dim strong4 As String
Dim string5 As String
Dim string6 As String
Dim string7 As String
Dim string8 As String
Dim string9 As String
Dim string10 As String
Dim strig11 As String
Dim string12 As String
Dim combined As String
MyDate = "2/02/2019"
msg = DateDiff("d", MyDate, Now)
string1 = "GETSATDAT"
string2 = "("""
string3 = "\\modbusdataserver.epic.systeminternalsK.com\KNN_SIFT_NEWYORK:TESTMAT:DRIVE Linx:KNN_SIFT_NEWYORK:TESTMAT_01_NNN._09_GOR_0221.IV"
string4 = ","
string5 = " "" "
string6 = "*"
string7 = "-"
string8 = "d"
string9 = 57
string10 = "inside"
string11 = ")"
string12 = "="
combined = string12 & string1 & string2 & string3 & Chr(34) & string4 & string5 & string6
combined2 = string7 & msg & string8 & Chr(34) & string4 & string6 & string4 & string9 & Chr(34)
combined3 = Chr(34) & string4 & Chr(34) & string10 & Chr(34) & string11
combined4 = combined & combined2 & combined3
MsgBox combined4
Range("A1").Select
ActiveCell.FormulaR1C1 = "=combined4"
Range("A2").Select
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
As your formula can be directly written to the worksheet without any 'string' variable​
but anyway according to your VBA procedure the correct line is ActiveCell.Formula = combined4 …​
 
Upvote 0
As your formula can be directly written to the worksheet without any 'string' variable​
but anyway according to your VBA procedure the correct line is ActiveCell.Formula = combined4 …​

you could figure out why I went through the hassle because I don't know much :)

Excel Formula:
=GETSATDat("\\modbusdataserver.epic.systeminternalsK.com\KNN_SIFT_NEWYORK:TESTMAT:DRIVE Linx:KNN_SIFT_NEWYORK:TESTMAT_01_NNN._09_GOR_0221.IV","*-159x","*",225,"","frside")

the above line is the actual formula - but I want to pass on variables to where '159' is placed above. basically the variable msg will replace "159" - rest of the formula will stay as is. is there an easier way to accomplish this?
 
Upvote 0
According to your actual formula a VBA demonstration as a beginner starter :​
VBA Code:
Sub Demo1()
    Const F = "=GETSATDat(""\\modbusdataserver.epic.systeminternalsK.com\KNN_SIFT_NEWYORK:TESTMAT:DRIVE Linx:KNN_SIFT_NEWYORK:TESTMAT_01_NNN._09_GOR_0221.IV"",""*-¤x"",""*"",225,"""",""frside"")"
    [A1].Formula = Replace(F, "¤", DateDiff("d", #2/2/2019#, Now))
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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