Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VBA "Invalid Procedure Call or Argument" Error when extracting text before comma

I am receiving "Error 5" when trying to execute below code. Aim is to extract all characters before comma in a cell and then paste them to another cell based on the if statement. I am sure the last line causes the problem but I am not sure why.

Dim r As Long, commapos As Long, m As Long

m = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row


For r = 2 To m

        If Cells(r, 16).Value <> "0" And Cells(r, 9).Value = "" Then
        commapos = InStr(1, Cells(r, 8), ",")
        Cells(r, 9).Value = Left(Cells(r, 8), commapos - 1)
    
    End If
    
Next r
      End Sub

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

On the Left function Syntax

Left(string, length) where length can’t be less than 0

Try this:

Dim r As Long, commapos As Long, m As Long

m = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row


For r = 2 To m

        If Cells(r, 16).Value <> "0" And Cells(r, 9).Value = "" Then
        commapos = InStr(1, Cells(r, 8), ",")
        
        If commapos <> 0 Then
            Cells(r, 9).Value = Left(Cells(r, 8), commapos - 1)
        End If
        
        
    End If
    
Next r
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading