Help rewriting VB Code:

Get_Involved

Active Member
Joined
Jan 26, 2007
Messages
383
How do I get this code to work?

Code:
Private Sub Workbook_Open()
 Dim Ans As String
 Ans = MsgBox("Update Invoice Number (???)", vbYesNo + vbInformation)
 If Ans = vbYes Then
 
 With Sheets("Expense Report").Range("H4")
 .Value = .Value + 1
 
 With Sheets("Legend").Range("Q4")
 .Value = .Value + 1
 
 With Sheets("Legend").Range("R4")
 .Value = . Value + MAX('Bid Calculator'!C29:G29)

Range R4 is the one I am trying to get to work.

I have the code in "ThisWorkbook"
 
Last edited:
Michael M Thanks

Got a question for you

Is there some code we can put into that code that keeps the other cells in "R4: R18" from advancing +1 except when Cell "C2" of the Bid Calculator has a truck number in it?
Code:
 Private Sub Workbook_Open()
Dim Ans As String
Ans = MsgBox("Update Invoice Number (???)", vbYesNo + vbInformation)
If Ans = vbYes Then
Sheets("Legend").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29")) + 1
End If
End Sub

This Code works except it does all the cells in the colum at the same time weather I am using the truck or not.
I placed a new copy in the drop box, https://www.dropbox.com/s/725f6hfi84p2efi/DUMMY NAME.xlsm?dl=0
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Maybe this...don't have Excel at the moment !!

Code:
Private Sub Workbook_Open()
Dim Ans As String
Ans = MsgBox("Update Invoice Number (???)", vbYesNo + vbInformation)
If Ans = vbYes Then
    If Sheets("Bid Calculator").Range("C2").Value = "" Then Exit Sub
    Sheets("Legend").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29")) + 1
End If
End Sub
 
Last edited:
Upvote 0
When I place a truck number in Cell "C2" of the Bid Calculator.

How can this formula be rewrit to find the number from Cell C2 of the Bid Calculator, in the "Legend" Cells "E4:E18" then update + 1 only the cell of the same row, of "R4:R18”?

Here is the code I have
Code:
Private Sub Workbook_Open()
Dim Ans As String
Ans = MsgBox("Update Invoice Number (???)", vbYesNo + vbInformation)
If Ans = vbYes Then
    If Sheets("Bid Calculator").Range("C2").Value = ("Legend""E4:E18") Then Exit Sub
    Sheets("Legend").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29")) + 1
End If
End Sub
The numbers in the sheet "Legend" Row R4:R18 represent the numbers of loads each truck has hauled.

https://www.dropbox.com/s/725f6hfi84p2efi/DUMMY NAME.xlsm?dl=0
 
Upvote 0
try

Code:
Private Sub Workbook_Open()
Dim Ans As String,r as long
Ans = MsgBox("Update Invoice Number (???)", vbYesNo + vbInformation)
If Ans = vbYes Then
    For Each r In Sheets("Legend").Range("E4:E18")
        If r = Sheets("Bid Calculator").Range("C2").Value Then Exit Sub
    Next r
    Sheets("Legend").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29")) + 1
End If
End Sub
 
Last edited:
Upvote 0
Thanks Michael M
It is giving me this message.

Compile error:
For Each Control variable must be Variant or Object
 
Upvote 0
Sorry...my mistake

Code:
Private Sub Workbook_Open()
Dim Ans As String
Ans = MsgBox("Update Invoice Number (???)", vbYesNo + vbInformation)
If Ans = vbYes Then
    For Each cell In Sheets("Legend").Range("E4:E18")
        If cell.Value = Sheets("Bid Calculator").Range("C2").Value Then Exit Sub
    Next cell
    Sheets("Legend").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29")) + 1
End If
End Sub
 
Last edited:
Upvote 0
It is working now.
The problem it is updating each row with the same number all through from e4 to e18.
can we make it just update the truck number in example "C2" 4346, find 4346 in the Legend and update R5 only, and not the others?
 
Upvote 0
Try

Code:
Private Sub Workbook_Open()
Dim Ans As String, cell As Range
Ans = MsgBox("Update Invoice Number (???)", vbYesNo + vbInformation)
If Ans = vbYes Then
    For Each cell In Sheets("Legend").Range("E4:E18")
        If cell.Value = Sheets("Bid Calculator").Range("C2").Value Then
            Sheets("Legend").Cells(cell.Row, 5).Offset(, 13).Value = Sheets("Legend").Cells(cell.Row, 5).Offset(, 13).Value + 1
            Exit Sub
        End If
    Next cell
    Sheets("Legend").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29")) + 1
End If
End Sub
 
Upvote 0
Thanks Michael M
The Code is working like I want, except it is only updating "C29", not the MAX "C29:G29".
Sometimes there may be only 1 load in "C29" and the other columns will be empty, or maybe 2 or more or all of the columns, depending on how many loads will be on that trip.
Thank You
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,381
Messages
6,130,294
Members
449,570
Latest member
TomMacca52

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top