Results 1 to 7 of 7

Thread: VBA Merge Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    4

    VBA Merge Excel

    Hi everyone,

    Im trying to create a sheet with a macro that comes from a button placed on another sheet, all its ok but when i try to merge several cells on the other sheet in the same macro it gives me the "1004" error range object worksheet ...

    My declaration

    Dim hB As Workbook

    Dim hH as Worksheet


    Set hB = Application.Workbooks("Libro1")
    Worksheets.Add(After:=Worksheets("Hoja1")).Name = "VI"
    Set hH = hB.Sheets("VI")

    sheets("VI").Activate


    hH.Range(Cells(12, 3), Cells(12, 3 + 8)).Merge <---- Here comes the error

    Thanks

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: VBA Merge Excel

    You shouldn't need to activate the sheet. when using references like that, be very explicit
    Code:
    Dim hB As Workbook
    Dim hH as Worksheet
    
    Set hB = Application.Workbooks("Libro1")
    Worksheets.Add(After:=Worksheets("Hoja1")).Name = "VI"
    Set hH = hB.Sheets("VI")
    
    'sheets("VI").Activate
    
    'put a breakpoint in and see if the following line selects the cells you wanted before merging
    hH.Range(hh.Cells(12, 3), hh.Cells(12, 3 + 8)).Select
    hH.Range(hh.Cells(12, 3), hh.Cells(12, 3 + 8)).Merge
    
    'clean up
    set hh = nothing
    set bh = nothing
    Note, if you have any data in those cells excel will alert you to that too.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: VBA Merge Excel

    It just appear the same error when it gets to the range .Select

    Error '1004'
    Error on 'Range' method of the object '_Worksheet'


  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Merge Excel

    same error when it gets to the range .Select
    you can only select cells on the active sheet, but the merge should work correctly without the sheet being activated
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: VBA Merge Excel

    I understand that, I just modify the entire sheet doing

    VI.cells(x,Y) = .... or something like that (it works)

    but when i get to that cell and try to merge i cant, it just appear that error.

    I just try

    With Vi ... or activesheet
    range....
    end with

    and the merge just appear on the first sheet and not where i want to, maybe it is because im doing it bad the way to activate it or the declarations of the workbook and worksheet.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Merge Excel

    you must specify that the cells are in the same sheet as the range, as the sample by ecniv

    hH.Range(Cells(12, 3), Cells(12, 3 + 8)).Merge
    this will cause error, you specify the range is on sheet hH, but the cells are on the active sheet

    hH.Range(hh.Cells(12, 3), hh.Cells(12, 3 + 8)).Merge
    this will work correctly, or
    Code:
    with hH
        .range(.cells(12,3),.cells(12, 3 + 8)).Merge
    end with
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: VBA Merge Excel

    I really thank you a lot!!!!! it was that!!! Thanks Thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width