Excel VBA Macro

From: Mod_Inside (MODINSIDE) 1 Dec 2010 13:55
To: ALL1 of 9

Hi

 

We have a fixed import file we run through excel for invoicing. It always has exactly 85 rows.

 

Is there a check in someway I can write to the begining of the macro if it has more or less than 85 rows it stops by displaying an error message?

 

The sheet concerned is called Main

 

Thanks in advance

EDITED: 1 Dec 2010 13:55 by MODINSIDE
From: Queeg 500 (JESUSONEEZ) 1 Dec 2010 14:00
To: Mod_Inside (MODINSIDE) 2 of 9
First thought is that it can be imported into an empty worksheet, use a countif statement to count the rows, and if it's OK, copy it into the correct worksheet, else show an error (and delete the imported crap).

That's how I'd do it but then I'm crap at coding and I'm sure there's a more elgant way.

--
From: af (CAER) 1 Dec 2010 14:24
To: Mod_Inside (MODINSIDE) 3 of 9
This should get you the last row of the first column:
VBA code:
Dim LastRow As Integer
Set Lastow = Main.Range("A65536").End(xlUp).Row
EDITED: 1 Dec 2010 14:31 by CAER
From: Mod_Inside (MODINSIDE) 1 Dec 2010 14:25
To: af (CAER) 4 of 9

Thanks Caer

 

The problem is I dont know what to do with it once ive got it!

 

Any ideas would be really appreciated

From: af (CAER) 1 Dec 2010 14:31
To: Mod_Inside (MODINSIDE) 5 of 9
Do with what, the code or the value it results in?
From: Mod_Inside (MODINSIDE) 1 Dec 2010 15:02
To: af (CAER) 6 of 9
I don't know how to show an error if the row returned is less than 85 or greater 85 in the sheet Main
From: af (CAER) 1 Dec 2010 15:12
To: Mod_Inside (MODINSIDE) 7 of 9
Oh, something like this should do it:
code:
If LastRow <> 85 Then
    PointlessVariable = MsgBox("Number of rows is not 85.", vbOKOnly, "YOU FAIL!")
End If

You could make it a bit more helpful by showing a different message depending on whether the number of rows is less than 85, or more.

edit:
Just in case you didn't know, '<>' means 'not equal to'. Sensible languages use '!=' or '!=='.
EDITED: 1 Dec 2010 15:13 by CAER
From: Queeg 500 (JESUSONEEZ) 1 Dec 2010 15:19
To: af (CAER) 8 of 9
See, knew there was a better way.

:'D

--
From: Mod_Inside (MODINSIDE) 1 Dec 2010 15:56
To: af (CAER) 9 of 9

Thanks Caer

 

Once i knew how it was supposed to be and a little tweak here and there it works a treat.

 

If i add a line it fails and if i take one away it fails. Brilliant thanks a lot