Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6569957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:41:45+00:00 2026-05-25T14:41:45+00:00

In an Excel 2007 VB Macro, I’m trying to do is take a comma

  • 0

In an Excel 2007 VB Macro, I’m trying to do is take a comma separate String, split it, and then reduce it, removing duplicate consecutive values. So “2,2,2,1,1” would become “2,1”, or “3,3,3,2,3,3,3” would become “3,2,3”.

It looks like it should work, but when it gets to the “If currentVal.equals(prevVal) = False Then”, it his a runtime error 424, ‘Object required’.

It’s been forever since I did any VB, and that was VP6.

Sheets("Sheet1").Select
Range("I1").Select

Dim data() As String
Dim currentVal, prevVal As String
Dim output As String
Dim temp As Boolean

Do Until Selection.Value = ""
        data = Split(Selection, ",")
        output = ""
        prevVal = ""
        For Each elem In data
            currentVal = CStr(elem)
            If currentVal.equals(prevVal) = False Then
            output = output + elem + ","
            End If
        Next elem
        Selection.Value = output
        Selection.Offset(1, 0).Select
Loop
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:41:46+00:00Added an answer on May 25, 2026 at 2:41 pm

    There’s a few problems. First, you can’t use:

    Dim currentVal, prevVal As String
    

    You must use:

    Dim currentVal as String
    Dim prevVal As String
    

    or:

    Dim currentVal as String, prevVal as String
    

    …as you can’t shortcut types in VBA unfortunately. Secondly, strings aren’t objects in VBA so there’s no .equals (or any other method). You want:

    If currentVal <> prevVal Then
    

    Lastly, you need to set prevVal at the end of your loop or your code won’t work as expected.

    EDIT Here’s some working code:

    Dim data() As String
    Dim currentVal As String, prevVal As String
    Dim output As String
    Dim temp As Boolean
    
    Do Until Selection.Value = ""
            data = Split(Selection, ",")
            output = ""
            prevVal = ""
            For Each elem In data
                currentVal = CStr(elem)
                If currentVal <> prevVal Then
                    output = output + elem + ","
                End If
                prevVal = currentVal
            Next elem
            Selection.Value = output
            Selection.Offset(1, 0).Select
    Loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i am working with c# and excel-2007... i wanted to add a macro
I am working on a Excel 2007 workbook that will contain a macro to
I have the following macro which needs to loop though an Excel-2007 table. The
I just wanted to know why Excel-2007 record macro does not write anything for
Does someone know library for C# to read xlsm file (Excel 2007 with macro)
Does MOSS 2007 Excel Services support macro execution?
I have written a macro in excel 2007 to log water-level readings. Once logged,
I am trying to use Excel's (2007) built in FFT feature, however, it requires
I have a simple Excel 2007 Macro that is reading a text file line
I'm running excel 2007 on XP. Is there a way to stop a macro

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.