Help with Excel VBA Code

I am trying to format different spreadsheets. I need the data to be in the same cells for each worksheet. This data is in column A. I need to check to see if DBCS# ( ) is above Tour, and if so insert rows until Tour is in row 175, column A. The problem is that the number after DBCS# is never the same. How can I use an IF statement to make sure that the cell above Tour says DBCS# (whatever the number is) This is the If statement that I tried. If I put DBCS#46 in my IF statement, it will not work because the number after # isn't always 46.

If (Range("A174:A174") = "DBCS#") And (Range("A175:A175") = "TOUR") Then
GoTo label_177:

Else:
Do While (Range("A174:A174") <> "DBCS#") And (Range("A175:A175") <> "TOUR")
Selection.EntireRow.Insert
Loop
End If


Answer this question

Help with Excel VBA Code

  • Syed Imam

    Which rows would you like the occurences of "TOUR" to be

    Chas


  • newbieneedshelp

    Thanks. I tried that and the program is still not working correctly.
  • Terence Curd

    That worked. Thank you so very much.
  • BD_Elektrona

    Sorry, do you get a syntax error or the code loops infinitely
    Cause i think the code after else is not the right one.

  • bslim

    Sorry, but I did not realise that you have had your problem solved.

    This is a good reason to mark a post as answered

    Chas


  • Sudheer Palyam

    Hello, maybe this will help
    Try
    If (Left(Range("A174:A174") ,5)= "DBCS#") And (Range("A175:A175") = "TOUR") Then
    GoTo label_177:


    ....

  • JB1863

    hmm,
    Try Left(Cstr(Range...),5)
    Also, is there a blank space between DBCS and # then you have to use left(string, 6).

  • RandomLick

    One more thing, use Cstr(Range(...)) to compare it with a string....

  • Shaun Bowe

    I tried that also and it is still not working. There are no spaces between DBCS, #, and the number.
  • Help with Excel VBA Code