error 438 didn't occur yesterday... why today? or even at all?

jbaich

Board Regular
Joined
Nov 2, 2011
Messages
140
Hey all, I'm getting Run-time error 438 "Object doesn't support this property or method" in my code, which i didn't get yesterday and I'm not sure why I'm getting at all... It's occuring on the
Code:
.Horizontal Alignment = xlRight
line in the section of code below...

Code:
       With Template.Sheets("Forms")
    
          .Range("G13") = FILEREF
            
        For i = 0 To RowCount
          .Range("A" & 23 + i & ":B" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlRight
          .NumberFormat = "@"
          .Range("D" & 23 + i & ":E" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlCenter
          .NumberFormat = "@"
Can anyone explain this to me? I see in the properties explorer window that .HorizontalAlignment is a propert of Range... I'll admit I don't have the best handle on this higher level objects, classes, methods stuff yet...

Thanks,
Joe
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You need to have a range object on the HorizontalAlignment property

I'd guess
.Range("A" & 23 + i & ":B" & 23 + i).HorizontalAlignment = xlRight


Same for the NumberFormat
 
Upvote 0
Thanks for quick reply! is it possible to do something like below? This formating code is repeated for several different ranges so I'm wondering if I can make it as concise as possible?

Code:
with Range
.HorizontalAlignment = xlRigh
.NumberFormat = "@"

larger section of code:

Code:
       With Template.Sheets("Forms")
    
          .Range("G13") = FILEREF
            
        For i = 0 To RowCount
        
          .Range("A" & 23 + i & ":B" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlRight
          .NumberFormat = "@"
          .Range("D" & 23 + i & ":E" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlCenter
          .NumberFormat = "@"
          
          .Range("F" & 23 + i & ":G" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlLeft
          .Format = "$#,##0"
          .Range("H" & 23 + i & ":J" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlLeft
          .Format = "$#,##0"
          .Range("K" & 23 + i & ":K" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlRight
          .NumberFormat = "@"
          .Range("L" & 23 + i & ":M" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlCenter
          .NumberFormat = "@"
          
          .Range("N" & 23 + i & ":O" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlCenter
          .NumberFormat = "@"
          .Range("P" & 23 + i & ":Q" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlLeft
          .Format = "$#,##0"
          .Range("R" & 23 + i & ":U" & 23 + i).MergeCells = True
          .HorizontalAlignment = xlLeft
          .Format = "$#,##0"


          .Range("A" & 23 + i) = "Class"
          .Range("C" & 23 + i) = Master.Sheets("Original Values").Range("O" & OriginalRow + i).Value
          .Range("D" & 23 + i) = Master.Sheets("Original Values").Range("Q" & OriginalRow + i).Value
          .Range("F" & 23 + i) = Master.Sheets("Original Values").Range("S" & OriginalRow + i).Value
          .Range("H" & 23 + i) = Master.Sheets("Original Values").Range("T" & OriginalRow + i).Value
          .Range("k" & 23 + i) = "Class"
          .Range("L" & 23 + i) = Master.Sheets("Original Values").Range("O" & OriginalRow + i).Value
          .Range("N" & 23 + i) = Master.Sheets("Original Values").Range("Q" & OriginalRow + i).Value
            
            Next


           .Range("F" & 23 + RowCount + 1).Formula = "=SUBTOTAL(9,F23:D" & 23 + RowCount & ")"
           .Range("H" & 23 + RowCount + 1).Formula = "=SUBTOTAL(9,H23:H" & 23 + RowCount & ")"
           .Range("E19") = Roll
           


    End With

Thanks,
Joe
 
Upvote 0
have you tried centreacrossselection rather than merging cell, ok you you can't do left or right alignment
 
Upvote 0
Yes, that's what the With Structure is for.

Looks like you already have a with going for the Sheet.
So you can now 'Nest' another with for the range on that sheet.

Something like this
Code:
With Template.Sheets("Forms")
    .Range("G13") = FILEREF
    
    With .Range("A" & 23 + i & ":B" & 23 + i)
        For i = 0 To RowCount
        
        .MergeCells = True
        .HorizontalAlignment = xlRight
        .NumberFormat = "@"
    End With
    
    With .Range("D" & 23 + i & ":E" & 23 + i)
        .MergeCells = True
        .HorizontalAlignment = xlCenter
        .NumberFormat = "@"
    End With
End With
 
Upvote 0
Thanks guys, I'd been trying that but must have been missing something, seems to be working now though!

Cheers!
Joe
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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