VBA code stops working at 53rd column

Euler271

New Member
Joined
Dec 4, 2017
Messages
31
I have VBA code in Access that creates an Excel workbook and fills a worksheet based on values in Access tables. The code fills five columns per state. (I have a state table that lists all 51 states, including DC.) My code stops working with "Error 1004 Method of 'Range 'of object'_Worksheet' failed" message. It always stops working when trying to insert something in column "BA", for some reason. In other words, in works just fine for the first two alphabets (A->Z and AA->AZ) but mysteriously stops with the error message in column "BA".

Does anyone have a clue why this is happening? I really need this to work. Thanks.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I have VBA code in Access that creates an Excel workbook and fills a worksheet based on values in Access tables. The code fills five columns per state. (I have a state table that lists all 51 states, including DC.) My code stops working with "Error 1004 Method of 'Range 'of object'_Worksheet' failed" message. It always stops working when trying to insert something in column "BA", for some reason. In other words, in works just fine for the first two alphabets (A->Z and AA->AZ) but mysteriously stops with the error message in column "BA".

Does anyone have a clue why this is happening? I really need this to work. Thanks.
Would you please send the .
Code:
PasteSpecial
line?
 
Last edited by a moderator:
Upvote 0
This is the line that fails for column BA:

xlWs.Range(ConvertToLetter(i + 4) & 7).FormulaR1C1 = "Per Visit"

There's nothing unusual about this line. The previous line, which always works, is:

xlWs.Range(ConvertToLetter(i + 3) & 7).FormulaR1C1 = "CMS Rate"


​I don't have any named ranges and I'm only using one worksheet. Thanks for responding.
 
Upvote 0
This is the line that fails for column BA:

xlWs.Range(ConvertToLetter(i + 4) & 7).FormulaR1C1 = "Per Visit"

There's nothing unusual about this line. The previous line, which always works, is:

xlWs.Range(ConvertToLetter(i + 3) & 7).FormulaR1C1 = "CMS Rate"


​I don't have any named ranges and I'm only using one worksheet. Thanks for responding.
First, use
Code:
.Value = "Per Visit"
instead of
Code:
.FormulaR1C1 = "Per Visit"

Second, would you please send the custom
Code:
ConvertToLetter()
function?
 
Last edited by a moderator:
Upvote 0
Changing "FormulaR1C1" to "Value" didn't help but you were right to ask about my ConvertToLetter function. That's where the problem is. Here's the code:

Function ConvertToLetter(iCol As Integer) As String
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function

I got this code off some website but I just tested it and it doesn't work past the number 52. I'm going to try changing my code to reference "cells" and not bother with converting to column letters. In other words, I'll try using:

xlWs.Cells(i + 4) & 7).Value = "Per Visit"

Thank you so much for showing me where the problem lay.
 
Upvote 0
Changing "FormulaR1C1" to "Value" didn't help but you were right to ask about my ConvertToLetter function. That's where the problem is. Here's the code:

Function ConvertToLetter(iCol As Integer) As String
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function

I got this code off some website but I just tested it and it doesn't work past the number 52. I'm going to try changing my code to reference "cells" and not bother with converting to column letters. In other words, I'll try using:

xlWs.Cells(i + 4) & 7).Value = "Per Visit"

Thank you so much for showing me where the problem lay.
You look familiar with VBA code. You chould write your own codes always instead of ready-functions ;)
 
Upvote 0
If you use Cells instead of Range then you can use the column number and won't need the ConvertToLetter function.
Code:
xlWs.Cells(7, i+4).Value = "Per Visit"
xlWs.Cells(7, i+3).Value = "CMS Rate"
 
Upvote 0
Actually, I found a new function:

Function ConvertToLetter(ColumnNumber As Long) As String
Dim n As Long
Dim c As Byte
Dim s As String
n = ColumnNumber
Do
c = ((n - 1) Mod 26)
s = Chr(c + 65) & s
n = (n - c) \ 26
Loop While n > 0
ConvertToLetter = s
End Function

that works with any column number. The reason I didn't want to use xlWs.Cells...value is that I wasn't sure how that would work with something like this:

xlWs.Range(ConvertToLetter(i + 4) & "8:" & ConvertToLetter(i + 4) & xlWs.Cells(xlWs.Rows.Count, 1).End(xlUp).row).FormulaR1C1 = "=RC[-1]*RC[-2]"


I'm sure it's probably easy but the new conversion function works perfectly.

Thanks to everyone who responded.
 
Upvote 0
I'll post this anyway.
Code:
With xlWs
    .Range(.Cells(8, i+4), .Cells(.Cells(xlWs.Rows.Count, 1).End(xlUp).Row, i+4).FormulaR1C1 = "=RC[-1]*RC[-2]"
End With
 
Upvote 0
Thanks for the post. I tried your code but I'm getting a "Syntax Error" message. Is there a missing ")" somewhere?
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,707
Members
452,667
Latest member
vanessavalentino83

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