• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

If, Left, Right formula via VBA CODE

Dokat

Member
Hi,

I am trying to write below formula in VBA code. Can someone please help me?

=IF(LEFT(C2,1)="4",("4 we "&RIGHT(C2,8)),"""YTD")

I tried below code but receiving Compile Error, Syntax error

Sub LeftRight()
Dim r1 As Range, r2 As Range, LResult As String
Set r1 = Range("C1:C12")
Set r2 = Range("B1:B12")
LResult = if(Left(R1, 1)="4","4 we "&RIGHT(C2,8)),"""YTD")
End Sub
 
Last edited:
Assuming the answer is going in D1:D12
it will be something like
Code:
Sub LeftRight()
Dim c As Range
For Each c In Range("D1:D12")
  If Left(c.Offset(0, -2), 1) = "4" Then
      c.Value = "4 we " & Right(c.Offset(0, -1), 8)
  Else
      c.Value = "YTD"
  End If
Next
End Sub
 
There are inconsistencies in your trial code and hence it is unclear what you are trying to achieve?

eg:
Sub LeftRight()
Dim r1 As Range, r2 As Range, LResult As String
Set r1 = Range("C1:C12")
Set r2 = Range("B1:B12")
LResult = if(Left(R1, 1)="4","4 we "&RIGHT(C2,8)),"""YTD")
End Sub



You have defined R1 and r2 as ranges
But then you only use R1 and C2 in the formula ?

Can you please describe what you are trying to achieve or Please attach a sample file?
 
I changed the offset number it fixed half of the issue but still it's not returning YTD where it supposed to.

Code:
Sub LeftRight()
Dim c As Range
For Each c In Range("D2:D13")
  If Left(c.Offset(0, -1), 1) = "4" Then
      c.Value = "4 we " & Right(c.Offset(0, -1), 8)
  Else
      c.Value = "YTD"
  End If
Next
End Sub

Thanks
 
Last edited:
Iam trying the code to return YTD in the row where it starts with "Building....' Please see attached file.

Thanks
 

Attachments

  • Book2.xlsm
    16.2 KB · Views: 2
Back
Top