Solved

Dynamically changing Scroll area in Excel using VBA

Posted on 2010-08-29
6
737 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
  • 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
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!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

756 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