rename sheet based on number format in last row for each sheet

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
Office Version
  1. 2019
hello,
I would rename sheet based on amount is existed in last row for column F when the sheet is active.
I try with this
VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim lr As Long
lr = Range("F" & Rows.Count).End(xlUp).Row
ActiveSheet.Name = Format("INVOICE" & " " & lr, "#,##0.00")
End Sub
data before
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4.00500.002,000.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
35TOTAL39.0028,485.00
SH1
Cell Formulas
RangeFormula
D35,F35D35=SUM(D22:D34)
F22:F34F22=E22*D22


OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR1.00500.00500.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR1.00460.00460.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00450.001,800.00
287GC 385/65R22.5 AT131 CHI12.001,640.0019,680.00
298KM 265/70R16 KOR4.00880.003,520.00
309KM 235/65R17 HP71 KOR1.00875.00875.00
3110KM 235/55R19 PS71 KOR2.00670.001,340.00
32TOTAL33.0032,425.00
IN1
Cell Formulas
RangeFormula
D32,F32D32=SUM(D22:D31)
F22:F31F22=E22*D22


what I want
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4.00500.002,000.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
35TOTAL39.0028,485.00
INVOICE 28,485.00
Cell Formulas
RangeFormula
D35,F35D35=SUM(D22:D34)
F22:F34F22=E22*D22


OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR1.00500.00500.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR1.00460.00460.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00450.001,800.00
287GC 385/65R22.5 AT131 CHI12.001,640.0019,680.00
298KM 265/70R16 KOR4.00880.003,520.00
309KM 235/65R17 HP71 KOR1.00875.00875.00
3110KM 235/55R19 PS71 KOR2.00670.001,340.00
32TOTAL33.0032,425.00
INVOICE 32,425.00
Cell Formulas
RangeFormula
D32,F32D32=SUM(D22:D31)
F22:F31F22=E22*D22
 
You have marked post #2 as the solution.
Does it solve your problem?
that's because my requirement in OP.;)
just I said
just curiosity if I put specific sheets in array supposes rename
I see you interest for this case that's why I gave you some examples.
this case could face in the future, that's it.
thank you for your solution.:)
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Well, the problem is if you specify the sheet name 'SH1' for example, and then the code changes its name to, say, 'INVOICE 23,000.00' and then the last value in column F changes again, nothing will happen because now sheet 'SH1' no longer exists because it is now called 'INVOICE 23,000.00'
 
Upvote 0
because now sheet 'SH1' no longer exists because it is now called 'INVOICE 23,000.00'
OMG !

this is missed me, sorry !
but what if set line in the beginning code by search for SH1,IN1 if there is no existed then will rename to SH1,IN1 before search for last row and change name to last row ,could be possible?
 
Upvote 0
but what if set line in the beginning code by search for SH1,IN1 if there is no existed then will rename to SH1,IN1 before search for last row and change name to last row ,could be possible?
Sorry, that makes no sense at all to me.
 
Upvote 0
Sorry, that makes no sense at all to me.
ok just I would share idea maybe useful ,sorry !
but I think instead of use sheets names in array could use sheets numbers in location?
I mean instead of write sheets(IN1,SH1) use sheets(1,3)
the IN1 sheet will be in first sheet in location, SH1 will be in third sheet in location
then will change based on location sheet instead of use specific sheets names.
 
Upvote 0
but I think instead of use sheets names in array could use sheets numbers in location?
I mean instead of write sheets(IN1,SH1) use sheets(1,3)
That is basically what I did in post #4. The slight change for sheets 1 and 3 instead of 1 and 2 could be

VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
  If Sh.Index = 1 Or Sh.Index = 3 Then
    On Error Resume Next
    Sh.Name = "INVOICE " & Sh.Range("F" & Rows.Count).End(xlUp).Text
  End If
End Sub

or another way, particularly if there was a lot of sheets to act on, would be

VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
  Select Case Sh.Index
    Case 1, 3   '<- Could add more here if needed
      On Error Resume Next
      Sh.Name = "INVOICE " & Sh.Range("F" & Rows.Count).End(xlUp).Text
  End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,139
Members
453,021
Latest member
Justyna P

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