Automatically run macro when I change tabs

KlausW

Active Member
Joined
Sep 9, 2020
Messages
378
Office Version
  1. 2016
Platform
  1. Windows
Hi there are some who can get these 2 VBA codes running together. So when I affect a button I made, the VBA code must first copy the cells and paste them again. And then import the images.
Someone who can help. All help will be appreciated. Best regards Klaus W

VBA Code:
Sub Rektangelafrundedehjørner6_Klik()

 'Copy the data
Sheets("Stamdata").Range("p9:p15").Copy
Worksheets("Billeter").Range("b2").PasteSpecial Paste:=xlPasteValues


Application.CutCopyMode = False

End Sub

Sub Rektangelafrundedehjørner1_Klik()

Dim pictname As String
Dim pastehere As Range
Dim pasterow As Long
Dim x As Long
Dim lastrow As Long

lastrow = Worksheets("Billeter").Range("B1").CurrentRegion.Rows.Count
x = 2
For x = 2 To lastrow
Set pastehere = Cells(x, 1)
pasterow = pastehere.Row
Cells(pasterow, 1).Select 'This is where picture will be inserted

pictname = Cells(x, 2) 'This is the picture name
If Len(Trim(pictname)) > 0 Then
ActiveSheet.Pictures.Insert("C:\Users\k-wit\Documents\Rejseafregning\" & pictname & ".png").Select 'Path to where pictures are stored
End If

With Selection

.Left = Cells(pasterow, 2).Left + (Cells(pasterow, 3).Width - .Width / 1)
.Top = Cells(pasterow, 2).Top + (Cells(pasterow, 1).Height - .Height / 1.1)



.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 370#
.ShapeRange.Width = 470#
.ShapeRange.Rotation = 0#
End With

Next


End Sub
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi there are some who can get these 2 VBA codes running together. So when I affect a button I made, the VBA code must first copy the cells and paste them again. And then import the images.
I seem that you can obtain that by adding in the first macro the following "call" in this position:
VBA Code:
Sub Rektangelafrundedehjørner6_Klik()

 'Copy the data
Sheets("Stamdata").Range("p9:p15").Copy
Worksheets("Billeter").Range("b2").PasteSpecial Paste:=xlPasteValues

Application.CutCopyMode = False
Call Rektangelafrundedehjørner1_Klik            '<<< ADDED CALL
End Sub

Bye
 
Upvote 0
Solution
I seem that you can obtain that by adding in the first macro the following "call" in this position:
VBA Code:
Sub Rektangelafrundedehjørner6_Klik()

 'Copy the data
Sheets("Stamdata").Range("p9:p15").Copy
Worksheets("Billeter").Range("b2").PasteSpecial Paste:=xlPasteValues

Application.CutCopyMode = False
Call Rektangelafrundedehjørner1_Klik            '<<< ADDED CALL
End Sub

Thank you very much it was just as it should be, still good day. regards Klaus W
 
Upvote 0
I seem that you can obtain that by adding in the first macro the following "call" in this position:
VBA Code:
Sub Rektangelafrundedehjørner6_Klik()

 'Copy the data
Sheets("Stamdata").Range("p9:p15").Copy
Worksheets("Billeter").Range("b2").PasteSpecial Paste:=xlPasteValues

Application.CutCopyMode = False
Call Rektangelafrundedehjørner1_Klik            '<<< ADDED CALL
End Sub

Bye
Thank you very much it was just as it should be, still good day. regards Klaus W
 
Upvote 0
@KlausW - I already marked the actual answer as the solution post to this question as you also confirmed. That would be great if you could mark the solution post in your future questions instead of your last comment post unless you post your own solution. Thanks.
 
Upvote 0
thanks, I can see the point.
@KlausW - I am not sure about it because you marked your post as the solution again after I changed it.
The checkmark button is not used to close/finalize a question but to mark the best answer post that solved or helped to solve your problem in your question thread.
Please use it only for marking the answer in a question.

I changed the marked solution once again. Please keep it as is. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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