VBA - Overflow Error

berwicks

New Member
Joined
Nov 29, 2011
Messages
6
Hi,

I am really new to writing code in VBA and the code below is meant to convert information on Goods Received Notes into XML for posting into a legacy system.

Each line in Excel contains details of the individual GRN for up to three order lines. The macro should then take this data and create an appropriate XML file.

I was getting a loop error, but after some digging on this site I resolved that, but now I am getting an overflow error.

Can someone please have a look at my code and let me know where I am going wrong?

Code:
Sub GenerateXML()
'
   Dim intRowS As Integer, intRowC As Integer, intRowC2 As Integer, intRowD As Integer
   Dim strTab As String, strTab2 As String, tempco As String
   
   
   
   intRowS = 10                  'row count on DataEntry Sheet
   intRowC = 3                  'row count on GenCode Sheet
   intRowC2 = 3
   intRowS2 = 6
   intRowS3 = 5
   intRowS4 = 4
   intRowS5 = 3
   strTab = "GenCode"
   strTab2 = "DataEntry"
   tempco = ""
   

   'codes to check for special characters
   Worksheets(strTab2).Range("A10", "Y1100").Replace What:="&", Replacement:="&", SearchOrder:=xlByColumns, MatchCase:=True
   Worksheets(strTab2).Range("A10", "Y1100").Replace What:="'", Replacement:="'", SearchOrder:=xlByColumns, MatchCase:=True
   Worksheets(strTab2).Range("A10", "Y1100").Replace What:="""", Replacement:=""", SearchOrder:=xlByColumns, MatchCase:=True
   Worksheets(strTab2).Range("A10", "Y1100").Replace What:="<", Replacement:="<", SearchOrder:=xlByColumns, MatchCase:=True
   Worksheets(strTab2).Range("A10", "Y1100").Replace What:=">", Replacement:=">", SearchOrder:=xlByColumns, MatchCase:=True
   
   Sheets(strTab).Select
   Cells.Select
   Selection.ClearContents

   
   Worksheets(strTab).Cells(1, 1) = "<?xml version=""1.0"" encoding=""UTF-8""?>"
   Worksheets(strTab).Cells(2, 1) = "<ReceiptReturnImport>"

      Worksheets(strTab).Cells(intRowC, 2) = "<ReceiptReturnRequest batchID=""" & Worksheets(strTab2).Cells(intRowS2, 2) & """  batchDate=""" & Worksheets(strTab2).Cells(intRowS3, 2) & """>"
      intRowC = intRowC + 1
      Worksheets(strTab).Cells(intRowC, 2) = "<Login>"
      intRowC = intRowC + 1
      Worksheets(strTab).Cells(intRowC, 2) = "<UserID>" & Worksheets(strTab2).Cells(intRowS5, 2) & "</UserID>"
      intRowC = intRowC + 1
      Worksheets(strTab).Cells(intRowC, 2) = "<Password>" & Worksheets(strTab2).Cells(intRowS4, 2) & "</Password></Login>"
      intRowC = intRowC + 1
   Do Until (Worksheets(1).Cells(intRowS, 1)) = "End"
   If Not IsEmpty(Worksheets(1).Cells(intRowS, 1)) Then
      Worksheets(strTab).Cells(intRowC, 3) = "<PurchaseOrder poNumber=""" & Worksheets(strTab2).Cells(intRowS, 1) & """>"
      intRowC = intRowC + 1
      End If
      Worksheets(strTab).Cells(intRowC, 3) = "<Receipt packingSlipNumber=""" & Worksheets(strTab2).Cells(intRowS, 2) & """ date =""" & Worksheets(strTab2).Cells(intRowS3, 2) & """ status=""1001"">"
      intRowC = intRowC + 1
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 3)) Then
      Worksheets(strTab).Cells(intRowC, 3) = "<ReceiptLine number=""" & Worksheets(strTab2).Cells(intRowS, 3) & """  status=""1001"">"
      intRowC = intRowC + 1
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 3)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<Line>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 3)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<ItemNumber>" & Worksheets(strTab2).Cells(intRowS, 4) & "</ItemNumber>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 3)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<ReceiptReturnValue type=""quantity"">" & Worksheets(strTab2).Cells(intRowS, 5) & "</ReceiptReturnValue>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 3)) Then
      Worksheets(strTab).Cells(intRowC, 3) = "</Line></ReceiptLine>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 6)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<ReceiptLine number=""" & Worksheets(strTab2).Cells(intRowS, 6) & """  status=""1001"">"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 6)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<Line>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 6)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<ItemNumber>" & Worksheets(strTab2).Cells(intRowS, 7) & "</ItemNumber>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 6)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<ReceiptReturnValue type=""quantity"">" & Worksheets(strTab2).Cells(intRowS, 8) & "</ReceiptReturnValue>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 6)) Then
      Worksheets(strTab).Cells(intRowC, 3) = "</Line></ReceiptLine>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 9)) Then
      Worksheets(strTab).Cells(intRowC, 3) = "<ReceiptLine number=""" & Worksheets(strTab2).Cells(intRowS, 9) & """  status=""1001"">"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 9)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<Line>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 9)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<ItemNumber>" & Worksheets(strTab2).Cells(intRowS, 10) & "</ItemNumber>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 9)) Then
      Worksheets(strTab).Cells(intRowC, 4) = "<ReceiptReturnValue type=""quantity"">" & Worksheets(strTab2).Cells(intRowS, 11) & "</ReceiptReturnValue>"
      intRowC = intRowC + 1
      End If
      If Not IsEmpty(Worksheets(strTab2).Cells(intRowS, 9)) Then
      Worksheets(strTab).Cells(intRowC, 3) = "</Line></ReceiptLine>"
      intRowC = intRowC + 1
      
      Worksheets(strTab).Cells(intRowC, 4) = "</Receipt>"
      intRowC = intRowC + 1
      Worksheets(strTab).Cells(intRowC, 3) = "</PurchaseOrder>"
      intRowS = intRowS + 1
      End If
      End If
      Loop
    Worksheets(strTab).Cells(intRowC + 1) = "</ReceiptReturnRequest>"
    intRowC = intRowC + 1
    Worksheets(strTab).Cells(intRowC + 1) = "</ReceiptReturnImport>"
    intRowC = intRowC + 1
   
End Sub

Thank you.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
One cause of an overflow error is trying to assign a value greater than 32767 to an integer. A quick thing to try, if the sheet is large, is change your integer vars to longs.

Gary
 
Upvote 0
Hi,

Tried this and got the same error. I then changed the ints to strings to see if that made a differnece and got a new error:

'Run-time error '1004':

Application-defined or object defined error'

If I change back to ints I get overflow errors agin.

Please help!
 
Upvote 0
Try Long (integer) instead of Integer.
 
Upvote 0
The 3rd line of your 'codes to check for special characters seems to have extra quotes.

Maybe try using chr(34) & chr(34) instead of quoting quotes or to start maybe simply comment that line and see if the error disappears.

Gary
 
Upvote 0
Hi Gary,

No luck, still getting on overflow error.

Is there anyway for me to upload the excel file so you can see it in contex?

Thanks
 
Upvote 0
I don't have any data but the code runs and gives me an error here:

intRowC = intRowC + 1

The value of the variable (intRowC) at that point is more than an integer can hold.

Gary
 
Upvote 0
Hi,

When I change intRowC to Long from and Integer I get the 1004 error.

I'm really lost with this now.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,678
Members
449,116
Latest member
HypnoFant

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