Having issues exiting a table in Word using VBA - vba

I am working on a macro that, among other thing, will need a table. I am testing a macro currently but cannot figure out how to exit the table. I tried the answer from here but it didn't work. I think I must be missing something.
`
Sub Macro2()
'
With Selection
.TypeText Text:="paragraph 1"
.TypeParagraph
.TypeText Text:="paragraph 2"
.TypeParagraph
ActiveDocument.Tables.Add Range:=.Range, NumRows:=4, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With .Tables(1).Range
For x = 1 To 8
.Cells(x).Width = 180
Next
End With
' .Tables(1).Borders.Enable = False
CellFormat
.TypeText Text:="Sundance Senior Services"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Bella Pregnancy Center"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Compassion International"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Far Reaching Ministries"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="The Ram Center"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Jail/Prison Ministries"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Funeral Services"
.TypeParagraph
.TypeText Text:="paragraph 3"
End With
End Sub
Sub CellFormat()
ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:= _
False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
wdWord10ListBehavior
End Sub
Thank you so much
I tried using the solution in the referenced post which is:
`
Dim rngTable as Word.Range
Set rngTable = tbl.Range
rngTable.Collapse Word.WdCollapseDirection.wdCollapseEnd
`
just before the type paragraph and "paragraph 3" text.
It didn't help. It still put the text in the cell where "Funeral Services" is.

I got it! I got it! No sooner than I posted this and I figured out how to do it. It's hinky but it works. I added a paragraph after paragraph 2, then added a bookmark. Moved up a line, did all the table stuff then went to the bookmark. Works like a champ. The code (with the middle taken out):
With Selection
.TypeText Text:="paragraph 1"
.TypeParagraph
.TypeText Text:="paragraph 2"
.TypeParagraph
.TypeParagraph
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="docend"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
.MoveUp Unit:=wdLine, Count:=1
ActiveDocument.Tables.Add Range:=.Range, NumRows:=4, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With .Tables(1).Range
For x = 1 To 8
.Cells(x).Width = 180
Next
End With
' .Tables(1).Borders.Enable = False
CellFormat
.TypeText Text:="Sundance Senior Services"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Bella Pregnancy Center"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Compassion International"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Far Reaching Ministries"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="The Ram Center"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Jail/Prison Ministries"
.MoveRight Unit:=wdCell
CellFormat
.TypeText Text:="Funeral Services"
.GoTo What:=wdGoToBookmark, Name:="docend"
.TypeParagraph
.TypeText Text:="paragraph 3"
End With
End Sub

Here is an alternative solution for you which avoids the shenanigans of a bookmark
Sub Macro2()
'
Dim mySites As Variant
mySites = Array("Sundance Senior Services", "Bella Pregnancy Center", "Compassion International", "Far Reaching Ministries", "The Ram Center", "Jail/Prison Ministries", "Funeral Services")
Dim myRange As Range
Set myRange = Selection.Range
With myRange
.InsertAfter "paragraph 1"
.InsertParagraphAfter
.InsertAfter "paragraph 2"
.InsertParagraphAfter
.Collapse direction:=wdCollapseEnd
ActiveDocument.Tables.Add Range:=myRange, NumRows:=4, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With .Tables(1).Range
Dim x As Long
For x = 1 To 8
.Cells(x).Width = 180
Next
End With
With .Tables(1).Range.Cells(1).Range
Dim mySiteName As Variant
For Each mySiteName In mySites
.InsertBefore mySiteName
'cellformat
.Move unit:=wdCell
Next
.Collapse direction:=wdCollapseEnd
.Move unit:=wdParagraph, Count:=2
.InsertAfter "Paragraph 3"
.InsertParagraphAfter
End With
End With
End Sub

Related

wdReplaceAll is Empty

Currently I am using a for loop to lay down 4 rounds of data, I was hoping to have it replace all that are corresponding to the current word template, but doing this all in excel VBA
Unfortunately the code that I am not using is no working and the wdReplaceAll = Empty.
I've tried resetting parameters and even having .Execute Replace:=wdReplaceAll changed to .Execute Replace:=1. But Nothing in my word document is being replaced
Dim WordApp As Object
Dim DocFileName As String
Set WordApp = CreateObject("Word.Application")
DocFileName = ActiveWorkbook.Path & "\Word\TCAP Update.docx"
WordApp.Documents.Open DocFileName
For i = 1 To TCAPCount
Summary(i) = ActiveCell.Offset(0, -1).Value
Key(i) = ActiveCell.Offset(0, -2).Value
Updated(i) = Format(ActiveCell.Offset(0, 1).Value, "MM/DD/YYYY")
Cells.Find(what:="NEAR DATE", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=True).Activate
ColorInd = ActiveCell.DisplayFormat.Interior.Color
ActiveCell.Offset(0, 1).Select
Do Until ActiveCell.Value <> "NEAR DATE" And ActiveCell.DisplayFormat.Interior.Color = ColorInd
ActiveCell.Offset(0, 1).Select
Loop
ActualDate(i) = Format(ActiveCell.Value, "MM/DD/YYYY")
ActualDateNam(i) = Range(Split(ActiveCell(1).Address(1, 0), "$")(0) & "1").Value
Range(SEMCol & ActiveCell.Row).Select
Do
ActiveCell.Offset(1, 0).Select
If ActiveCell.EntireRow.Hidden = True Then
'keep looking
Else
Exit Do
End If
Loop
Next i
If TCAPCount > 1 Then
MsgBox "Please take this time to create " & TCAPCount & " extra TCAP Templates for " & SEM
WordApp.Visible = True
MsgBox "When you are ready to proceed please select OK"
End If
For i = 1 To TCAPCount
With WordApp.Selection.Find
.Text = "Key " & (i)
.Replacement.Text = Key(i)
.Execute Replace:=wdReplaceAll
.Text = "Summary " & (i)
.Replacement.Text = Summary(i)
.Execute Replace:=wdReplaceAll
.Text = "ActualDate " & (i)
.Replacement.Text = ActualDate(i)
.Execute Replace:=wdReplaceAll
.Text = "ActualDateNam " & (i)
.Replacement.Text = ActualDateNam(i)
.Execute Replace:=wdReplaceAll
.Text = "Updated " & (i)
.Replacement.Text = Updated(i)
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next i
I am hoping to replace
Key 1 | Summary 1
ActualDateNam 1– ActualDate 1
Last Update: Updated 1
Key 2 | Summary 2
ActualDateNam 2– ActualDate 2
Last Update: Updated 2
Key 3 | Summary 3
ActualDateNam 3– ActualDate 3
Last Update: Updated 3
Key 4 | Summary 4
ActualDateNam 4– ActualDate 4
Last Update: Updated 4
with it's corresponding data ie i=1 and Key (i) = "Apples", then Key 1 will be replaced as "Apples".
If you use Option Explicit, vba compiler would complain about not knowingwdReplaceAllas you use late-bound word object, what prevents you using its enums.
You tried to fix that, but who told you thatwdReplaceAll = 1?
If you read the docs you see wdReplaceAll = 2

VBA Copy multiline text from Word paste into Excel cell

My goal is to copy a multiline formatted text from Word to an Excel worksheet into one single cell using a VBA macro.
Now I've got a multiline text which needs two cells.
This is my current code:
With oWB.Worksheets("EPICS")
' Insert DESCRIPTION - todo
'
' HEADING xyz is selected, move one down and go to Pos1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
' Save current line number (BEGIN)
BeginText = Selection.Range.Information(wdFirstCharacterLineNumber)
' Go to the first table and one move up
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
Selection.MoveUp Unit:=wdLine, Count:=1
' Save current line number (END)
EndText = Selection.Range.Information(wdFirstCharacterLineNumber)
RangeToSelect = EndText - BeginText
Selection.MoveUp Unit:=wdLine, Count:=RangeToSelect, Extend:=wdExtend
Selection.Copy
.Cells(1, 1).PasteSpecial xlPasteValues
End With
This creates the following:
I would like to have the following:
Any ideas how I can handle this or any input?
Instead of
...
Selection.Copy
.Cells(1, 1).PasteSpecial xlPasteValues
...
Code
.Cells(1, 1).Value=Selection.text

Vba Word add table after copying table from Excel

The macro is written in Excel. I have copied the table to word first page from excel and then I would like to insert table into page 2 in word.But from below the set "MyRange" returning empty and table with 1 row is not creating.
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(str_path_folder & "\" & AdID & ".dotm")
objWord.Visible = True
objDoc.Activate
AppActivate AdID & ".dotm"
Sheets("Meta").Select
Range("B1:E" & LastRow).Select
Selection.Copy
objWord.Selection.PasteExcelTable False, False, False
objDoc.Tables(1).AutoFitBehavior wdAutoFitContent
objWord.Selection.InsertBreak.Type = wdPageBreak
Set MyRange = ActiveDocument.Content
MyRange.collapse Direction:=wdCollapseEnd
ActiveDocument.Tables.Add Range:=MyRange, NumRows:=1, _
NumColumns:=1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
Also if i use below it is replacing the already copied table in page 1:
objDoc.Tables.Add Range:=objDoc.Range, NumRows:=3, NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
Another way to get the end of the document is objDoc.Range(objDoc.Range.End - 1)
objDoc.Range.PasteExcelTable 0, 0, 0
objDoc.Range(objDoc.Range.End - 1).InsertBreak
objDoc.Tables.Add objDoc.Range(objDoc.Range.End - 1), 2, 2
If you record the macro in Word, you get something like this
Selection.PasteExcelTable False, False, True
Selection.InsertBreak Type:=wdPageBreak
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed

Excel to dynamic Word table

I am a VBA newb and am having an extremely difficult time trying to write some code for this solution. Any help would be greatly appreciated!
Within MS Word, I need to look in one Excel workbook across a worksheet and copy/paste the data that fits my criteria into a two-column table:
Start in Row 6 of the worksheet, look within range D6:M6. If D6:M6 is blank, then go to the next row. If any cell in D6:M6 has data, copy the data from C6 and paste it in the first row of a table (preferably merged across two columns). Then, copy the data from Row 1 of the column that has data and paste it into the table's next row (1st column). Then, copy the data from the cell that has data and paste that into the 2nd column.
Basically, if there is data, the first row of a table will come from column C of the row that has data, the next row's first column will come from Row 1 of the column that has data, and the 2nd column of the second row will come from the cell that has data within that same column.
Thank you for offering to help. Here's a hyperlink to a sample Excel file, and the very Amateurish code I've started to write within MS Word that only covers the first product:
Excel Sample File
Private Sub useVBinWord()
Dim workBook As workBook
Dim dataInExcel As String
Application.ScreenUpdating = False
Selection.TypeText Text:="Comments:"
Selection.TypeParagraph
Selection.TypeText Text:="Printed: " & Now
Selection.TypeParagraph
Set workBook = Workbooks.Open("C:\Users....xls", True, True)
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=100, NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
dataInExcel = workBook.Worksheets("Top30 Comments").Range("C6").Formula
ActiveDocument.Tables(1).Cell(1, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("D1").Formula
ActiveDocument.Tables(1).Cell(2, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("D6").Formula
ActiveDocument.Tables(1).Cell(2, 2).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("E1").Formula
ActiveDocument.Tables(1).Cell(3, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("E6").Formula
ActiveDocument.Tables(1).Cell(3, 2).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("F1").Formula
ActiveDocument.Tables(1).Cell(4, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("F6").Formula
ActiveDocument.Tables(1).Cell(4, 2).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("G1").Formula
ActiveDocument.Tables(1).Cell(5, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("G6").Formula
ActiveDocument.Tables(1).Cell(5, 2).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("H1").Formula
ActiveDocument.Tables(1).Cell(6, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("H6").Formula
ActiveDocument.Tables(1).Cell(6, 2).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("I1").Formula
ActiveDocument.Tables(1).Cell(7, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("I6").Formula
ActiveDocument.Tables(1).Cell(7, 2).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("J1").Formula
ActiveDocument.Tables(1).Cell(8, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("J6").Formula
ActiveDocument.Tables(1).Cell(8, 2).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("K1").Formula
ActiveDocument.Tables(1).Cell(9, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("K6").Formula
ActiveDocument.Tables(1).Cell(9, 2).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("L1").Formula
ActiveDocument.Tables(1).Cell(10, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("L6").Formula
ActiveDocument.Tables(1).Cell(10, 2).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("M1").Formula
ActiveDocument.Tables(1).Cell(11, 1).Select
Selection.TypeText Text:=dataInExcel
dataInExcel = workBook.Worksheets("Top30 Comments").Range("M6").Formula
ActiveDocument.Tables(1).Cell(11, 2).Select
Selection.TypeText Text:=dataInExcel
workBook.Close True
Set workBook = Nothing
Application.ScreenUpdating = True
End Sub
You've picked a difficult project to start with! Here's my almost complete solution :
Sub ImportTable()
Dim AppExcel As Excel.Application ' link to Excel
Dim ExcelRange As Excel.Range ' range in worksheet to process
Dim ExcelData As Variant ' worksheet data as VBA array
Dim ExcelHeadings As Variant ' worksheet headings as VBA array
Dim FoundCol As Boolean ' a column found with data ***
Dim exCol As Integer ' Excel column (iterator)
Dim exRow As Integer ' Excel row (iterator)
Dim wdRow As Integer ' Word table row
' reference to open instance of Excel
Set AppExcel = GetObject(class:="Excel.Application")
' change this to create an instance and open the file
Set ExcelRange = AppExcel.ActiveSheet.UsedRange ' the spreadsheet data as a range
' change this to ensure we have the correct worksheet
' the following reads cells C6 to End into a VBA array (row,column)
ExcelData = ExcelRange.Offset(5, 2).Resize(ExcelRange.Rows.Count - 6, _
ExcelRange.Columns.Count - 2)
' the following reads the heading row starting at C1
ExcelHeadings = ExcelRange.Offset(0, 2).Rows(1)
' assumes we have a blank document in word
With ActiveDocument.Range
.InsertAfter "Comments:" & vbCrLf ' insert your document header
.InsertAfter "Printed: " & Now & vbCrLf & vbCrLf
End With
Selection.EndOf wdStory ' reposition selection at end
ActiveDocument.Tables.Add Selection.Range, 1, 2 ' create a 1 x 2 table
With ActiveDocument.Tables(1) ' use this table
.Style = "Table Grid" ' set the style (copied from your code)
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
' the first row is left blank for you to insert a title
' perhaps you should make this row repeat on each page
wdRow = 2 ' we will fill from row 2 which doesn't exist yet
For exRow = 1 To UBound(ExcelData, 1) Step 3 ' process every 3rd row
FoundCol = False ' mark 'not found' ***
For exCol = 2 To UBound(ExcelData, 2) ' test each column from D
If Trim(ExcelData(exRow, exCol)) <> "" Then ' if cell not empty
If Not FoundCol Then ' first filled column, write header
.Rows.Add ' add row for header
.Rows.Add ' add row for data (avoid problem with merged row)
.Rows(wdRow).Cells.Merge ' merge header row
.Rows(wdRow).Range.InsertAfter ExcelData(exRow, 1) ' add title from C
' this keeps the two rows together across page breaks
.Rows(wdRow).Range.ParagraphFormat.KeepWithNext = True
wdRow = wdRow + 1 ' row added
FoundCol = True ' header written
Else
.Rows.Add ' add row for data
' this keeps the two rows together across page breaks
.Rows(wdRow - 1).Range.ParagraphFormat.KeepWithNext = True
End If
' write heading from row 1
.Cell(wdRow, 1).Range.InsertAfter ExcelHeadings(1, exCol)
' write found data
.Cell(wdRow, 2).Range.InsertAfter ExcelData(exRow, exCol)
wdRow = wdRow + 1 ' row added
End If
Next exCol
Next exRow
End With
' don't forget to close the instance of Excel
End Sub
Read the comments, I've left you a bit of work to do!

VBA Code Error: Chart Title in Excel

I have a sas code that writes text (Lets call is "sas text") in an excel file and then VBA creates the graph in excel and copy the "sas text" in the title.
Whenver the "sas text" length is samll the VBA works fine, but whenever the "SAS text" length is long, VBA gives a run time error - "VBA, method "text" of object error.
My code is:
Sub FormatChart()
Let Title = Sheets("Sheet1").Cells(2, 1)
Let Title1 = Sheets("Sheet1").Cells(2, 2)
Let Title2 = Sheets("Sheet1").Cells(2, 3)
Let Title3 = Sheets("Sheet1").Cells(2, 4)
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(1).Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).ReversePlotOrder = True
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartTitle.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartTitle.Text = Title & Title3 & Chr(10) & Title1 & "to " & Title2 & ": People with 25 or more visits" & Chr(10) & "Source: www.xxxxxxxxxxx.xxx.xxx"
With ActiveChart.ChartTitle.Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 8
End With
With ActiveChart.Axes(xlCategory).TickLabels.Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 7
End With
With ActiveChart.Axes(xlValue).TickLabels.Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 7
End With
With ActiveChart.PlotArea.Interior
.ColorIndex = 2
.PatternColorIndex = 1
.Pattern = xlSolid
End With
Range("S4").Select
ActiveWindow.SmallScroll Down:=48
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Legend.Select
Selection.delete
ActiveWindow.SmallScroll Down:=45
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.PlotArea.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveWindow.SmallScroll Down:=-45
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).MajorGridlines.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveWindow.SmallScroll Down:=-54
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Width = 500
ActiveChart.ChartArea.Height = 1000
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).MajorGridlines.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).MajorGridlines.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 100
ActiveChart.Axes(xlValue).MajorUnit = 20
ActiveChart.Axes(xlValue).MajorUnit = 10
With ActiveChart.SeriesCollection(1)
.Interior.Color = RGB(0, 51, 153)
End With
ActiveSheet.ChartObjects("Chart 1").Activate
Dim c As Chart
Dim s As Series
Dim iPoint As Long
Dim nPoint As Long
Set c = ActiveChart
Set s = c.SeriesCollection(1)
nPoint = s.Points.Count
For iPoint = 1 To nPoint
If s.XValues(iPoint) = "MINNESOTA STATE AVERAGE " Then
s.Points(iPoint).Interior.Color = RGB(80, 116, 77)
End If
If s.XValues(iPoint) = "NATIONAL AVERAGE " Then
s.Points(iPoint).Interior.Color = RGB(80, 116, 77)
End If
Next iPoint
ActiveSheet.ChartObjects("Chart 1").Activate
Selection.Cut
Sheets("Chart1").Select
ActiveChart.Paste
End Sub
On debugging, it highlights this line of the code
" ActiveChart.ChartTitle.Text = Title & Title3 & Chr(10) & Title1 & "to " & Title2 & ": People with 25 or more visits" & Chr(10) & "Source: www.xxxxxxxxxxx.xxx.xxx"
"
Thanks much!
The ChartTitle.Text is limited to 255 characters.
Confirmed via experimentation the following fails when x = 256.
Sub Macro2()
'
Dim x as long
With ActiveChart
.HasTitle = True
For x = 1 To 500
.ChartTitle.Text = Characters(x)
Next
End With
End Sub
Function Characters(x As long)
dim charCount as long
For charCount = 1 To x
Characters = Characters & "."
Next
End Function
My advice would be to truncate your title ChartTitle.Text = Left("your built string",255)
Several things in Excel are limited to 255 characters due to being (old style) Pascal counted strings with the length in the first byte.
To workaround this issue, you can superimpose a TextBox and populate that with your title. Note that there is still a 255 characters at a time limit when interacting, but you can build up.
The following will not work:
Sub WillNotWork()
Const LongString As String = _
"Pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164..."
Dim title As TextFrame
Set title = ActiveSheet.Shapes("Text Box 1").TextFrame
title.Characters.Text = LongString 'fails silently
End Sub
But this will:
Sub Works()
Const LongString As String = _
"Pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164..."
Dim title As TextFrame
Set title = ActiveSheet.Shapes("Text Box 1").TextFrame
title.Characters.Text = ""
For i = 0 To Len(LongString) \ 255
title.Characters(i * 255 + 1).Insert Mid$(LongString, 255 * i + 1, 255)
Next
End Sub