Consulting

Results 1 to 11 of 11

Thread: Solved: Error in code need help.

  1. #1
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location

    Solved: Error in code need help.

    Hi everyone,


    I'm getting a error in the code. its saying:

    Run-time error '1004'

    Select method of range class.

    This is the line its highlighting.

    [VBA]Range("B2").Select 'select the cell where you want to place your image [/VBA]

    Here is the entire code.

    [VBA]
    Sub Convert_Group_Objects_into_JPEGS()

    Dim sht As Worksheet
    Dim shp As Shape



    Application.ScreenUpdating = False
    For Each sht In ActiveWorkbook.Sheets
    For Each shp In sht.Shapes
    sht.Activate
    If shp.Type = msoGroup Then
    shp.Cut

    Range("B2").Select 'select the cell where you want to place your image
    sht.PasteSpecial Format:="Picture (JPEG)", Link:=False, _
    DisplayAsIcon:=False
    Exit For
    End If
    Next
    Next

    End Sub
    [/VBA]

    Do you know what I'm doing wrong?

    The code above convert all group objects in the workbook into Jpegs.
    SHAZAM!

  2. #2
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hey Shazam,

    I'm not getting the error...but either way I would use:

    [VBA]
    sht.Range("B2")
    [/VBA]
    Rather than not explicitly calling the sheet you're on. I don't think that's your problem (because I didn't get the error using your exact code).

    How about a dummy workbook?




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Hi malik641,


    Thank you for replying. Here is the sample workbook that I'm trying to run the code from.

    But the code you gave me it work.

    [VBA]
    sht.Range("B2").Select
    [/VBA]
    SHAZAM!

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You should reorder these lines as follows, so you don't activate the sheet for every shape.
    [VBA]
    For Each sht In ActiveWorkbook.Sheets
    sht.Activate
    For Each shp In sht.Shapes

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Shazam,

    Glad to see it's working. I'm still unsure why the code was getting the error. I thought it had to do with the ActiveSheet possibly not being the sht variable during the for-next loop, but after some testing, the ActiveSheet name and sht.Name was the same during the loop.

    ...But, I still think that Excel didn't know which sheet range you were talking about. Which is (maybe) why it needed the explicit call to the variable sht 's range.


    Anyway, if it's resolved don't forget to mark it solved




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  6. #6
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by mdmackillop
    You should reorder these lines as follows, so you don't activate the sheet for every shape.
    [vba]
    For Each sht In ActiveWorkbook.Sheets
    sht.Activate
    For Each shp In sht.Shapes

    [/vba]
    Hi mdmackillop,

    I did not know that. That will be very useful.


    malik641,


    Thank you for the help.
    SHAZAM!

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Shazam,
    It's a general rule for any loop. Avoid including any code in the loop that can be carried out once either before or after the loop.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by malik641
    I'm still unsure why the code was getting the error.
    In order to select a cell on a sheet, that sheet must be active.

    I would suggest, although I haven't tested, that the Range in Shazam's original code, (without qualification) was pointing back to the original sheet, and not the newly activated one. I always always always explicitly refer to my ranges to avoid this kind of issue.

    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  9. #9
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by Ken Puls
    I would suggest, although I haven't tested, that the Range in Shazam's original code, (without qualification) was pointing back to the original sheet, and not the newly activated one. I always always always explicitly refer to my ranges to avoid this kind of issue.

    That's what I thought....but when I tested it I checked the ActiveSheet.Name and the sht.Name in the FOR loop and it was the same....but still, like you, I'm going to assume the default was pointing to the original sheet rather than the newly activated one.

    It's better to get used to explicitly calling out everything anyway. It's better coding practice, and not to mention when VBA goes .Net (if it does... haven't looked it up yet) we're going to have to explicitly call out what we used to use for defaults.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  10. #10
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Joseph,

    I think that you would have needed to test the Range("B2").Parent to see the issue in this case.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  11. #11
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by Ken Puls
    Hi Joseph,

    I think that you would have needed to test the Range("B2").Parent to see the issue in this case.
    Hey Ken,

    Will do




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •