1

Hi can anyone figure out why I am getting this message..

run time error '1004' Application-defined or object-defined error

Here is my code, the problem line seems to be:

range("A1").Select

here is the rest of the code:

Sub HorizontalLoop()
Dim lCol As Long

Sheets("output").Select

For lCol = 1 To 100
    Dim inputrange As String
        If Not IsEmpty(Cells(lCol).Value) Then
           inputrange = Cells(1, lCol).Value
           ActiveCell.EntireColumn.Select
           Selection.Copy
           Sheets("input").Select
           range("A1").Select
           ActiveSheet.Paste
           Sheets("output").Select

        End If

Next lCol
End Sub

Thank you in advance :)

11
  • What are you trying to achieve? Commented Apr 5, 2016 at 14:16
  • I notice that in range("A1").Select range is not with a capital, meaning that VB did not parse it as the Range method. Possibly you have to prepend the object of which you want to get a range, e.g. ActiveSheet.Range("A1").Select. Commented Apr 5, 2016 at 14:19
  • At the very top (before Sub HorizontalLoop()), put Option Explicit to make sure your variables are okay. I think you have a variable called range somewhere. Can you post the rest of your code? Commented Apr 5, 2016 at 14:26
  • for your information, no cells are in active in your code to select the entire column Commented Apr 5, 2016 at 14:28
  • would the active cell not be the cell its currently looping through? sorry if is a daft question @karthick Commented Apr 5, 2016 at 14:35

2 Answers 2

1

You need to qualify the Range("A1") with the the worksheet name (as mentioned in Paul Ogilvie's comment.

But you don't need to switch back and forth between the worksheets to paste. After the "copy" you can just add the "Destination".

I did a cut down version of your code (just 2 columns) so you can see what I mean:

Sub HorizontalLoop2()

Dim lCol As Long
Dim inputrange As String
Dim wsO As Worksheet
Dim wsI As Worksheet

Set wsO = ThisWorkbook.Worksheets("output")
Set wsI = ThisWorkbook.Worksheets("input")

For lCol = 1 To 2

If Not IsEmpty(wsO.Cells(lCol).Value) Then
inputrange = wsO.Cells(lCol).Value

wsO.Columns(lCol).Copy _
Destination:=wsI.Cells(1, lCol)

End If

Next lCol

End Sub

This assumes of course that you want to paste to the same column number!

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming that Cell A13 is Active cell in worksheet output, see the below code

worksheets("output").Range("A13").Activate
ActiveCell.EntireColumn.Copy Worksheets("input").Range("A1")

it will copy the entire column and paste it to the worksheet input

5 Comments

you were right there was no active cel, ive replaced acticecell with Cells(1, lCol).EntireColumn.Select and its worked :) thanks
@HushHoney, please mark as this is solution for your question
@HushHoney - While this certainly works, it still leaves your macro using .Select, which isn't best practice. Read through this page as using .Select can cause many headaches, especially as you bounce between worksheets/workbooks, which was some of the problem here.
I wonder If you can help me with something else, in my code I am selecting the entire column, how would I select all the column from row 4 and down?
@BruceWayne thanks, will look into it, you all have been so helpful

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.