variable using formula

Ingemar

New Member
Joined
May 8, 2017
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hello Excel users

The following two rows works fine. They check if value in cell C4 is correct (202102) and return the value in a modified shape to the cell A1 (2021-02).

Range("A1").Select
ActiveCell.FormulaR1C1 = "=IF(AND(ISNUMBER(R[3]C[2]),LEN(R[3]C[2])=6),LEFT(R[3]C[2],4)&""-""&RIGHT(R[3]C[2],2),"""")"


I have tried to create a variable, using the formula below, which is a minor modification from the formula above, but I only get the result "FALSE" in the Message box.

Yearandmonth = FormulaR1C1 = "=IF(AND(ISNUMBER(R[3]C[2]),LEN(R[3]C[2])=6),LEFT(R[3]C[2],4)&""-""&RIGHT(R[3]C[2],2),"""")"
MsgBox Yearandmonth

I am grateful for any help.

Kind regards

Ingemar
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Perhaps:

VBA Code:
Dim Yearandmonth As String

With Range("A1")
    .Formula = "=IF(AND(ISNUMBER(C4),LEN(C4)=6),LEFT(C4,4) & ""-"" & RIGHT(C4,2),"""")"
    Yearandmonth = .Value
End With

'or

With Range("C4")
    If (IsNumeric(.Value) And Len(.Value) = 6) Then Yearandmonth = Left(.Value, 4) & "-" & Right(.Value, 2)
End With
 
Upvote 0
Dear Stephen,

I have tested the first alternative and it works great. T H A N K Y O U very much for the help!


Kind regards

Ingemar
 
Upvote 0

Forum statistics

Threads
1,223,943
Messages
6,175,552
Members
452,652
Latest member
eduedu

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