Thread: VBA Code Error
View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default VBA Code Error

Hi again Jim,

Further to what Jacob has said, I notice that you were attempting to select
a range in a worksheet that was not the selected worksheet. Cannot do that.

Your code.
sh.Range("Summary_Gross").Select.NumberFormat = _
"[$$-409] #,##0.00;[Red]-([$$-409] #,##0.00)"

Sheets("Contract Data") was the selected sheet at that point of your code.

There should be no need to select ranges to alter their contents if you
correctly address the ranges. The example below changes the values without
selecting the ranges.

I have also included some more line breaks so that hopefully the lines won't
break up on this post.

Just as a point of interest, line breaks cannot be placed anywhere in a
string between double quotes. However you can break the string into separate
parts and use a line break.

Insert double quotes where you want the break.
Insert the space and underscore for the line break.
Press Enter to push the code to the next line.
Insert an ampersand (&) then double quotes at the start of the line.

The above method effectively breaks the string between the double quotes
into 2 strings and then concatenates them with the ampersand.

Anyway the sample code that does not select worksheets or ranges.

For Each sh In ActiveWorkbook.Sheets

'Select Case Me.ComboBox1.Text
'add as many Case tests as required
Case "$"

With Sheets("Contract Data")
.Range("C7").FormulaR1C1 = "$"

.Range("G4:G34").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0.00)"

.Range("C13:C14").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0.00)"

.Range("C20").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0.00)"
End With

With sh
.Range("Summary_Gross").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0.00)"

.Range("Summary_Rate").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0.00)"

.Range("Summary_Prev").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0.00)"

.Range("Adj_Gross").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0.00)"
.Range("Adj_Rate").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0.00)"

.Range("Adj_Prev").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0.00)"

.Range("K108:K105").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0."

.Range("I67").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0."

.Range("D12:D13").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0."

.Range("K16").NumberFormat = _
"[$$-409] #,##0.00;[Red]-" _
& "([$$-409] #,##0."

End With



Now the following error 424.

Where did you dim sh? Was it inside another sub? If so, the sub where it was
running did not know its value.

If you want to use a variable in another sub in the same module then Dim it
at the top of the VBA editor before any subs (Called the Declarations area.)

If you want to use a variable in multiple modules then delare it as a Public
variable but still in the Declarations area like this.

Public sh as Worksheet

Hope this helps to point you in the right direction.


--
Regards,

OssieMac