Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 2007: Object variable or With block variable not set ERROR 1

Status
Not open for further replies.

canasta

Technical User
May 15, 2010
7
US
Am getting an 'object variable or With block variable not set' error on below part of my code to transpose a column of data in Excel. The error is happening on this line:
.Range("D1").Select

Any ideas what I'm doing wrong? Thank you.

With xSheet
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
.Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End With
 



Why are not ALL ranges referencing a sheet object? What sheet is the xSheet object? It appears that xSheet is not the active sheet.


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thank you - xSheet is dimmed As Worksheet. I am not sure how to answer, so sorry. I've gotten this far with a mix of recording macros and help menus and googling to find code. In this case I used the macro recorder to see what would happen if I copied a column from the top of the data to bottom, then moved a couple cells to the right and pasted special to transpose. Somewhere along the way it stopped working - due to me 'refining' it a little too much, and now I am trying to start over and just get one piece at a time straight. This piece I simply want to go to the top of column A, scroll down to make sure I've gotten all the text, copy it, paste it on column D in a transposed state. Thanks.
 



You must have a statement that assigns a specific sheet object to your xSheet object variable, prior to using it in your code, like
Code:
Set xSheet = Worksheets("Some Sheet Name")


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thank you - getting closer, that helped, and now it is stopping on below line. It's copied the column, moved to the cell I need, but not pasting. What have I done wrong? Thanks again for your help.

Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
 


Please post ALL your code, not just this fragment.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks, I've modified it and here is the entire code. The error message is the one about copy/paste area not being same size and shape, to click on a single cell then paste. I tested it on a smaller dataset and realized I believe the rows I am copying to transpose will exceed the number of columns, even in 2007. So sorry, I should have seen that. I will have to rethink this and approach from another angle. Thanks for your help, turns out I've got more issues to deal with and am heading back to the drawing board. :)

Sub PasteSpecial()

Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

End Sub
 



Here is a better way to code your procedure. Please post VBA (macro) questions in Forum707 in the future.
Code:
Sub PasteSpecial()

    Range(Range("A1"), Range("A1").End(xlDown)).Copy

    Range("D1").PasteSpecial _
      Paste:=xlPasteAll, _
      Operation:=xlNone, _
      SkipBlanks:= False, _
      Transpose:=True

End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thank you for the streamlining of the code and advice on proper place to post, I will do that in the future. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top