I have a question regarding a Macro that I am trying to make.
What I have set up is a UserForm that I have made with two listboxes as well as a command button. The listboxes show information from 2 categories, and when you pick one of both options and hit the command button, it displays a few characteristics that each category has, like length and width. The trouble I'm having is I need to be able to find and display the dimensions of each object, however, the way I have been trying to accomplish this hasn't been working. I the method I used to find the row of the selected item is as follows
This method works, and it is stored in the above variables, and the columns for the width are the same for each category. The way I tried to find the value of the width was using this piece of code and it wasn't working.rowV1 = Application.WorksheetFunction.Match(ListBox1.Value, Range("List!A1:A50"), 0) rowT1 = Application.WorksheetFunction.Match(ListBox2.Value, Range("List!I1:I20"), 0)
I need to do the same process for the length, but I am not sure how to go about this.width1 = Cells(rowV1, 6).Value width2 = Cells(rowT1, 13).Value Cells(7, 2).Value = width1 Cells(8, 2).Value = width2
Any help is appreciated. Thank you!
Last edited by JayEmTee91; 10-11-2011 at 08:04 AM. Reason: Solved
You need to provide more information. What isn't working? Do you mean it gives you the wrong answer, or it gives you an error?
Also, where is this code? Is it the code for the userform, or a worksheet, or something else?
Can you attach your file?
Making the world a better place one fret at a time
||||||
If someone helped you, please click on the star icon at the bottom of their post
If your problem is solved, please update the first post:
EDIT, Go Advanced button, set Prefix to SOLVED
[code]
' Enclose code in tags like this
[/code]
Don't attach a screenshot--just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.
When I try to get the cell that contains the width data, using the cells(rowV1, 6).Value, my macro does not give any information. There are no errors, it does not return the value that I would like it to return. I'm a beginner to Visual Basic, so I'm not really sure what to do.
All of the code was done in Visual Basic through Excel, and it's on the command button of the UserForm.
I will attach the code.
Last edited by JayEmTee91; 10-19-2011 at 01:50 PM.
In this code
you are getting the value from column 6, which is F. That's GVWR, not width. Then you get a value from column 13, which is M, which is blank.width1 = Cells(rowV1, 6).Value width2 = Cells(rowT1, 13).Value
You also seem to confusing the two worksheets in your code. Instead of activating a sheet, use explicit qualification for each cell reference.
Also, you are using a MATCH function to look up the position of the selected item, but the ListIndex property of the list already tells you that. ListIndex starts using 0 as the first item so you have to add 1 to get the row number. Also you have a header row, so you have to add 1 again.
You also use intermediate variables where they aren't really necessary. This sometimes make code more readable, but sometimes just makes it bigger.
You seem to be loading the second listbox from the list of trailer GVWR. I think you meant to use the trailer name.
One suggestion, which I didn't implement: You may want to create named ranges for the vehicle and trailer names. Then if you change the way your data is organized, you won't have to change the listbox range source.
Here is how I would write this code, book is also attached:
Private Sub CommandButton1_Click() With Worksheets("Macro") ' Vehicle .Cells(7, 1).Value = ListBox1.Value .Cells(7, 2).Value = Worksheets("List").Cells(ListBox1.ListIndex + 2, 5).Value ' width .Cells(7, 3).Value = Worksheets("List").Cells(ListBox1.ListIndex + 2, 2).Value ' length ' Trailer .Cells(8, 1).Value = ListBox2.Value .Cells(8, 2).Value = Worksheets("List").Cells(ListBox2.ListIndex + 2, 11).Value ' width .Cells(8, 3).Value = Worksheets("List").Cells(ListBox2.ListIndex + 2, 10).Value ' length End With End Sub
That works great. Thank you so much!
I could not get the way you suggested to work given my code, but the following code allows me to do what I want.
With each click in the userform, the counter and all the information in the click is moved to another sheet called history, and since the information that is copied occupies 2 rows, i want it to skip a row and then continue onto the one after that. Each click stores history based on the number.
The code that I have messes up on the first two clicks. The first click puts the items that were copied in the first spot I want, but then the second click overwrites the first clicks copy. The third click skips a line and copies the information correctly. I do not understand why that is. My code is below:
This code is for the counter...
This code is for the copy command.Num = Sheets("Macro").Cells(7, 1).Value 'A1 change to suit If Sheets("Macro").Cells(7, 1).Value = "" Then Num = 1 Sheets("Macro").Cells(7, 1).Value = Num Else: Sheets("Macro").Cells(7, 1).Value = Num + 1 End If
Any suggestions would be appreciated!Set srang1 = Range(Cells(7, xlFirstCol), (Cells(xlLastRow, 2))) Set srang2 = Range(Cells(7, xlFirstCol + 1), (Cells(xlLastRow, xlFirstCol + 1))) Set srang3 = Range(Cells(7, xlFirstCol + 2), (Cells(xlLastRow, xlFirstCol + 2))) Set srang4 = Range(Cells(7, xlFirstCol + 3), (Cells(xlLastRow, xlFirstCol + 3))) Set srang5 = Range(Cells(7, xlFirstCol + 4), (Cells(xlLastRow, xlFirstCol + 4))) Set srang6 = Range(Cells(7, xlFirstCol + 5), (Cells(xlLastRow, xlFirstCol + 5))) Set srang7 = Range(Cells(7, xlFirstCol + 6), (Cells(xlLastRow, xlFirstCol + 6))) Set MyRange = Union(srang1, srang2, srang3, srang4, srang5, srang6, srang7) If Num = 1 Then MyRange.Copy Destination:=Sheets("Hist").Cells(3, 1) ElseIf Num > 1 Then MyRange.Copy Destination:=Sheets("Hist").Cells(CurPos(Num), 1) End If Function xlFirstCol(Optional WorksheetName As String) As Long ' find the first populated column in a worksheet If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If With Worksheets(WorksheetName) xlFirstCol = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByColumns, xlNext).Column End With End Function Function xlFirstRow(Optional WorksheetName As String) As Long ' find the first populated row in a worksheet If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If With Worksheets(WorksheetName) xlFirstRow = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByRows, xlNext).row End With End Function Function xlLastRow(Optional WorksheetName As String) As Long ' find the last populated row in a worksheet If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If With Worksheets(WorksheetName) xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByRows, xlPrevious).row End With End Function Function xlLastCol(Optional WorksheetName As String) As Long ' find the last populated column in a worksheet If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If With Worksheets(WorksheetName) xlLastCol = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByColumns, xlPrevious).Column End With End Function Function CurPos(Num) CurPos = Num * 3 End Function
With a problem like this you need to attach your whole workbook. I can't see what happens when you click a button.
I am very puzzled by this seeing as though I didn't just make a suggestion. I took your workbook, fixed it, and then posted the completed solution. What did you post if it wasn't your code?I could not get the way you suggested to work given my code....
I apologize, I posted this is in the wrong thread. I meant to post it in my other thread.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks