-
Sep 25th, 2018, 09:58 AM
#1
Thread Starter
New Member
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
-
Sep 25th, 2018, 12:34 PM
#2
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.
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...
-
Sep 26th, 2018, 03:40 AM
#3
Thread Starter
New Member
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'
-
Sep 26th, 2018, 05:30 AM
#4
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
-
Sep 26th, 2018, 06:15 AM
#5
Thread Starter
New Member
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.
-
Sep 26th, 2018, 07:43 AM
#6
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
-
Sep 26th, 2018, 08:33 AM
#7
Thread Starter
New Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|