Excel VBA Chart Not Recognizing Values - vba

I have another pretty perplexing problem that one of you might be able to solve. So basically I have this code to write a line graph using Excel VBA. It's made so it can be dynamic and can change if more values are added. But it's reading the first value as 0 and the last value as 0 even though they are not 0. Here is the code I'm using to put the graph on the page.
Sub createchart4()
lastA = Range("A1").End(xlDown).Row
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range("Main!$A$1:$A$" & lastA)
ActiveChart.ChartTitle.Select
ActiveChart.SeriesCollection(1).Name = "=Main!$A$1"
ActiveChart.SeriesCollection(1).Values = "=Main!$A$3:$A$" & lastA
End Sub
It's saying that A3's value is 0 when it's actually 1534 and it's also reading A10 and A11 as 0 also when they're different as well.
If you can figure this out, I would be amazed :D

The only scenario when a Chart ignores a number is when the number is stored as text. See Snapshot
How to know if the number is stored as text and how do we rectify it?
If you are using Excel 2003 then go to menu Tools | Options. Under the Error Checking Tab, ensure that the checkbox Number Stored as text is checked.
In Excel 2007/2010, click on Excel Options which can be accessed from the Office Icon in Excel 2007 or Options in the File Tab in Excel 2010.
Go to the Formulas tab and ensure that Numbers formatted as text or preceded by an apostrophe is checked.
If the above checkbox is checked then you will notice a small Green Triangle as shown in the snapshot above. Select the entire range starting from the 1st cell which has the Green Triangle. Click on the Exclamation mark. Click on Convert to number and you are done.

Related

How to bypass all Excel message boxes with VBA?

I have code that loops through sheets of data and creates charts based on each set of data. That works great. But when I added a line to the code to add a black border to each chart (j is the index used to loop through and identify the appropriate chart):
With ActiveSheet.Shapes("MyChart" & j).Line
.ForeColor.ObjectThemeColor = msoThemeColorText1
End With
Excel displays the following message box for each chart being created when I run the code:
"Complex formatting that is applied to the selected chart may take a while to display. Do you want to continue using the formatting?
Yes/No"
The code works fine if I just keep clicking "Yes" for each chart it's creating until the code is finished, but I don't want Excel to ask this at all, I want it to just go ahead with the formatting.
Everywhere I've looked in my research for how to do this says to set:
Application.DisplayAlerts = False
But this isn't working. Excel still displays the Yes/No box every time. Are there other ways to suppress messages/alerts in Excel? Why isn't the above line of code working?

PowerPoint: Repeat text on every slide

I'm working on creating a PowerPoint template for daily class presentations. In the template I'd like to have a hunk of text that is prominently displayed on the first slide and which repeats at the bottom of the subsequent slides at the bottom in a smaller size. The text will change every day.
The ideas I've had so far:
Use a text field. As far as I can tell, PowerPoint doesn't have anything like a text field that can be dynamically set.
Use a footer - this works and I can modify the master to get the look I want, but I'd really like to be picking up the value of the text from the first page so that edits would be automatically applied and to save the initial step of setting the footer.
Using VBA - I'd be willing to give this a shot, but I've never used VBA and the learning curve seems steep so it would be nice to know if the idea is feasible in VBA.
Can this be done? How would you approach it?
Here's an example of what I'm hoping to be able to do. Ideally the solution would work on both the Mac (2013) and Windows (2016) version of PowerPoint.
You can connect your presentation with an excel file. And running the code in the ppt would pull out the text in the excel file and update the titles instantly.
Create a presentation with a textbox with some temporary text. Put the below code in ppt. save as pptm.
Sub AddMotionPath()
Dim Temp As String
Excel.Application.Workbooks.Open ("D:\Users\Desktop\Book1.xlsx") ' update the path of the excel file
Workbooks("Book1.xlsx").Activate 'activate the file
For p = 1 To 4
Temp = Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("B" & p + 1).Value ' Column B has the titles
ActivePresentation.Slides(p).Shapes(1).TextFrame.TextRange.Text = Temp ' this updates the titles from excel to ppt slide
Next
Excel.Application.Workbooks("Book1.xlsx").Close False 'closes the excel file
End Sub
Let me know if this works for you. You can update the excel file and run the macro in ppt. The text in the slides will be updated automatically.

Intermittent failure using Excel VBA for chart series data in external workbook

There are two workbooks, MakePlots.xlsm and ext_data.xlsm. MakePlot contains an instruction sheet named PlotMe, a reference data set, and a number of reference plot sheets named Page 1, Page 2, etc. ext_data contains sheets of run data, e.g. run 1, run 2, ... The plot sheets contain a number of charts (i.e. 14 charts) that are arranged to print on one page to a pdf file. There is a macro in MakePlots.xlsm, that reads initialization data off of sheet PlotMe, and then the macro copies the reference sheets, and updates the series in each chart with the reference to [ext_data.xlsm]run xxx!...., correcting the source and the number of rows. In each series, the reference to Data is replaced with [ext_data.xlsm]run xxx and the number of rows is corrected. The reference sheets identify the columns to plot in each chart, along with the number of series on each chart, and x/y labels, legend labels, etc. Unfortunately, the macro occasionally doesn't work, illustrated below with pseudo code and output. Previously, the macro also copied the sheet from ext_data into MakePlots, and then the reference source was changed from Data to "Run XXX". That previously ran without error. However, I thought it would be better to reference the external worksheet instead of copying the data sheets. But now the macro doesn't always work.
Pseudo code
Loop mySrs In oChart.Chart.SeriesCollection
debug.print "Original formula: " & mySrs.Formula
... code to extract formula parts
debug.print "Reconstruct fmla: " & reconstructFmla
... code to modify series data source
debug.print "Proposed formula: " & updatedFmla
mySrs.Formula = updatedFmla <- ERROR OCCURS ON THIS LINE
debug.print "Updated formula: " & mySrs.Formula
end loop
I'm receiving errors illustrated with the following debug printing
Original formula: =SERIES(,'Data'!$B$2:$B$204,'Data'!$C$2:$C$204,1)
Reconstruct fmla: =SERIES(,'Data'!$B$2:$B$204,'Data'!$C$2:$C$204,1)
Proposed formula: =SERIES(,'[ext_data.xlsm]Run XXX'!$B$2:$B$13,'[ext_data.xlsm]Run XXX'!$C$2:$C$13,1)
NEXT LINE SHOWS THE ERROR, THE FORMULA WAS NOT COPIED INTO THE SERIES
Updated formula: =SERIES(,PlotMe!$B$2:$B$13,PlotMe!$C$2:$C$13,1)
If I go to the chart being updated, and scroll till the series formula
=SERIES(,'Data'!$B$2:$B$204,'Data'!$C$2:$C$204,1)
appears in the formula bar, and then replace with the proposed formula, the chart updates correctly. This doesn't seem to happen often, but it happens on the same chart and run. There are 14 different charts and a large number of runs. It only occurs on one of the 14 chart, and only on some of the runs. If I stop the code right when the error is going to occur, and update the chart by copying the proposed formula into the formula bar, then the code continues running without error. I'm unclear how to debug this further. How to determine why the formula string can not be updated with a formula that works in the formula bar?
Previously, the vba code was running when the external data was copied into the workbook with the charts, prior to updating the source links. But it seems redundant, and potentially error prone, to generate copies of the data for the charts.
Temporary work around. If proposed formula != update formula, then write proposed formula to a text file so that it can be copied in later, and put a text message on the plot indicating the data is incorrect.

VBA code to clear FORMATTING and leave plain text data in a chosen column in Excel

I need to create a button to add to Excel (2010 currently) that will strip all the formatting in that column and just leave plain text. Our current solution is to copy the data into Notepad to strip out the formatting and then copy it back in from Notepad. This works, but is inelegant and would be far easier if I could just create a button to do this within Excel itself. I've seen a few solutions posted but none of them seem to deal with a randomly selected column. Help!
The Range.ClearFormats method seems appropriate here.
With Worksheets("Sheet1")
.Columns(1).ClearFormats 'clear formatting from column A
End With
'for a manually selected group of cells
Selection.ClearFormats 'clear formatting from the cells currently selected
fwiw, the Clear Formats command is available on the ribbon through Home ► Editing ► Clear ► Clear Formats (Alt+H+E+F). You could easily add that command to the QAT rather than create a macro that largely duplicates the command and assign it to a custom button.
Excel already has a button on the Home tab:
Just select the entire column and click Clear Formats
Just to add to Jeeped's answer:
Using this code you'll clear the whole columns formatting (select a random cell, run this code and the whole columns formatting has been cleared)
Sub ClearColumn()
i = ActiveCell.Column
ActiveSheet.Columns(i).ClearFormats
End Sub

How do I convert a column of text URLs into active hyperlinks in Excel?

I have a column in excel, wherein I have all the website url values. My question is I want to turn the url values to active links. There are about 200 entries in that column with different urls in all cells. Is there a way I can create active hyperlinks to all the cells without writing a macro.
If you don't want to make a macro and as long as you don't mind an additional column, then just create a new column alongside your column of URLs.
In the new column type in the formula =HYPERLINK(A1) (replacing A1 with whatever cell you are interested in). Then copy the formula down the rest of the 200 entries.
NOTE: This solution does not work if the cell A1 contains a string longer than 255 characters. It results in a #VALUE! error
Create the macro as here:
On the Tools menu in Microsoft Excel, point to Macro, and then click Visual Basic Editor.
On the Insert menu, click Module.
Copy and paste this code into the code window of the module.
It will automatically name itself HyperAdd.
Sub HyperAdd()
'Converts each text hyperlink selected into a working hyperlink
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell
End Sub
When you're finished pasting your macro, click Close and Return to Microsoft Excel on the File menu.
Then select the required cells and click macro and click run.
NOTE Do NOT select the whole column! Select ONLY the cells you wish to be changed to clickable links else you will end up in a neverending loop and have to restart Excel!
Done!
Pretty easy way for rather short lists:
Double click on the box where the url is
Enter
You have your link ;)
Here's a way I found. I'm on a Mac using Excel 2011.
If column B had the text values you want to be hyperlinks, put this formula in the cell C1 (or D1 or whatever as long as it's a free column): =HYPERLINK(B1,B1)
This will insert a hyperlink with the location as the link text and the "friendly name" as the link text. If you have another column that has a friendly name for each link, you could use that too.
Then, you could hide the text column if you didn't want to see it.
If you have a list of IDs of something, and the urls were all http://website.com/folder/ID, such as:
A1 | B1
101 | http://website.com/folder/101
102 | http://website.com/folder/102
103 | http://website.com/folder/103
104 | http://website.com/folder/104
you could use something like =HYPERLINK("http://website.com/folder/"&A1,A1) and you wouldn't need the list of urls. That was my situation and worked nicely.
According to this post: http://excelhints.com/2007/06/12/hyperlink-formula-in-excel/ this method will work in Excel 2007 as well.
OK, here's a hokey solution, but I just can't figure out how to get Excel to evaluate a column of URLs as hyperlinks in bulk.
Create a formula, ="=hyperlink(""" & A1 & """)"
Drag down
Copy new formula column
Paste Special Values-only over the original column
Highlight column, click Ctrl-H (to replace), finding and replacing = with = (somehow forces re-evaluation of cells).
Cells should now be clickable as hyperlinks. If you want the blue/underline style, then just highlight all cells and choose the Hyperlink style.
The hyperlink style alone won't convert to clickable links, and the "Insert Hyperlink" dialog can't seem to use the text as the address for a bunch of cells in bulk. Aside from that, F2 and Enter through all cells would do it, but that's tedious for a lot of cells.
If adding an extra column with the hyperlinks is not an option,
the alternative is to use an external editor to enclose your hyperlink into =hyperlink(" and "), in order to obtain =hyperlink("originalCellContent")
If you have Notepad++, this is a recipe you can use to perform this operation semi-automatically:
Copy the column of addresses to Notepad++
Keeping ALT-SHIFT pressed, extended your cursor from the top left corner to the bottom left corner, and type =hyperlink(". This adds =hyperlink(" at the beginning of each entry.
Open "Replace" menu (Ctrl-H), activate regular expressions (ALT-G), and replace $ (end of line) with "\). This adds a closed quote and a closed parenthesis (which needs to be escaped with \ when regular expressions are activated) at the end of each line.
Paste back the data in Excel. In practice, just copy the data and select the first cell of the column where you want the data to end up.
I shocked Excel didn't do this automatically so here is my solution I hope would be useful for others,
Copy the whole column to clipboard
Open this on your Chrome or Firefox
data:text/html,<button onclick="document.write(document.body.querySelector('textarea').value.split('\n').map(x => '<a href=\'' + x + '\'>' + x + '</a>').join('<br>'))">Linkify</button><br><textarea></textarea>
Paste the column on the page you just opened on the browser and press "Linkify"
Copy the result from the tab to the the column on Excel
Instead step two, you can use the below page, first, click on "Run code snippet" then paste the column on it
<button onclick="document.write(document.body.querySelector('textarea').value.split('\n').map(x => '<a href=\'' + x + '\'>' + x + '</a>').join('<br>'))">Linkify</button><br><textarea></textarea>
This method works for me using the hyperlink function:
=HYPERLINK("http://"&B10,B10)
Where B10 is the cell containing the text version of the URL (in this example).
With Excel 2007 on Windows, I found these steps simplest;
Select cells with the non-active URLs
Copy
Paste as hyperlink
Create a temporary new column of hyperlinks using formula =HYPERLINK()
Copy that column into Microsoft Word (copy to clipboard only after Word is running).
Copy everything in the new word document (ctrl+a, then ctrl+c).
Paste into Excel, replacing the original column of text. Delete the temporary column with the formula.
I found that none of the methods here worked if the hyperlink did not include http:// as they linked to local locations.
I also wanted to fool-proof the script as the users would not be able to maintain it themselves and I would not be available.
It will only run on cells in a selected range if they contain a dot and no spaces. It will only run for up to 10,000 cells.
Sub HyperAdd()
Dim CellsWithSpaces As String
'Converts each text hyperlink selected into a working hyperlink
Application.ScreenUpdating = False
Dim NotPresent As Integer
NotPresent = 0
For Each xCell In Selection
xCell.Formula = Trim(xCell.Formula)
If xCell.Formula = "" Or InStr(xCell.Formula, ".") = NotPresent Then
'Do nothing if the cell is blank or contains no dots
Else
If InStr(xCell.Formula, " ") <> 0 Then
CellsWithSpaces = CellsWithSpaces & ", " & Replace(xCell.Address, "$", "")
GoTo Nextxcell
End If
If InStr(xCell.Formula, "http") <> 0 Then
Hyperstring = Trim(xCell.Formula)
Else
Hyperstring = "http://" & Trim(xCell.Formula)
End If
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=Hyperstring
End If
i = i + 1
If i = 10000 Then Exit Sub
Nextxcell:
Next xCell
If Not CellsWithSpaces = "" Then
MsgBox ("Please remove spaces from the following cells:" & CellsWithSpaces)
End If
Application.ScreenUpdating = True
End Sub
For me I just copied the entire column which has the URLs in text format into another application (say Evernote), and when they were pasted there they became links, and then I just copied them back into Excel.
The only thing here is you need to make sure the data you copy back lines up with the rest of the columns.
Try this:
=HYPERLINK("mailto:"&A1, A1)
Replace A1 with your text of email address cell.
For anyone landing here with Excel 2016, you can simply highlight the column, then click the Hyperlink tab located on the Home ribbon in the Styles box.
Edit: Unfortunately, this only updates the cell style, not the function.
You can insert the formula =HYPERLINK(<your_cell>,<your_cell>) to the adjacent cell and drag it along all the way to the bottom. This will give you a column with all the links. Now, you can select your original column by clicking on the header, right-click, and select Hide.
If you copy the text contents into a new column and use:
=HYPERLINK("http://"&B10,B10)
on your original column. Then use the $ for the column so it looks like this:
=HYPERLINK("http://"&$B10,$B10)
That's the only way it worked for me on Excel 2010 on Windows 7. You can copy down the formula.
Put the URLs into an HTML table, load the HTML page into a browser, copy the contents of that page, paste into Excel. At this point the URLs are preserved as active links.
Solution was proposed on http://answers.microsoft.com/en-us/mac/forum/macoffice2008-macexcel/how-to-copy-and-paste-to-mac-excel-2008-a-list-of/c5fa2890-acf5-461d-adb5-32480855e11e by (Jim Gordon Mac MVP)[http://answers.microsoft.com/en-us/profile/75a2b744-a259-49bb-8eb1-7db61dae9e78]
I found that it worked.
I had these URLs:
https://twitter.com/keeseter/status/578350771235872768/photo/1
https://instagram.com/p/ys5ASPCDEV/
https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-15/10881854_329617847240910_1814142151_n.jpg
https://twitter.com/ranadotson/status/539485028712189952/photo/1
https://instagram.com/p/0OgdvyxMhW/
https://instagram.com/p/1nynTiiLSb/
I put them into an HTML file (links.html) like this:
<table>
<tr><td>https://twitter.com/keeseter/status/578350771235872768/photo/1</td></tr>
<tr><td>https://instagram.com/p/ys5ASPCDEV/</td></tr>
<tr><td>https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-15/10881854_329617847240910_1814142151_n.jpg</td></tr>
<tr><td>https://twitter.com/ranadotson/status/539485028712189952/photo/1</td></tr>
<tr><td>https://instagram.com/p/0OgdvyxMhW/</td></tr>
</table>
Then I loaded the links.html into my browser, copied, pasted into Excel, and the links were active.
Thank you Cassiopeia for code. I change his code to work with local addresses and made little changes to his conditions. I removed following conditions:
Change http:/ to file:///
Removed all type of white space conditions
Changed 10k cell range condition to 100k
Sub HyperAddForLocalLinks()
Dim CellsWithSpaces As String
'Converts each text hyperlink selected into a working hyperlink
Application.ScreenUpdating = False
Dim NotPresent As Integer
NotPresent = 0
For Each xCell In Selection
xCell.Formula = Trim(xCell.Formula)
If InStr(xCell.Formula, "file:///") <> 0 Then
Hyperstring = Trim(xCell.Formula)
Else
Hyperstring = "file:///" & Trim(xCell.Formula)
End If
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=Hyperstring
i = i + 1
If i = 100000 Then Exit Sub
Nextxcell:
Next xCell
Application.ScreenUpdating = True
End Sub
I had a list of numbers that feed into url's I want hotlinked.
For example I have Column A with question numbers (i.e., 2595692, 135171) and I want to turn these question numbers into hotlinks and to display only the question numbers.
So I built a text-only hyperlink pointing to Column A, and copied it down for all my question numbers:
="=HYPERLINK("&"""http""&"":"""&""&"&"&"""//stackoverflow.com/questions/"&A1&""""&","&A1&")"
Then I copy - paste value this column of text hyperlinks to another column.
You end up with a column of text that looks like the following:
=HYPERLINK("http"&":"&"//stackoverflow.com/questions/2595692",2595692)
Then I selected these pasted items and ran the F2Entry Macro that follows:
Sub F2Enter()
Dim cell As Range
Application.Calculation = xlCalculationManual
For Each cell In Selection
cell.Activate
cell = Trim(cell)
Next cell
Application.Calculation = xlCalculationAutomatic
EndSub
I then deleted the text entry column and Column A.
I ended up with a single column of hotlinked question numbers:
2595692
135171
etc.
Cheers
On Mac, a dead easy way to do it is select your entire spreadsheet in Excel, copy, launch Numbers, paste, then select all, copy, and paste back into Excel.
Easiest way here
Highlight the whole column
click ''insert''
click ''Hyperlink''
click ''place in this document''
click ok
thats all
The simplest way in Excel 2010:
Select the column with the URL text, then select Hyperlink Style from the Home tab.
All URLs in the column are now hyperlinks.
Also double clicking each cell at the end of the URL text and adding a blank or just enter will also produce a hyperlink. Similar to the way you have to create URL links in MS Outlook emails.
There is a very simple way to do this. Create one hyperlink, and then use the Format Painter to copy down the formatting. It will create a hyperlink for every item.