User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 392,076 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,034 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser:
Views: 2153 | Replies: 3
Reply
Join Date: Aug 2006
Posts: 4
Reputation: mikefromchitown is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mikefromchitown mikefromchitown is offline Offline
Newbie Poster

Help Excel Form - For/Next - Naming Convention Problem

  #1  
Aug 28th, 2006
Hi,

I'm hoping that someone can help me out. The Range statement below has an issue with the right hand side of the equal sign. If I leave out the "& i", the information in the first text box is copied into all of the cells. However, I want to copy info from multiple text boxes. Does anyone see what I'm doing wrong?

Thanks,
Mike


 
Private Sub Save_Click()
'Transfers all values from Userform to Excel sheet
For i = 1 To 10
Range("PWTitle" & i).Value = ProgramWork.PWTitle & i.Text
Next i
End Sub
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 107
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Excel Form - For/Next - Naming Convention Problem

  #2  
Aug 28th, 2006
It might be a bit more helpful if you can attach the excel document (as a .zip if necessary). One problem, is that you are trying to reference a property of an integer, which is not an object. For example, the variable i is going to be a sequence of numbers starting at 1, and ending at 10. So, the first time through the loop, I will be 1 then 2, etc.

So, even though you are trying to concatenate ProgramWork.PWTitle and the number referenced by i. VB still thinks you are trying to get the .text property for the number represented by i. So, it flips out. You MIGHT be able to make it work like this Range("PWTitle" & i).Value = ProgramWork.PWTitle & i & .Text but I'm not sure that will fly either. Let me know how it turns out, or attach the excel document.
Reply With Quote  
Join Date: Aug 2006
Posts: 4
Reputation: mikefromchitown is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mikefromchitown mikefromchitown is offline Offline
Newbie Poster

Re: Excel Form - For/Next - Naming Convention Problem

  #3  
Aug 29th, 2006
It didn't work for me, but I've attached the code if you would like to take a look.

Thanks,
Mike
Attached Files
File Type: zip DataForm.ZIP (9.3 KB, 5 views)
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 107
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Excel Form - For/Next - Naming Convention Problem

  #4  
Sep 3rd, 2006
Sorry it has taken so long to respond, I've had a lot on my plate....

The problem here, is that what you want to do, requires a control array. VBA (Excel) doesn't allow control arrays, and therefore means you have to modify your for loop.

An array, is a variable that has many slots for values. I try to describe the simplest form of an array, like an egg carton, with 1 row for eggs. There is only 1 egg carton (1 variable, say X) but a number of places to put eggs (called an index). So, The variable X can be like this:
X(0) = "hello"
X(1) = "goodbye"
X(2) = "some more stuff"
Well, you can do the same thing with controls (at least, in VB4, 5 and 6). You can make a control array (which would be many controls, all with the same name), in two ways. 1, is to put a control on the form, with the same name as another control (lets say, you add two command buttons, command1 and command2, then change command2's name to command1) and VB will notice there is already a command1, and ask you if you want to make a control array. The other way, is when you add a control to the form, change it's "index" property to 0. Then, whenever you name a control the same name, it will just change the index of that control.

The bad news, is VBA (the VB that comes packaged with office [ie excel]) does NOT support this. So, instead of using a traditional "for loop", we are going to use a modified version, called "for each". That basically does the same thing as a for loop, but it will automagically calculate how many times it needs to loop (ya know, for each item involved). If you change your code to this:
For Each ctrl In DataForm.Controls
    MsgBox ctrl.Text
Next ctrl
it should pop up a messagebox with all the values..... now just replace the msgbox with your range code, and it should work alright.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 12:16 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC