Solved

Dynamically changing Scroll area in Excel using VBA

Posted on 2010-08-29
6
759 Views
Last Modified: 2012-05-10
Hi,
I am having some difficulty dynamically changing the scroll area of my worksheets. I copy a range from one sheet to another and then I want to limit the scroll area of the final sheet. any help would be appreciated.

Thanks,
scurvylion
Sub DetailCalculations()

Dim wkmc As Worksheet, wkss As Worksheet
Dim rngLast As Range, rng As Range
Dim lrow As Long, lastrow As Long, endrow As Long, endtbl As Range

Set wkmc = Worksheets("ModelCalculations")
Set wkss = Worksheets("Menu Detail Data")
Set rngLast = wkmc.Range("A1").End(xlDown)
lrow = rngLast.Row
lastrow = lrow + 1
endtbl = lastrow + 9

    endrow = wkss.Range("A9").SpecialCells(xlCellTypeLastCell).Row
    wkss.Range(Cells(9, 1), Cells(endrow, 20)).Clear

     wkmc.Range("A1", wkmc.Cells(lastrow, 20)).Copy
    
  With wkss.Range("A9")
     .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
     .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
   end with

wkss.scrollarea = wkss.Range(Cells(1,1), Cells(endtbl, 20))

End Sub

Open in new window

0
Comment
Question by:scurvylion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 

Author Comment

by:scurvylion
ID: 33554478
Sorry, I forgot to mention that the line

wkss.scrollarea = wkss.Range(Cells(1,1), Cells(endtbl, 20))

gives me a run-time error 13: type mismatch

I've tried modifying it in multiple different ways but it still gives me an error although the error may be different than the one mentioned above.

The direct reference

wkss.ScrollArea = ("A1:T1900")

works fine but I would like to be able to dynamically control the scroll area.

Thanks

0
 
LVL 12

Expert Comment

by:tilsant
ID: 33555981
Try this one....

I have used:
wkss.scrollarea = "A1:" & Chr(64 + endtbl) & 20



Tils
Sub DetailCalculations()

Dim wkmc As Worksheet, wkss As Worksheet
Dim rngLast As Range, rng As Range
Dim lrow As Long, lastrow As Long, endrow As Long, endtbl As Long 'dim of endtbl changed to Long

Set wkmc = Worksheets("ModelCalculations")
Set wkss = Worksheets("Menu Detail Data")
Set rngLast = wkmc.Range("A1").End(xlDown)
lrow = rngLast.Row
lastrow = lrow + 1
endtbl = lastrow + 9

    endrow = wkss.Range("A9").SpecialCells(xlCellTypeLastCell).Row
    wkss.Range(Cells(9, 1), Cells(endrow, 20)).Clear

     wkmc.Range("A1", wkmc.Cells(lastrow, 20)).Copy
    
  With wkss.Range("A9")
     .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
     .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
   end with

'ScrollArea changed
wkss.scrollarea = "A1:" & Chr(64 + endtbl) & 20
End Sub

Open in new window

0
 
LVL 12

Accepted Solution

by:
tilsant earned 500 total points
ID: 33556046
My mistake!!
I got confused with the row and column numbering!

It should be this way..
wkss.scrollarea = "A1:T" & endtbl


Revised Code attached.
Tils.
Sub DetailCalculations()

Dim wkmc As Worksheet, wkss As Worksheet
Dim rngLast As Range, rng As Range
Dim lrow As Long, lastrow As Long, endrow As Long, endtbl As Long 'dim of endtbl changed to Long

Set wkmc = Worksheets("ModelCalculations")
Set wkss = Worksheets("Menu Detail Data")
Set rngLast = wkmc.Range("A1").End(xlDown)
lrow = rngLast.Row
lastrow = lrow + 1
endtbl = lastrow + 9

    endrow = wkss.Range("A9").SpecialCells(xlCellTypeLastCell).Row
    wkss.Range(Cells(9, 1), Cells(endrow, 20)).Clear

     wkmc.Range("A1", wkmc.Cells(lastrow, 20)).Copy
    
  With wkss.Range("A9")
     .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
     .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
   end with

'ScrollArea changed
wkss.scrollarea = "A1:T" & endtbl
End Sub

Open in new window

0
Industry Leaders: 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!

 

Author Comment

by:scurvylion
ID: 33558199
Awesome job!

Thanks bro. That works great - I think I must have tried every combination but that one.

scurvylion
0
 

Author Closing Comment

by:scurvylion
ID: 33558209
Thanks again - much appreciated!
0
 
LVL 12

Expert Comment

by:tilsant
ID: 33559255
Thanks for the Grade, Scurvylion! :)


Tils.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question