Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help fixing "space between excel graphs" code 2

Status
Not open for further replies.

chilly442

Technical User
Jun 25, 2008
151
US
The following code puts all the charts right next to eachother. I have played around with the code, and can't seem to get them to space out.

Sub LineUpMyCharts()
Dim MyWidth As Single, MyHeight As Single
Dim NumWide As Long
Dim iChtIx As Long, iChtCt As Long

Sheets("Figs").Activate

MyWidth = 660
MyHeight = 430
NumWide = 4

iChtCt = ActiveSheet.ChartObjects.Count
For iChtIx = 1 To iChtCt
With ActiveSheet.ChartObjects(iChtIx)

'The next two lines position each chart exactly next to each other
.Left = iChtIx - 1 * MyWidth + NumWide
.Top = iChtIx - 1 * MyHeight + NumWide
End With
Next
End Sub

What would I need to change to get space between each chart?

Any help would be greatly appreciated!!!

Thanks,
Chilly442
 



Hi,

1. If they are to be "right next to eachother", whay are you varying the .Top property by
Code:
 .Top = [b]iChtIx[/b] - 1 * MyHeight + NumWide
Here's what you might want...
Code:
   dim LeftStart as single
   LeftStart = 10
              .Left = (iChtIx - 1) * (MyWidth + NumWide) + LeftStart
              [s].Top = iChtIx - 1 * MyHeight + NumWide[/s]


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
When I run your code, it puts one slide in the correct position, and then drops down and over a slide size and then puts the next slide there. Sort of like a stair step effect. Any suggestions to fix this?

Thank you for the quick response.

Sub LineUpMyCharts()
Dim MyWidth As Single, MyHeight As Single
Dim NumWide As Long
Dim iChtIx As Long, iChtCt As Long
Dim Leftstart As Single

Sheets("Figs").Activate

MyWidth = 660
MyHeight = 430
NumWide = 1

iChtCt = ActiveSheet.ChartObjects.Count
For iChtIx = 1 To iChtCt
With ActiveSheet.ChartObjects(iChtIx)
Leftstart = 10
.Left = (iChtIx - 1) * (MyWidth + NumWide) + Leftstart

End With
Next
End Sub

 
MyWidth = 660
MyHeight = 430
NumWide = 1
Leftstart = 10
For iChtIx = 1 To ActiveSheet.ChartObjects.Count
With ActiveSheet.ChartObjects(iChtIx)
.Left = (iChtIx - 1) * (MyWidth + NumWide + Leftstart)
End With
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 



You also need to TopStart
Code:
    Dim Leftstart As Single, TopStart  As Single

    Leftstart = 10
    TopStart = 10

    For iChtIx = 1 To iChtCt
       With ActiveSheet.ChartObjects(iChtIx)
            .Left = (iChtIx - 1) * (MyWidth + NumWide) + Leftstart
            .Top = TopStart
            .Width = MyWidth
            .Height = MyHeight
          End With
      Next


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Still the same thing.
should be:

1 6
2 7
3 8
4 9
5 10

getting:
1 6
2 7
3 8
4 9
5 10

I messed around with the numbers, changing Leftstart, or NumWide, but still get the same layout.

Sorry for being a pain...
Chilly442
 




Well where is the code you are using? I cannot guess!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Here it is. Sorry.
Sub LineUpMyCharts()
Dim MyWidth As Single, MyHeight As Single
Dim NumWide As Long
Dim iChtIx As Long, iChtCt As Long
Dim Leftstart As Single, TopStart As Single

Sheets("Figs").Activate

MyWidth = 660
MyHeight = 430
NumWide = 10
Leftstart = 10
TopStart = 10

For iChtIx = 1 To ActiveSheet.ChartObjects.Count

With ActiveSheet.ChartObjects(iChtIx)
.Top = TopStart
.Left = (iChtIx - 1) * (MyWidth + NumWide) + Leftstart
.Width = MyWidth
.Height = MyHeight
End With
Next

End Sub
 



This code does
[tt]
1 2 3 4 5 6 7 8 9 10
[/tt]
What code are you using to get what you stated above, ie...
[tt]
getting:
1 6
2 7
3 8
4 9
5 10
[/tt]


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Sub LineUpMyCharts()
Dim MyWidth As Single, MyHeight As Single
Dim NumWide As Long
Dim iChtIx As Long, iChtCt As Long
Dim LeftStart As Single, TopStart As Single

Sheets("Figs").Activate

MyWidth = 660
MyHeight = 430
NumWide = 10
LeftStart = 80
TopStart = 80

For iChtIx = 1 To ActiveSheet.ChartObjects.Count

With ActiveSheet.ChartObjects(iChtIx)
'.Top = TopStart
.Left = (iChtIx - 1) * (MyWidth + NumWide + LeftStart)
.Width = MyWidth
.Height = MyHeigh

End With
Next

End Sub
This was before you had suggested using the TopStart which does line the graphs along the top of the sheet.

I have 10 different units that have graphs. I need to keep each unit in its own column. Each chart/graph has a name (don't know if that will make any difference). I want the graphs to all be the same size (that part is done), and have them all spaced apart the same.
This is a better depiction of what I am looking for:

unit1 unit2 unit3
1 6 11
2 7 12
3 8 13
4 9 14
5 10 15

Thanks for taking the time to help me out. This is the last part that I need to finish.
 



Well here's what you have to do.

In multiples of 3, you have to...

1. incriment the TOP property, similar to how you're imcrimenting the LEFT property
2. reset the left property

You can get multiples of 3 using the INT & MOD functions
Code:
for i = 1 to limit
   TopOff = Int((i-1)/3)
   LeftOff = (i-1) Mod 3
next


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Like this???

Sub LineUpMyCharts()
Dim MyWidth As Single, MyHeight As Single
Dim NumWide As Long
Dim iChtIx As Long, iChtCt As Long
Dim LeftStart As Single, TopStart As Single

Sheets("Figs").Activate

MyWidth = 660
MyHeight = 430
NumWide = 10
LeftStart = 80
TopStart = 80

For iChtIx = 1 To ActiveSheet.ChartObjects.Count

TopOff = Int((iChtIx - 1) / 3)
LeftOff = (iChtIx - 1) Mod 3


With ActiveSheet.ChartObjects(iChtIx)
.Left = (iChtIx - 1) * (MyWidth + NumWide + LeftStart)
.Width = MyWidth
.Height = MyHeigh
End With

Next

End Sub
 



Code:
  For iChtIx = 1 To ActiveSheet.ChartObjects.Count
  
     TopOff = Int((iChtIx - 1) / 3)
     LeftOff = (iChtIx - 1) Mod 3
        
    
  With ActiveSheet.ChartObjects(iChtIx)[b]
      .Top = TopOff * (MyHeight + NumWide + TopStart)[/b]
      .Left = [b]LeftOff[/b] * (MyWidth + NumWide + LeftStart)
      .Width = MyWidth
      .Height = MyHeigh

  End With
  
Next


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top