1

This works

  mySheet.Rows(CStr(activeRow) & ":" & CStr(activeRow + deletedRowAmount)).Delete

This does not work

mySheet.Columns(CStr(columDelete) & ":" & CStr(columDelete + deletedRowAmount)).Delete

What I am missing here?

ERROR is VBA Runtime Error 1004 “Application-defined or Object-defined error”

2
  • 1
    What are the values of the variables? What are they declared? What is the error you are getting? Commented Sep 12, 2017 at 14:14
  • Variables are longs Commented Sep 12, 2017 at 14:14

2 Answers 2

3

Columns are A,B,C,... when used in a string as you are using them. If they are numbers you will need to do it slightly different:

With mySheet    
    .Range(.cells(1,columDelete), .cells(1,columDelete + deletedRowAmount)).EntireColumn.Delete
End With
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mate. Very useful piece of code. As a newbie it is hard to understand VBA illogical syntax.
1

In general, what Scott Craner says is quite enough, but here is a way to go around it and make it easier:

Sub TestMe()

    Columns(GetColumnRange(2, 5)).Delete

End Sub

Public Function GetColumnRange(colStart As Long, colEnd) As String

    GetColumnRange = Split(Cells(1, colStart).Address(True, False), "$")(0) & _
              ":" & Split(Cells(1, colEnd).Address(True, False), "$")(0)

End Function

Try the TestMe function, it should delete the columns from 2(B) to 5(E).

5 Comments

As this is maybe easier to understand, the fact that vba will need to parse the resultant string back to numbers it will add time, albeit a fraction of a second. This is like changing your cars DC output to AC just to convert it back to DC to charge your phone. If done in a loop it will make a difference in performance.
@ScottCraner - yup, a few fractions of a second could be lost.
It was just a FWIW comment, the OP may be willing for the time payoff if it is easier to understand and use for them.
@ScottCraner - I know :)
Thank you for your proposal, but I have so many methods already in place, so not convenient to make new ones :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.