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:
It appears your cells C29:G29 are "center across selection" formatted.....Why ????
Change that to General and see if that does the trick.
 
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.
So where is the MIN coming from as it's not in the workbook_Open code ????
Are there any other codes running?
 
Last edited:
Upvote 0
Michael M

I could not find a MIN code any where.
So I Backed up the workbook and renamed it.
I then striped all codes out of the Bid Calculator, except Cells (C29:G29).
When I update the workbook it still gives me the MIN number in cell "C29" not the MAX of "C29:G29".
I even striped out the Conditional Formatting.
This has me baffled.
 
Upvote 0
Michael M

I reversed the sheets lines, got it to doing just what I want to do in the Bid Calculator, it finds the MAX, and puts it in "C29".
the problem now is it fills every row in "R4:R18". I just need the row of the truck number I am working from Bid Calculator "C2".


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").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29")) + 1
            Exit Sub
        End If
    Next cell
    Sheets("Legend").Cells(cell.row, 5).Offset(, 13).Value = Sheets("Legend").Cells(cell.row, 5).Offset(, 13).Value
End If
End Sub
 
Last edited:
Upvote 0
Thats what this one does !!!

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").Cells(cell.Row, 5).Offset(, 13).Value = Sheets("Legend").Cells(cell.Row, 5).Offset(, 13).Value
End If
End Sub
 
Last edited:
Upvote 0
Sorry about that in my excitement I posted the wrong.
Here is the on
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").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29"))
            Exit Sub
        End If
    Next cell
    Sheets("Legend").Cells(cell.row, 5).Offset(, 13).Value = Sheets("Legend").Cells(cell.row, 5).Offset(, 13).Value
End If
End Sub
 
Upvote 0
Sorry about that in my excitement I posted the wrong code
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").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29")) + 1
            Exit Sub
        End If
    Next cell
    Sheets("Legend").Cells(cell.row, 5).Offset(, 13).Value = Sheets("Legend").Cells(cell.row, 5).Offset(, 13).Value + 1
End If
End Sub
Except it places the number in every row of R4:R18, I just need it to place in the row that the truck number is in, not the other ones.
 
Last edited:
Upvote 0
HAve you tried the code in Post #36....but make sure you have removed the excess numbers from Column "R" FIRST !!!!!


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").Cells(cell.Row, 5).Offset(, 13).Value = Sheets("Legend").Cells(cell.Row, 5).Offset(, 13).Value
End If
End Sub
 
Last edited:
Upvote 0
Thanks Michael M

This code finds the MAX number in the Bid Calculator Cells "C29:G29 and places in in Cell "C29" after updating. Works Right.
But puts the MAX number in all the Cells of the Legend "R4:R18', Not what I want on the Legend page.
Just need it to change in the row of the truck number from that is in Cell "C2" of the Bid Calculator
I need it to do what the code in #36 does.
But it needs to put MAX number, not the MIN in Cell "C29" of the Bid Calculator.
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").Range("R4:R18").Value = WorksheetFunction.Max(Sheets("Bid Calculator").Range("C29:G29")) + 1
            Exit Sub
        End If
    Next cell
    Sheets("Legend").Cells(cell.row, 5).Offset(, 13).Value = Sheets("Legend").Cells(cell.row, 5).Offset(, 13).Value + 1
End If
End Sub

********
This is the Code that is in # 36
It works right in the Legend Cells R4:R18, but it places the MIN not the MAX in Cell "C29" of the Bid Calculator.
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").Cells(cell.row, 5).Offset(, 13).Value = Sheets("Legend").Cells(cell.row, 5).Offset(, 13).Value
End If
End Sub

Hope I explained it right.
By the way I drive a truck, that is the reason it takes me so long to answer you back.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,443
Messages
6,124,890
Members
449,194
Latest member
JayEggleton

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