• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1154
  • Last Modified:

Excel 2010 - Run Macro at current cell location

We're entering a lot of data and want to record a macro that inserts a page break at the current cell we've selected - then copies and pastes a standard header that we're using.

The header to copy is at a static location near the top (3 rows) but we don't know how to tell the macro to insert the page break and paste at the current cell that we selected.

Thanks.
0
Hank11
Asked:
Hank11
  • 11
  • 8
  • 7
1 Solution
 
Rob HensonIT & Database AssistantCommented:
Why repeat the headers??

Assuming headers in rows 1-3 select A4 and choose Freeze Panes from the Window Menu. The headers will now always be visible.

For printing, in the Page Setup select the sheet tab and select the option for rows to repeat and select header rows.

Cheers
Rob H
0
 
Hank11Author Commented:
Unfortunetly, we will have several different tables with different headers and cannot use the Repeat Row range in page setup.

Instead of inserting a static cell in VBA, can we refer to Active.cell?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Rob HensonIT & Database AssistantCommented:
Is there a particular reason why you want specific page breaks rather than letting the page breaks sort themselves out eg new page for change of reference/dept etc?

If so let us know what criteria change and then we could write a macro that adds the page breaks after all data entered.

Cheers
Rob
0
 
Rob HensonIT & Database AssistantCommented:
Your question states that you have static headers to copy to the required row. If the macro copies the headers they will be the same for each table. A copy will not give different headers unless they are formula driven.

Please be more specific, some sample data would be good.

Thanks
Rob H
0
 
Rob HensonIT & Database AssistantCommented:
In VBA ActiveCell.Row will return the row of the active cell, so yes a routine could take the row of the active cell before going off to get headers to copy and return to active row to paste.
0
 
Hank11Author Commented:
Sub Macro14()
'
' Macro14 Macro
'

'
    ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
    Range("A36:L38").Select
    Selection.Copy
    Range("CURRENT SELECTED CELL").Select
    ActiveSheet.Paste
End Sub
0
 
SiddharthRoutCommented:
Hank11: Did you see the link that I gave?

Sid
0
 
Hank11Author Commented:
Sid,
My expertise in VBA is nil and I'm limited to understaning Macros.

I'm don't know what the link is telling me.
0
 
SiddharthRoutCommented:
Ok, let me break it down for you :)

Sid
0
 
SiddharthRoutCommented:
Is this what you want?

Sub Sample()
    Dim rng As Range
    
    '~~> Assuming that your headers are in this range
    Set rng = Range("A1:B5")
    
    '~~> This will insert Pagebreak at activecell
    ActiveCell.Offset(1, 0).Select
    ActiveWindow.SelectedSheets.HPageBreaks.Add before:=ActiveCell
    ActiveWindow.SelectedSheets.VPageBreaks.Add before:=ActiveCell
    
    '~~> This will copy the header to activecell
    rng.Copy ActiveCell
End Sub

Open in new window


Sid
0
 
SiddharthRoutCommented:
If your headers are in Range("A36:L38") then the above code becomes

Sub Sample()
    Dim rng As Range
    
    '~~> Assuming that your headers are in this range
    Set rng = Range("A36:L38")
    
    '~~> This will insert Pagebreak at activecell
    ActiveCell.Offset(1, 0).Select
    ActiveWindow.SelectedSheets.HPageBreaks.Add before:=ActiveCell
    ActiveWindow.SelectedSheets.VPageBreaks.Add before:=ActiveCell
    
    '~~> This will copy the header to activecell
    rng.Copy ActiveCell
End Sub

Open in new window


Sid
0
 
Rob HensonIT & Database AssistantCommented:
The code above does what you have asked but......

With the headers still being the same, I don't see why you can't use the Repeat Rows function.

Are you printing specific chunks of the input data? Would a data extract using a filter enable the print to be accomplished?

Cheers
Rob H
0
 
Hank11Author Commented:
Sid,
I select a cell and execute the macro and goes to debug.  See attached image
Debug-Image.bmp
0
 
SiddharthRoutCommented:
Which cell are you selecting?

Sid
0
 
SiddharthRoutCommented:
I suspect that you selected the cell in the first column. Try this code.

Sub Sample()
    Dim rng As Range
    
    '~~> Assuming that your headers are in this range
    Set rng = Range("A36:L38")
    
    '~~> This will insert Pagebreak at activecell
    ActiveCell.Offset(1, 0).Select
    
    If ActiveCell.Row <> 1 Then _
    ActiveWindow.SelectedSheets.HPageBreaks.Add before:=ActiveCell
    
    If ActiveCell.Column <> 1 Then
    ActiveWindow.SelectedSheets.VPageBreaks.Add before:=ActiveCell
    
    '~~> This will copy the header to activecell
    rng.Copy ActiveCell
End Sub

Open in new window


Sid
0
 
Rob HensonIT & Database AssistantCommented:
I am guessing column A selected so can't put vpage break before column A.

Put an apostrophe at the start of that line and rerun the macro.

Cheers
Rob H
0
 
Hank11Author Commented:
The master list part of a construction design document.  It has several tables, but one in particular will have 50 sheets after the data is input.  the spreadsheet goes to a different department that will be responsible for printing it out.  The boss wants us to dumb it down so the girls printing don't have to select different worksheets fo printing.  This would have made it simple by putting each table style on a seperate worksheet and simply using the Repeat Row feature.  

I will ultimately assign the new macro to a button and give the tech an easy tool for him to insert the page break and table header.  Once he's done entering data, the macro will be deleted so the girls printing the doc won't screw things up.
0
 
SiddharthRoutCommented:
Hank, I posted the amended code above in ID: 35211525. Have a look at that and tell me if you are getting any errors?

Sid
0
 
Hank11Author Commented:
Commenting that line out worked.

What do we have to enter to the code to have the first blank cell under the new header ready to enter data.
0
 
Hank11Author Commented:
Example, if we inserted the new header in A300, we would want the cell active and ready to type in A304 etc...
0
 
SiddharthRoutCommented:
>>>>Commenting that line out worked.

You don't need to comment it :) see my post above.

>>>>What do we have to enter to the code to have the first blank cell under the new header ready to enter data.

What do you mean?

Sid
0
 
SiddharthRoutCommented:
Is this what you want?

Sub Sample()
    Dim rng As Range, ExitLoop As Boolean
    
    '~~> Assuming that your headers are in this range
    Set rng = Range("A36:L38")
    
    '~~> This will insert Pagebreak at activecell
    ActiveCell.Offset(1, 0).Select
    
    If ActiveCell.Row <> 1 Then _
    ActiveWindow.SelectedSheets.HPageBreaks.Add before:=ActiveCell
    
    If ActiveCell.Column <> 1 Then _
    ActiveWindow.SelectedSheets.VPageBreaks.Add before:=ActiveCell
    
    '~~> This will copy the header to activecell
    rng.Copy ActiveCell
    
    Do While ExitLoop = False
        If Len(Trim(ActiveCell.Value)) = 0 Then ExitLoop = True
        ActiveCell.Offset(1, 0).Select
    Loop
End Sub

Open in new window


Sid
0
 
Rob HensonIT & Database AssistantCommented:
Girls in other dept receive file and click on Print button????

Other than access to different printers and media etc, if page setup is already done what else do they need to do?

Still non comprendez but it is late!

Cheers
Rob H
0
 
Hank11Author Commented:
Outstanding solution.
Thanks so much Sid.
0
 
SiddharthRoutCommented:
Hank11: Rob also worked on this. It would be just fair to split the points with him :)

Sid
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 11
  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now