Macro Merge Question

GaryG9595

Board Regular
Joined
Jun 13, 2014
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Hello,
I recorded a macro that works perfectly, but is rather lengthy and I feel there is a shorter version that can be created. Also I would like to merge this with another macro that is currently the second step in my process.

I currently create a pivot table on a second sheet called "scorecard" from data on sheet 1 then I recorded this process.
Sub CreateSheet()
'
' CreateSheet Macro
'

'

Range("B3").Select
Selection.ShowDetail = True
Sheets("Scorecard").Select
Range("B4").Select
Selection.ShowDetail = True
Sheets("Scorecard").Select
Range("B5").Select
Selection.ShowDetail = True
Sheets("Scorecard").Select


ETC...

Range("B399").Select
Selection.ShowDetail = True
Sheets("Scorecard").Select
Range("B400").Select
Selection.ShowDetail = True
Sheets("Scorecard").Select
End Sub

<tbody>
</tbody><colgroup><col></colgroup>


Is there a way to shorten this to go ahead and click "B3,B4,B5, etc..... until it reaches an empty cell as I sometimes can have as many as 2000 or more sheets to create...?

And if possible can I combine the new macro with this one...?

This Macro obviously renames the new spreadsheets to my customers name in "B2".

Sub RenameTabs()
For i = 1 To Sheets.Count
If Worksheets(i).Range("B2").Value <> "" Then
Sheets(i).Name = Worksheets(i).Range("B2").Value
End If
Next
End Sub

Thanks in advance...
Gary
 

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.
Welcome to the forum.

The macro recorder is very verbose.

Most things that you want to do with a macro should never change the selection.

You can trim a lot of the recorded code like this:

Code:
[COLOR=#0000FF]Range("B3").Select
Selection.ShowDetail = True
Sheets("Scorecard").Select
Range("B4").Select
Selection.ShowDetail = True
Sheets("Scorecard").Select
Range("B5").Select
Selection.ShowDetail = True
Sheets("Scorecard").Select[/COLOR]

Becomes

Code:
[COLOR=#0000FF]Sheets("Scorecard").[/COLOR][COLOR=#0000FF]Range("B3").ShowDetail = True
Sheets("Scorecard").Range("B4").ShowDetail = True
Sheets("Scorecard").Range("B5").ShowDetail = True
[/COLOR]

You might even be able to do it in 1 line. I do a lot of work with pivot tables but not in this way.

Sheets("Scorecard").Range("B3:B400").ShowDetail = True

If that doesn't work, you could write a loop:
Code:
dim currRow as Long
for currRow=3 to 400
    Sheets("Scorecard").Range("B"&currRow).ShowDetail = True
next

This could be modified to find the last cell or to use a While loop to increase currRow until it finds a blank.

People will do the same thing in many different ways. Knowing the starting point and what is the goal will often change the way you do something.

Good luck
 
Upvote 0
Thank you very much...... The loop function worked perfectly...
 
Upvote 0
Glad that helped.

The macro recorder is a good way to learn about the different object and how you can work with them.

But it is VERY literal. If you scroll the screen to do something while recording, the macro will have the screen scroll. It tends to "select" everything and then work on the selection. You can usually remove those. Very rarely do my macros change the selection and depending on the macro I usually store the selection in a range before I change it and restore it when I'm done.
 
Upvote 0

Forum statistics

Threads
1,203,060
Messages
6,053,305
Members
444,651
Latest member
markkuznetsov1

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