Start a VBA-code aften another VBA-code in another sheet

KlausW

Active Member
Joined
Sep 9, 2020
Messages
453
Office Version
  1. 2016
Platform
  1. Windows
Hi all,can some help me to get this VBA code running after another VBA-code when I push a button. The VBA-code to be run is in sheet VPL.
VBA Code:
Sub ReNameTab()
Dim NewName As String
Dim OldName As String
Dim Rw As Integer
Rw = Selection.Row  '***Assumes cell within relevant row in VPL will be selected?????

'If A has value, rename Tab B as  A
If Len(Cells(Rw, 1).Text) > 1 Then
    OldName = Cells(Rw, 2)
    NewName = Cells(Rw, 1)
    GoTo DoStuff
Else
'Otherwise If B has value, rename Tab E as B
If Len(Cells(Rw, 2).Text) > 1 Then
    OldName = Cells(Rw, 5)
    NewName = Cells(Rw, 2)
End If
End If

DoStuff:
On Error Resume Next  ' ignore if sheet name cannot be found
'Change tab name
Sheets(OldName).Name = NewName
On Error GoTo 0  ' reset default error handling
End Sub

The VBA code I want it to start after is in a module. I would like to get help to make it run after this VBA code.
VBA Code:
Sub Rektangelafrundedehjørner4_Klik()

Dim ws As Worksheet
   Dim wbk As Workbook
   Dim Pth As String, Fname As String
      Pth = Range("m1") 'GetFolder()
      Fname = Dir(Pth & "\*.xls*")
   
   Do While Fname <> ""
     Set wbk = Workbooks.Open(Pth & "\" & Fname)
      For Each ws In wbk.Worksheets
         If Not ShtExists(ws.Name, ThisWorkbook) Then
            ws.Copy , ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
         End If
      Next ws
      wbk.Close False
      Fname = Dir
   Loop
   
  'Sheets("Fast besætning").Select
Call ReNameTab

End Sub

Any help will be appreciated.
Best regards
Klaus W
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
you could just call it from the first sub
VBA Code:
DoStuff:
On Error Resume Next  ' ignore if sheet name cannot be found
'Change tab name
Sheets(OldName).Name = NewName
On Error GoTo 0  ' reset default error handling
call Rektangelafrundedehjørner4_Klik
End Sub
 
Upvote 0
Hi Michael M, I think I wrote wrong. It is this VBA code that must be the start code.
VBA Code:
Sub Rektangelafrundedehjørner4_Klik()

Dim ws As Worksheet
   Dim wbk As Workbook
   Dim Pth As String, Fname As String
      Pth = Range("m1") 'GetFolder()
      Fname = Dir(Pth & "\*.xls*")
   
   Do While Fname <> ""
     Set wbk = Workbooks.Open(Pth & "\" & Fname)
      For Each ws In wbk.Worksheets
         If Not ShtExists(ws.Name, ThisWorkbook) Then
            ws.Copy , ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
         End If
      Next ws
      wbk.Close False
      Fname = Dir
   Loop
   
  'Sheets("Fast besætning").Select
Call ReNameTab

End Sub
And this is the secound code.

VBA Code:
Sub ReNameTab()
Dim NewName As String
Dim OldName As String
Dim Rw As Integer
Rw = Selection.Row  '***Assumes cell within relevant row in VPL will be selected?????

'If A has value, rename Tab B as  A
If Len(Cells(Rw, 1).Text) > 1 Then
    OldName = Cells(Rw, 2)
    NewName = Cells(Rw, 1)
    GoTo DoStuff
Else
'Otherwise If B has value, rename Tab E as B
If Len(Cells(Rw, 2).Text) > 1 Then
    OldName = Cells(Rw, 5)
    NewName = Cells(Rw, 2)
End If
End If

DoStuff:
On Error Resume Next  ' ignore if sheet name cannot be found
'Change tab name
Sheets(OldName).Name = NewName
On Error GoTo 0  ' reset default error handling
End Sub

Both codes are in sheet VPL.

Klaus W
 
Upvote 0
Ok, so I note you have change the call line ...is it working now ???
 
Upvote 0
It doesn't make any sense to run the rename macro after a macro that copies a number of workbooks an worksheets into the current workbook.
The rename macro relies on ONE row being selected in the sheet that contains the code. That seems to bear no relationship to what the other macro is doing.
If you do want to go ahead then in the Rektangelafrundedehjørner4_Klik module your Call ReNameTab looks correct but in the ReName macro you will need to add the line in Blue.

Rich (BB code):
    Me.Select
    Rw = Selection.Row  '***Assumes cell within relevant row in VPL will be selected?????
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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