Home Simple vba sub giving error Compile error: Expected: =
Reply: 1

Simple vba sub giving error Compile error: Expected: =

cohara
1#
cohara Published in 2018-01-12 13:49:43Z

I have a very simple sub in excel vba:

Private Sub AlignText(r As Integer, c As Integer)                                                 'A sub to align the data in the cell
With ActiveSheet
    .Cells(r, c).HorizontalAlignment = xlCenter
    .Cells(r, c).VerticalAlignment = xlCenter
End Sub

I just want to supply column and row numbers to align text. I have tried putting in the full path to the cells using full sheet references too, but that doesn't help.

When I try to use the sub:

AlignText(NextRow, 16)

for example, I get the error:

'Compile error: Expected: ='

I have written very very similar subs before, that aligned text as well as doing other things, and they have worked fine. This must be something simple, but I don't know what.

Thanks in advance for any help.

Shai Rado
2#
Shai Rado Reply to 2018-01-12 16:26:32Z

Use:

AlignText NextRow, 16

Note: You should try to avoid using ActiveSheet. I would add the Worksheet object to your Sub as well.

Private Sub AlignText(Sht As Worksheet, r As Integer, c As Integer)

'A sub to align the data in the cell
With Sht
    .Cells(r, c).HorizontalAlignment = xlCenter
    .Cells(r, c).VerticalAlignment = xlCenter
End With

End Sub

And call it by using:

AlignText ActiveSheet, NextRow, 16 ' <-- if you have to use ActiveSheet
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.332109 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO