ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   "Object Required" (https://www.excelbanter.com/excel-programming/378299-object-required.html)

BOBODD

"Object Required"
 
As a newbie to VBA coding, I'm still strugling with the syntax. The loop
below is to fill in bank forms where each letter is in a seperate box.

Counter.Value = 0
Do
If Counter < 24 Then
ActiveCell.Offset(0, Counter) = Mid(txtBankName, Counter, 1)
Counter = Counter + 1
End If
Loop Until Counter = 24

I can't work out how to reference my counter so that the offset and the
letter change with each iteration. Any help would be appreciated.

Tim Williams

"Object Required"
 
dim i as long

for i=1 to 24
ActiveCell.Offset(0, i-1).value = Mid(txtBankName, i, 1)
next i

Tim

"BOBODD" wrote in message
...
As a newbie to VBA coding, I'm still strugling with the syntax. The loop
below is to fill in bank forms where each letter is in a seperate box.

Counter.Value = 0
Do
If Counter < 24 Then
ActiveCell.Offset(0, Counter) = Mid(txtBankName, Counter, 1)
Counter = Counter + 1
End If
Loop Until Counter = 24

I can't work out how to reference my counter so that the offset and the
letter change with each iteration. Any help would be appreciated.




JLatham

"Object Required"
 
First problem is that there is no "zero-eth" character of a string of text.
So the first time you try to get Mid(txtBankName,Counter,1) you are hitting a
wall.

Solution to that is to either initialize Counter to 1 before beginning the
loop, or to move the Counter=Counter+1 up ahead of the ActiveCell.Offset(...
statement.

Also, if Counter is just a variable used in the code, get rid of the .Value
part of Counter.Value = 0 (or use Counter=1 if you decide on that fix here).

When referencing controls on a user form, if you want to be very specific
about it, you can use a reference like Me!txtBankName



"BOBODD" wrote:

As a newbie to VBA coding, I'm still strugling with the syntax. The loop
below is to fill in bank forms where each letter is in a seperate box.

Counter.Value = 0
Do
If Counter < 24 Then
ActiveCell.Offset(0, Counter) = Mid(txtBankName, Counter, 1)
Counter = Counter + 1
End If
Loop Until Counter = 24

I can't work out how to reference my counter so that the offset and the
letter change with each iteration. Any help would be appreciated.


JLatham

"Object Required"
 
If you want the first letter of whatever is in txtBankName to be in the
active cell (column offset 0) then try this
Counter=0
Do
If Counter<24 Then
ActiveCell.Offset(0,Counter)=Mid(txtBankName,Count er+1, 1)
Counter=Counter+1
End If
Loop Until Counter = 24

"BOBODD" wrote:

As a newbie to VBA coding, I'm still strugling with the syntax. The loop
below is to fill in bank forms where each letter is in a seperate box.

Counter.Value = 0
Do
If Counter < 24 Then
ActiveCell.Offset(0, Counter) = Mid(txtBankName, Counter, 1)
Counter = Counter + 1
End If
Loop Until Counter = 24

I can't work out how to reference my counter so that the offset and the
letter change with each iteration. Any help would be appreciated.



All times are GMT +1. The time now is 03:07 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com