VBA code, Error 1004 - vba

Apollogies, I know this has been posted hundreds of times but Ive looked at them and still cant solve this
So I have a workbook (first time using vba)
first sheet is a graph
second sheet is a sheet containing values of nodes and stress
Third sheet is a sheet containing values of nodes and stress
Etc up to 17th sheet
I have a graph plot of stress vs number in sheet 1, and it contains only data from sheet 2 "stress1" and sheet 3 "stress2"
trying to make code to add the values of stress3 to my graph in sheet 1
CODE
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(x).Name = "=""Stress3"""
ActiveChart.SeriesCollection(x).Values = ThisWorkbook.Sheets("Stress3").Range("B9:B782")
Everything but the last line works, throws an error 1004 which I assume is because I have to do something to allow access to other sheets?
I have also tried
ActiveChart.SeriesCollection(x).Values = "=Stressx!$B$9:$B$782"
as the last line, same error

SOLVED
I am an idiot for putting x=3 at the top of my code, getting it working then not paying attention and removing it
the issue is the (x)
when I removed x=3 the sheets try to grab values from sheet x
which it doesnt know what x is
fixed by readding x=3 or changing the (x) to a number

Related

Copy relevant rows and paste to new worksheet

I have multiple sheets which I am looking to copy and paste the relevant cells from rows into a new worksheet. The defining factor is whether the rows are "Live" which is determined by whether today falls between the "start date" and "end date" columns. For all rows that are "live" certain cells on that row should then be copy pasted to the bottom of the list on the new worksheet. I would then repeat these steps for the other worksheets so that the new worksheet essentially contains a summary of all "live" lines from the other sheets.
I then ultimately need to create a button so I can re-run this at the end of each day.
Hoping you can help!
You've defined and explained what you want to do nicely. So the next step would be to start researching the actions you want the code to do:
Start with how to copy rows:
http://www.bluepecantraining.com/copying-moving-rows-columns-excel-vba/
Next is how to find the last used row in a worksheet:
https://powerspreadsheets.com/excel-vba-last-row/
Next is to apply your condition on what rows to copy:
Using VBA to check if a date is between two dates
Next is how to create a for loop that runs the code the desired number of times:
https://www.excel-easy.com/vba/loop.html (a tip is to use the LastRow that you defined via the second link i posted. eg: "For x = 1 To LastRow")
And lastly is how to assign a macro to a button or shape:
https://wellsr.com/vba/2017/excel/how-to-assign-a-macro-to-a-shape-in-excel/
Try to build a code with the help of these links. If you get stuck, come back and show us what you've created and where the problem is. I (aswell as and many others) would love to help you on from there.

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.

Moving page when pasting items

First of all sorry for not including any code from my attempts as I don't even know where to begin to start trial coding. What Im trying to do is I have a sheet which I have pre formatted to be my report. I then use a macro to copy certain columns from one sheet to the report sheet. I need to report sheet to be singed by somebody in the business and therefore need the bottom few rows for that. My question is how do I get the pasted columns to paste onto the next sheet if they reach a line that I decided is the last line I would like any information on. I apologise for the very vague question so please ask questions if you need any more info.
Loop Horizontal page breaks. Something like this.
For x = 1 To .HPageBreaks.Count - 1
MsgBox .HPageBreaks(x).Location.Row
'Write your rows adding your "sheet" row value to the pagebreak rows.
ws.Range("D" & lRow + .HPageBreaks(x).Location.Row).Value = "SomeValue"
Next

Link from cell in other sheet shows 0 but other sheet shows data cant get past this

Spent hours searching for any mention of this but no luck. Maybe someone here knows the answer. I developed a large application in excel. sheet1 receives data from a Com server that just places it there. This has been working for a long time with no problems.
I have sheet2 two with links from sheet 1 and formulas. Mostly I do all the processing in sheet1 change event because I got circular refernces when putting functions in a cell.
From yesterday, I have a problem that the cell in sheet 2 shows 0. When I go to the linked cell it has data. Sometimes numbers and sometimes text depending, but sheet 2 shows 0.
As well as the linked cells, I also screen scrape some data with a seperate process and place it sheet 3 to link across to other cells, so sheet2 will also contain links form this data. I spent today changing this so it places the data in directly in sheet1 instead of creating links. This is tedious and will cause other problems.
I checked options -> formulas. Right now it is set at max iterations 100; max change 100. I had reduced max iterations to stop some functions firing themselves recursively through the sheet change event. Now I raised it back to 100.
I deleted all the data saved the sheet started again and the same thing happened again.
e.g. sheet1 cell "A1" has a value of 10
sheet2 cell a1 has a formula of ='sheet1'!A1
sheet 2 cell A1 is showing 0
when I look in the formula bar I see the formula
This data was damaged down to row 42 the last row that had held data. below that the conditional formatting was the correct color even though the formulas looked identical and i could see nothing wrong to explain it, I dragged the formula from 44 upwards again and everything worked, but bizarrely, when I copied from sheet1 cells 1 to 44 and pasted a link back in, I still got 0.
When I run the system and the COM erver delivers a load and the change event fires etc, I end up back with the same problem.
Now when I add a new sheet and paste my links from sheet1 into this, it works as it should.
Right now after my attempts to fix it. All sheet1 and sheet2 columns are formatted general though previousy I had one column text and some others number. I can't control the format of sheet1 because excel just adapts to the data it receives regardless what I do.
I also keep checkng for application.enableevents and this is not the problem nor is worksheet calculate and in the options I have automatic claculation set
The only other routine running under sheet change in sheet 2 is a tidyup that clears out old data after the COM server has delivered a smaller number of rows.
It copies default data from unused cells in the sheet. I placed the right formatting and default such as blank or 0 depending on what is required. This has not in the past caused any issues and I cant see what it might do, though previously when I just tried puttng "" in the cells to be cleared that did cause nasty excel erros. but not now.
Does anyone have any ideas?
I don't know if this is still relevant, but I had the same issue and came here searching for an answer.
In my case, I realized the cell I was trying to copy was a merged cell (i.e. it provided a range) the formula was [='Sheet1'!B10:N10] and the result was 0 and not the text I wanted. so I changed the formula to ='USA-CA'!B10 and it worked.
hope this is helpful to someone else.
This little routine fixes it.
Sub fixformulas()
'fix formulas broken by excel for some reason
Cells.Replace What:="=", Replacement:="=", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
All I can work out is that when excel copies the formulas and puts them back in place it corrupts the equals sign. Everything else is fine but as oon as I replace this the lights go on.
I'd rather know how to prevent it, but I hope nobody else suffers like I have.

Copy range until empty row and paste into new sheet

I'm trying to write a VBA macro for Excel 2013. It's purpose is to merge two worksheets into a combined worksheet. (I tried to find a built in feature to do this but was unable to find what I needed).
What the macro needs to do is this:
Activate "Sheet3" and clear all rows starting with row 3 and down
Go into "Sheet1" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Go into "Sheet3" and paste those rows starting at A3.
Go into "Sheet2" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Go into "Sheet3" and past those rows starting at the first empty cell in column A.
I'm a novice at VBA but I've managed to find the following code and I'm trying to make it work to accomplish the above requirements.
Sub CreateCombinedSheet()
lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("A3", ActiveSheet.Cells(lastRow, 12)).Copy
End Sub
I'm trying to write parts of it and test it as I go but I'm already getting a 1004 error with this:
Application-defined or object-defined error
Any thoughts on how I should work this?
Thanks
Activate "Sheet3" and clear all rows starting with row 3 and down
You do not need to activate a sheet to clear the rows. You may want to see THIS You can directly say
Sheets("BlahBlah").Rows("3:200").ClearContents
I have hardcoded 200 as an example. To find the end of rows, see the below point.
Go into "Sheet1" and copy all rows starting with (A3:P3) and copy down until, and not including, the first row with null in the A column.
Same for this. You do not need to go to that sheet. You need to first find the last row. I see that you are hardcoing the rows in your code. You don't need to do that. xl2007+ now has 1048576 rows. Use .Rows.Count Please see THIS
Go into "Sheet3" and paste those rows starting at A3.
To paste, you again don't need to go to that sheet. You can directly say
rng.Copy Sheet("BlahBlah").Rows(3)
Your point 4 and 5 are just variations of the above. I am sure you can now take it from here :). In case you still face any difficulty, simply post back.