1

I got error 91 when I execute my code in VBA

There is my code

Sub Traingulation()    
 Dim table As Range
 Dim tableRows As Integer
 Dim tableCols As Integer
 Dim ws As Worksheet   

 Set table = ws.Cells
 tableRows = ws.UsedRange.Rows.Count
 tableCols = ws.UsedRange.Columns.Count

 Dim x1 As Double
 Dim x2 As Double
 Dim x3 As Double
 Dim y1 As Double
 Dim y2 As Double
 Dim y3 As Double   

 Dim r1 As Double
 Dim r2 As Double
 Dim r3 As Double    

 Dim phase1 As Double
 Dim phase2 As Double
 Dim phase3 As Double    
 Dim frequence1 As Double
 Dim frequence2 As Double
 Dim frequence3 As Double   
 Dim A As Double
 Dim B As Double
 Dim C As Double
 Dim D As Double
 Dim E As Double
 Dim F As Double   
 Dim Xu As Double
 Dim Yu As Double

 x1 = Range("L5").Value
 x2 = Range("L6").Value
 x3 = Range("L7").Value
 y1 = Range("M5").Value
 y2 = Range("M6").Value
 y3 = Range("M7").Value

 For i = 1 To table1Rows
   For j = 1 To table1Cols
     If table(i, 5).Value = 1 Then
       phase1 = table(i, 2).Value & frequence1 = table(i, 3).Value
     End If
   Next

   If table(i, 5).Value = 2 Then  
     phase1 = table(i, 2).Value & frequence1 = table(i, 3).Value
   End If
 Next

 If table(i, 5).Value = 3 Then
   phase1 = table(i, 2).Value & frequence1 = table(i, 3).Value
 End If

 r1 = -(3 * 10 ^ 8 * phase1) / 4 * 3.14 * frequence1    
 r2 = -(3 * 10 ^ 8 * phase2) / 4 * 3.14 * frequence2
 r3 = -(3 * 10 ^ 8 * phase3) / 4 * 3.14 * frequence3

 A = x3 - x1
 B = y3 - y1
 C = x3 - x2
 D = y3 - y2
 E = ((r1) ^ 2 - (r3) ^ 2) - ((x1) ^ 2 - (x3) ^ 2) - ((y1) ^ 2 - (y3) ^ 2)
 F = ((r2) ^ 2 - (r3) ^ 2) - ((x2) ^ 2 - (x3) ^ 2) - ((y2) ^ 2 - (y3) ^ 2)

 Xu = 0.5 * ((F * B) - (D * E)) / ((C * D) - (A * D))
 Yu = (0.5 * E / B) - (A * ((F * B) - (D * E))) / (B * ((C * B) - (A * B))) 

 MsgBox "Triangulation : Xu =  " & Xu & ",  et Yu =  : " & Yu & " "
End Sub
Community
  • 1
  • 1
Yassine
  • 17
  • 5
  • 1
    what exactly `phase1 = table(i, 2).Value & frequence1 = table(i, 3).Value` suppose to be ? – Shai Rado Feb 01 '17 at 15:17
  • Set your table to either your sheet name or "ActiveSheet.Cells" instead of ws.Cells. Same for your tableRows and tableCols – pokemon_Man Feb 01 '17 at 15:18
  • 4
    You do not appear to be defining `ws`. – SJR Feb 01 '17 at 15:21
  • phase1 and frequence1 are parameters , I have phase in column 2 and frequence in column 3 . when I said phase1 means that I select phase1 of Antenna1 and – Yassine Feb 01 '17 at 15:25
  • As a side point you can dimension variables in this format: Dim A As Double, B As Double, C as Double – Preston Feb 01 '17 at 15:28
  • I don't think that the problem was in the condition – Yassine Feb 01 '17 at 15:31
  • @Yassine I believe Shai Rado is pointing out that `phase1 = table(i, 2).Value & frequence1 = table(i, 3).Value` does not what you think it does. Namely, it stores into `phase1` the result of calculating `table(i, 2).Value & frequence1 = table(i, 3).Value`, which is `(table(i, 2).Value & frequence1) = table(i, 3).Value`, which concatenates `table(i, 2).Value` and `frequence1` as strings and then verifies that the result equals the apparently numeric value of `table(i, 3).Value`. The result of that comparison, `True` or `False`, is stored in `phase1`. – GSerg Feb 01 '17 at 15:35
  • @Yassine Yes, it is not the source of the error 91. Its source, as SJR has noted, is that you never assign `ws` with anything. – GSerg Feb 01 '17 at 15:37
  • @Yassine yes, define your worksheet in "ws" else use activesheet – pokemon_Man Feb 01 '17 at 15:45
  • have I write ws.activesheet.Cells ...? – Yassine Feb 01 '17 at 15:53
  • 2
    @Yassine no, `Set ws = ActiveSheet` (or `Set ws = Worksheets("some other sheet")`, etc.) – David Zemens Feb 01 '17 at 15:57
  • Also: `Integer` will overflow if your `UsedRange` has more than 32,767 rows. Use a `Long` whenever you're storing anything that has to do with row numbers. – Mathieu Guindon Feb 01 '17 at 15:59

1 Answers1

-2

Define your worksheet here:

 Set table = ActiveSheet.Cells
 tableRows = ActiveSheet.UsedRange.Rows.Count
 tableCols = ActiveSheet.UsedRange.Columns.Count
pokemon_Man
  • 902
  • 1
  • 9
  • 25
  • I corrected this three lines but I have another error 6 , and before I modified all doubles by Long but I got the same error – Yassine Feb 01 '17 at 16:05
  • you should accept this as a answer since it fix your initial question and ask a new question regarding your new error. – pokemon_Man Feb 01 '17 at 16:08
  • thank you so muck @pokemon_Man I'll ask a new question now – Yassine Feb 01 '17 at 16:10
  • @Yassine Spitting `ActiveSheet` everywhere instead of your `ws` [is bad](http://stackoverflow.com/q/10714251/11683). Please do [what David Zemens said](http://stackoverflow.com/questions/41983229/error-91-vba-excel/41984119#comment71146053_41983229) instead. – GSerg Feb 01 '17 at 21:38