Compile error: Invalid use of property

Elshey

New Member
Joined
Jan 19, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I'm trying to use Macros to quicken the process of merging cells with the same name. However, I keep getting a compile error.
This is my code. The highlighted part is the area the error was flagged.

1674179494739.png
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi and welcome to MrExcel

Review the following observations:
1. Do not use reserved words to name macros or variables. MergeCells is a property of the cell object.
2. The value of the VerticalAlignment property is missing
3. Do not use this: GoTo MergeCells , it is not good practice to use the GoTo statement.
I don't really know what the objective is, you should give an example of what you have on your sheet and another example of what you want as a result.
But try the following:

VBA Code:
Sub Merge_Cells_Test()
  Dim rng As Range
  For Each rng In Selection
    If rng.Value = rng.Offset(1, 0).Value And rng.Value <> "" Then
      Range(rng, rng.Offset(1, 0)).Merge
      Range(rng, rng.Offset(1, 0)).VerticalAlignment = xlCenter
      Range(rng, rng.Offset(1, 0)).HorizontalAlignment = xlCenter
    End If
  Next
End Sub
----------
Note Code Tag:
In future please use code tags when posting code, rather that a picture.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
---------
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
---------
 
Upvote 0
Solution
Hi and welcome to MrExcel

Review the following observations:
1. Do not use reserved words to name macros or variables. MergeCells is a property of the cell object.
2. The value of the VerticalAlignment property is missing
3. Do not use this: GoTo MergeCells , it is not good practice to use the GoTo statement.
I don't really know what the objective is, you should give an example of what you have on your sheet and another example of what you want as a result.
But try the following:

VBA Code:
Sub Merge_Cells_Test()
  Dim rng As Range
  For Each rng In Selection
    If rng.Value = rng.Offset(1, 0).Value And rng.Value <> "" Then
      Range(rng, rng.Offset(1, 0)).Merge
      Range(rng, rng.Offset(1, 0)).VerticalAlignment = xlCenter
      Range(rng, rng.Offset(1, 0)).HorizontalAlignment = xlCenter
    End If
  Next
End Sub
----------
Note Code Tag:
In future please use code tags when posting code, rather that a picture.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
---------
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
---------
I see. Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,308
Members
449,151
Latest member
JOOJ

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top