PDA

View Full Version : For Loop Label1 to LabelXX



matlab4lyfe
05-03-2019, 08:45 PM
I have a macro that I wrote recently to populate a table in a word document from excel data,
I used this script to pull the data from excel using labels,



Private Sub CommandButton1_Click()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook


Set exWb = objExcel.Workbooks.Open("C:\Users\Brad\Desktop\expenses.xlsx")


ThisDocument.Label1.Caption = exWb.Sheets("Sheet1").Cells(12, 2)


exWb.Close


Set exWb = Nothing
End Sub


The thing is, i'd like to loop through many labels...Labels1 to Labels36, and each time the cell location changes from 12,2 to 13,2 etc. How would I do this using an for loop or something similar?
And better yet, is there a way to create the word table on the fly? I may not know the total number of rows, it could be 12, 16 or 36 depending on the excel data. Is it possible to create a label in the loop?
Also, is it possible for the user to write down in the word document the file path, then have the vba script pull the path from there and then use it in the script?

gmayor
05-03-2019, 08:53 PM
Why not simply use mail merge to populate the labels
https://www.gmayor.com/merge_labels_with_word_2007.htm
Note that this link is good for all versions of Word.

matlab4lyfe
05-03-2019, 08:58 PM
I could, but the idea was to have an interactive document with the user

macropod
05-03-2019, 10:41 PM
What kind of interactivity? There's nothing about the code you posted that suggests any.

gmayor
05-03-2019, 11:27 PM
Interactivity? Take a look at https://www.gmayor.com/Envelope_Label_Add_In.htm which will use Excel data laid out as you require.

matlab4lyfe
05-04-2019, 05:50 AM
What kind of interactivity? There's nothing about the code you posted that suggests any.

It's also just meant to be a VBA exercise. Do you know how to use a for loop for these labels?

macropod
05-04-2019, 12:54 PM
That doesn't even go close to answering my question. Moreover, if it's "just meant to be a VBA exercise", find someone else's time to waste.

matlab4lyfe
05-04-2019, 01:13 PM
That doesn't even go close to answering my question. Moreover, if it's "just meant to be a VBA exercise", find someone else's time to waste.

There's no need to be hostile here, I'm just trying to learn a little bit more about VBA. I don't really understand much about it, and I was hoping to find some helpful kind people here who could point me in the right direction.

As far as interactivity goes, there is a piece of my code that suggests that. There's a button, that's pretty much the limit of the interaction. Also, what I meant by 'vba exercise' is, I know that I can use other methods like mail merge, but I am trying to learn VBA. So I don't want to use any other options.

I would really appreciate it if you could help me out though. I'm sure the solution is pretty simple, but for some reason I just can't for the life of me find anywhere online that states how to loop through labels in the way i've described. And i'm just really, really, not familiar with the language. Usually looping through something like that is pretty straight forward.

macropod
05-04-2019, 01:30 PM
There are probably thousands of posts here (millions if you include other forums as well) showing how to implement loops in various situations. Do a search.

matlab4lyfe
05-04-2019, 01:46 PM
There are probably thousands of posts here (millions if you include other forums as well) showing how to implement loops in various situations. Do a search.

Oh I don't doubt that there is, but there isn't any threads which are specifically answering what I'm asking.

macropod
05-04-2019, 02:55 PM
Look man, if you don't want to be of any help,you can show yourself the door.
This forum is not a free coding service for those who are too lazy to do a bit of basic research and instead prefer to waste someone else's time for their amusement. Thread closed. And kindly take your attitude elsewhere.