|
-
Aug 2nd, 2012, 05:30 AM
#1
Thread Starter
Junior Member
How to add value to a combobox which is on a worksheet (not form)
"My problem is that I cant give value to a combobox in the way that I want to!
"code place was here"
This works fine, if I run the program from the VB editor, but if a user opens the xls document, there will be no options to choose from in the combobox.
How can I do that, when the user opens, those items are already in the combobox?
I'm beginner to this, but I don't have time to learn just for the beginnings!"
I figured out that problem. I had to put the code into the Private Sub Workbook_Open() section!
Now another question:
I have this xls file (teszt2-.zip) where the user have to enter, the author, year of publications etc, and then with a record button i have to copy those cells value to the Recorded items section (Like A4 cell value to A9, B4 value to B9, C4 value to C9, and D4 value to D9)
How is it possible?
I can do it for one record like this:
Code:
Range("A9").Value = Range("A4").Value
Range("B9").Value = Range("B4").Value
Range("C9").Value = Range("C4").Value
Range("D9").Value = ComboBox1.Value
But this is not good because I have to do it as much as the user hits the record button.
So every of the new record have to be in a new line, with every button hit!
Last edited by Gülredy; Aug 2nd, 2012 at 06:13 AM.
-
Aug 2nd, 2012, 05:53 AM
#2
Re: How to add value to a combobox which is on a worksheet (not form)
place code in workbook open event?
or use text property to put 1st item, then additems in comboboxdropbuttonclick event if list count below number of items to load
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 2nd, 2012, 07:12 AM
#3
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
I've figured it out myself, but I did as the same as you told. Can you answer my second question? I don't wanted to make another topic for that!
-
Aug 2nd, 2012, 09:59 AM
#4
Re: How to add value to a combobox which is on a worksheet (not form)
I'm not clear on exactly what you want to do (2nd question). Please explain further.
-
Aug 2nd, 2012, 10:34 AM
#5
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
-
Aug 2nd, 2012, 10:37 AM
#6
Re: How to add value to a combobox which is on a worksheet (not form)
I understand now. Will put something together shortly.
-
Aug 2nd, 2012, 11:13 AM
#7
Re: How to add value to a combobox which is on a worksheet (not form)
Try something like this (done in Excel 2010):
Code:
Sub Button1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim rngCopy As Range
Dim rngPaste As Range
Dim nextRow As Long 'next open row to copy into
Set wb = Workbooks("copy data.xlsm") 'the name of my workbook
Set ws = wb.Sheets("sheet1") 'name of my sheet
Set rngCopy = ws.Range("a4", "d4") 'where to copy from
nextRow = Range("a" & ws.Rows.Count).End(xlUp).Row + 1
Set rngPaste = ws.Range("a" & nextRow) 'where to copy to
rngCopy.Copy
rngPaste.PasteSpecial
End Sub
-
Aug 2nd, 2012, 11:19 AM
#8
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
I've got a runtime error '9' Subscript out of range at this line
Set wb = Workbooks("copy teszt2-.xlsm") (teszt2-.xlsm is the name of my file)
-
Aug 2nd, 2012, 11:26 AM
#9
Re: How to add value to a combobox which is on a worksheet (not form)
"Set wb = Workbooks("copy teszt2-.xlsm") (teszt2-.xlsm is the name of my file) "
If your file is "teszt2-.xlsm" then you should have this:
Code:
set wb = workbooks("teszt2-.xlsm")
-
Aug 2nd, 2012, 11:31 AM
#10
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
That was the first thing I did , but still the same error.
Even renamed my file to test2.xlsm but same error at same line :S
Also renamed sheet1 to Munka1 (because in my language its called this)
Code:
Set wb = Workbooks("copy test2.xlsm") 'the name of my workbook
Set ws = wb.Sheets("Munka1") 'name of my sheet
Set rngCopy = ws.Range("a4", "d4") 'where to copy from
nextRow = Range("a" & ws.Rows.Count).End(xlUp).Row + 1
Set rngPaste = ws.Range("a" & nextRow)
Last edited by Siddharth Rout; Aug 2nd, 2012 at 12:50 PM.
Reason: Code Tags Added
-
Aug 2nd, 2012, 11:40 AM
#11
Re: How to add value to a combobox which is on a worksheet (not form)
Is the word "copy" part of your file name, or no?
If no, this:
Code:
Set wb = Workbooks("copy test2.xlsm") 'the name of my workbook
should be this:
Code:
Set wb = Workbooks("test2.xlsm") 'the name of my workbook
-
Aug 2nd, 2012, 11:43 AM
#12
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
Ohh it works! At least it copies the first three values!
But it don't Copy the value one from the combo box! 
And another problem that it copies the formation too (the green background).
Can you deal with these problems too? It has to be this specific! But dont misunderstand me, I'm already greatful for your help!
-
Aug 2nd, 2012, 11:52 AM
#13
Re: How to add value to a combobox which is on a worksheet (not form)
PasteSpecial can take an argument, for example:
Code:
Worksheets("Munka2").Range("A1:D10000").PasteSpecial xlPasteValues
will paste the VALUE, not the format or formula.
I don't know what you mean by this:
"But it don't Copy the value one from the combo box!"
-
Aug 2nd, 2012, 12:01 PM
#14
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
This is what I'm talk about!
It doesent copies the Kategória's value for me!
And it even copies the format settings of the cells (but its not even a problem)
-
Aug 2nd, 2012, 12:25 PM
#15
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
-
Aug 2nd, 2012, 12:51 PM
#16
Re: How to add value to a combobox which is on a worksheet (not form)
Here is some changed code for your Command Button 1:
Code:
Private Sub CommandButton1_Click()
'Rögzítés button
'Mezõk kitöltésének vizsgálata:
If Len(Range("A4")) = 0 Then
MsgBox "Adjon meg egy Szerzõt!"
Cancel = True
Exit Sub
End If
If Len(Range("B4")) = 0 Then
MsgBox "Adjon meg egy Címet!"
Cancel = True
Exit Sub
End If
If Len(Range("C4")) = 0 Then
MsgBox "Adjon meg a Kiadási évet!"
Cancel = True
Exit Sub
End If
If ComboBox1.ListIndex < 0 Then
MsgBox "Nem választott kategóriát"
Cancel = True
Exit Sub
End If
'---------------------------------------------
'Rögzítés folyamata
Dim comboValue As String '*********************************
Dim wb As Workbook
Dim ws As Worksheet
Dim rngCopy As Range
Dim rngPaste As Range
Dim nextRow As Long 'next open row to copy into
comboValue = ComboBox1.Value 'retrieve value of combo box *****
Set wb = Workbooks("munka.xlsm") 'the name of my workbook
Set ws = wb.Sheets("Munka1") 'name of my sheet
Set rngCopy = ws.Range("a4", "c4") 'where to copy from
nextRow = Range("a" & ws.Rows.Count).End(xlUp).Row + 1
Set rngPaste = ws.Range("a" & nextRow) 'where to copy to
rngCopy.Copy
rngPaste.PasteSpecial xlPasteValues 'changed this *********
ws.Range("d" & nextRow).Value = comboValue 'use value from combo ****
'--------------------------------------------
End Sub
-
Aug 2nd, 2012, 01:06 PM
#17
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
NICE!
Thank you very much for your time! It works very good!
I added a little code snippet becuse the data which is written into by the user, have to be deleted every time I hit the Rögzít button!
I don't want to be ungreatful, but could I ask a little more of your help?
There's the export button, which should export all the data from A9 : D9 and everything which is under it (like A10 : D10, A11 : D11 etc until theres data) from the Munka1 (alias sheet1). (in a simple way, every data which is under the dark green line)
I have a codesnippet for it but it only saves the first line from there to a file:
Code:
Private Sub CommandButton3_Click()
'Export button
Dim ce As Range
Open "D:\export_from_xls.txt" For Append As #1
fileba = ""
For Each ce In Range("A9:D9")
fileba = fileba & ce.Value & ";"
Next ce
Print #1, fileba
Close #1
'--------------------------------------------
End Sub
Thanks again for your help! I'm really in your debt!
Last edited by Gülredy; Aug 2nd, 2012 at 01:14 PM.
-
Aug 2nd, 2012, 01:24 PM
#18
Re: How to add value to a combobox which is on a worksheet (not form)
No problem!
To read all the lines for export (newly added code has the 5 stars behind it):
Code:
Dim lastRow As Long '*****
Dim rngExport As Range '*****
comboValue = ComboBox1.Value 'retrieve value of combo box
Set wb = Workbooks("munka.xlsm") 'the name of my workbook
Set ws = wb.Sheets("Munka1") 'name of my sheet
Set rngCopy = ws.Range("a4", "c4") 'where to copy from
nextRow = Range("a" & ws.Rows.Count).End(xlUp).Row + 1
Set rngPaste = ws.Range("a" & nextRow) 'where to copy to
rngCopy.Copy
rngPaste.PasteSpecial xlPasteValues 'changed this
ws.Range("d" & nextRow).Value = comboValue 'use value from combo
lastRow = ws.Range("a" & ws.Rows.Count).End(xlUp).Row '*****
Set rngExport = ws.Range("a9", "d" & lastRow) '*****
For Each ce In rngExport '*****
'code here '*****
Next ce '*****
-
Aug 2nd, 2012, 01:40 PM
#19
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
So it have to look like this for me?
Code:
Dim lastRow As Long
Dim rngExport As Range
lastRow = ws.Range("a" & ws.Rows.Count).End(xlUp).Row
Set rngExport = ws.Range("a9", "d" & lastRow)
Open "D:\export_from_xls.txt" For Append As #1
fileba = ""
For Each ce In rngExport
fileba = fileba & ce.Value & ";"
Next ce
Print #1, fileba
Close #1
Because it have to be under the export button for me! So its code have to be seperated from the one which we worked before!
Last edited by Gülredy; Aug 2nd, 2012 at 01:44 PM.
-
Aug 2nd, 2012, 01:48 PM
#20
Re: How to add value to a combobox which is on a worksheet (not form)
If you want to loop through each CELL in the export range, yes.
If you wanted to loop through "LINES," then no.
For example, the first "CE" would have the first "Szerző" in it, the next would have the first "Cím" and so on, until you got to the last filled row in the range.
-
Aug 2nd, 2012, 01:57 PM
#21
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
So then what should I write to the place where you put the comment "'code here" ?
Sorry but I'm a beginner in this langueage!
-
Aug 2nd, 2012, 08:33 PM
#22
Re: How to add value to a combobox which is on a worksheet (not form)
I'm not sure how the output needs to be / should be. I haven't done too much with appending to text files, but will check it out. Can you show what you'd like the output to look like?
-
Aug 2nd, 2012, 10:41 PM
#23
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
It have to bee look like this:
Szerző1;Cím1;Kiadáséve1;Kategória1;
Szerző2;Cím2;Kiadáséve2;Kategória2;
-
Aug 3rd, 2012, 12:07 AM
#24
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
I have a solution here:
Code:
Open "d:\export_from_xls.txt" For Append As 1#
fileba = ""
Sheets("Munka1").Select
For Each ce In Range("a9:d9" & Range("d65536").End(xlUp).Row)
If Len(ce) > 0 Then
fileba = fileba & ce.Offset(0, 0) & ";"
End If
Next ce
Print #1, fileba
Close #1
But it writes all in one line!
I need every record in a new line
-
Aug 3rd, 2012, 11:29 AM
#25
Re: How to add value to a combobox which is on a worksheet (not form)
Maybe change to something like this:
Code:
Private Sub CommandButton3_Click()
'Export button
Dim ce As Range
Dim strTemp As String '*****
Open "D:\export_from_xls.txt" For Append As #1
fileba = ""
For Each ce In Range("A9:D9") 'this range needs to be changed to something variable that would end up being "a9:d21" for example ***
If ce.Row = 9 And ce.Column = 1 Then
strTemp = ce.Value & ";"
ElseIf ce.Column <> 4 Then
strTemp = strTemp & ce.Value & ";"
ElseIf ce.Column = 4 Then
strTemp = strTemp & ce.Value & ";" & vbCrLf
End If
Next ce
fileba = strTemp
Print #1, fileba
Close #1
'--------------------------------------------
End Sub
-
Aug 3rd, 2012, 02:29 PM
#26
Thread Starter
Junior Member
Re: How to add value to a combobox which is on a worksheet (not form)
I've figured it out, so I got the solution in the morning!
So the whole project is solved.
Thanks for all your help and your time!
I'm greatful!
Have a nice day!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|