VBA crashing after autofill line of code - vba

I have a set of code below. The "NPPA_Alive_Row" is a string = 60000. Before the autofill line, the underlying workbooks will save and close fine (I have stepped through this). However after this line, no open workbooks will close or save (I have tried calculating the workbook in question after this line at still doesn't work)
WB_Checking_NPPA_Alive.Sheets("NPPA_Alive").Cells(2, 81) = "=VLOOKUP(B2,NPPA_Alive.xlsx!B2:BR" & NPPA_Alive_Row & ",68)"
WB_Checking_NPPA_Alive.Worksheets("NPPA_Alive").Activate
WB_Checking_NPPA_Alive.Worksheets("NPPA_Alive").Range("CC2").Select
Range("CC2").AutoFill Destination:=Range("CC2:CC" & NPPA_Alive_Row)
Does anyone have any ideas to why this is?

Without seeing more code, it's hard to say for sure. If the file is pretty big, the processing time to calculate 60k vlookups might be what's causing the crash. If you manually copy the vlookup down, does it crash too?
You could simplify those 4 lines of code a few ways, which may or may not help
Sheets("NPPA_Alive").Range(Sheets("NPPA_Alive").Cells(2, 81), Sheets("NPPA_Alive").Cells(NPPA_Alive_Row, 81)).Formula = "=VLOOKUP(B2,NPPA_Alive.xlsx!$B$2:$BR$" & NPPA_Alive_Row & ",68)"
or
WB_Checking_NPPA_Alive.Sheets("NPPA_Alive").Range("CC2:CC" & NPPA_Alive_Row).Formula = "=VLOOKUP(B2,NPPA_Alive.xlsx!$B$2:$BR$" & NPPA_Alive_Row & ",68)"

Related

"checked" statement causing error on a vba macro for excel

So i need to add a formula to an entire column using a macro,
so far i have:
Range("S2:S" & lastRow).Formula = "=IF(TRIM(N2)="checked",M2,IF(LEN(P2)>=60, P2, IF(LEN(SUBSTITUTE(R2,CONCATENATE(" ", $J$2),""))>=60,SUBSTITUTE(R2,CONCATENATE(" ", $J$2),""),R2)))"
Which is supposed to take several values of different cells and form a new one, the problem is that the code sends an error "Syntax Error" but if i put the formula in a cell in excel the formula works perfectly.
Any idea on what could be causing the error? i think it could be linked with how its writen and how vba reads it.
In VBA you must double-up all the quotes inside, viz (there might be other issues, I haven't checked). I expect a google search would have solved this for you.
Range("S2:S" & lastRow).Formula = "=IF(TRIM(N2)=""checked"",M2,IF(LEN(P2)>=60, P2, IF(LEN(SUBSTITUTE(R2,CONCATENATE("" "", $J$2),""""))>=60,SUBSTITUTE(R2,CONCATENATE("" "", $J$2),""""),R2)))"

Cannot use Application.Evaluate() for CUBEVALUE function

Dear MS Excel wizards =)
Recently I'm developing a UserForm for Budget file, to bring all basic controls to one place and ease the life of users. My initial idea was calculating all primary values (starting num of Students, finishing num of Students...) in this form and feed to Budget file formulas as values. For some calculations, I have to address ThisWorkbookDataModel, therefore I was planning to use Application.Evaluate()
Yet it fails with case like this:
m1 = Application.Evaluate("=CUBEVALUE(""ThisWorkbookDataModel"",CUBEMEMBER(""ThisWorkbookDataModel"",{""[DimDate].[1.Year].&[" & mYear & "]"",""[DimDate].[3.MonthCalendarTrue].&[" & mMonth & "]""}),CUBEMEMBER(""ThisWorkbookDataModel"",""[Measures].[mQofStudents]""),CUBEMEMBER(""ThisWorkbookDataModel"",{""[DimCampus].[Burleigh]""}))")
As a workaround, I evaluate this formula on the worksheet first:
Range("AvgStudStartValBH").Formula = "=CUBEVALUE(""ThisWorkbookDataModel"",CUBEMEMBER(""ThisWorkbookDataModel"",{""[DimDate].[1.Year].&[" & mYear & "]"",""[DimDate].[3.MonthCalendarTrue].&[" & mMonth & "]""}),CUBEMEMBER(""ThisWorkbookDataModel"",""[Measures].[mQofStudents]""),CUBEMEMBER(""ThisWorkbookDataModel"",{""[DimCampus].[Burleigh]""}))"
Yet this really creates inconveniences in refreshing of the result in UserForm.
What I found while debugging this Application.Evaluate() issue is that if I cut away last CUBEMEMBER(), it works! So maybe Application.Evaluate() has a limit in symbols?
If so, is there any other workaround except pasting this into Excel worksheet to evaluate there before getting this to the form?

Macro to copy and change set numbers Excel

so am working on a spreadsheet to simplify logging of data. However it's quite a tedious process of copy, paste & change. Is there a way I can create a Macro to copy the following:
=IF(Trade2!I16=99,0,Trade2!I15)
Paste it the cell directly below but as
=IF(Trade3!I16=99,0,Trade3!I15)
Beyond this it needs to be copied 100 times. Am at 15 typing it manually each time and losing the will to live haha
This seems insanely simple but everything am trying is leading to a massive mess, am good at getting the spreadsheet side working but the coding isn't my strong point.
Any advice would be greatly appreciated
If you use this formula:
=IF(INDIRECT("Trade"&ROWS(A$1:A1)&"!I16")=99,0,INDIRECT("Trade"&ROWS(A$1:A1)&"!I15"))
this returns:
=IF(Trade1!I16=99,0,Trade1!I15)
But you can now copy it down, and the sheet number increases.
The ROWS formula functions as a counter as you copy it down. The INDIRECT formula returns a reference to a sheet.
not sure what you mean but try this
Sub Main()
Dim i As Long
For i = 1 To 10000
Range("A" & i).Formula = "=IF(Trade2!I" & i + 1 & "=99, 0, Trade2!I" & i & ")"
Next i
End Sub

Run-time error '-2147188160(80048240) DataType:=ppPasteEnhancedMetafile Error

Seems there is some bug. Can't resolve this problem, all code is running fine and I am able to see the AutoShape is getting copied from Excel file but it is not adding it to PowerPoint. Popping up an error Run-time error '-2147188160(80048240) View.Pastespecial : Invalid Request. The specified data type is unavailable
If Range("H" & i).Value = 1 And Range("B" & i).Value = "FRONT" Then
objPPT.Presentations(1).Slides(9).Select
objPPT.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile
Your code will be faster and possibly more reliable if you don't rely on selecting anything:
With objPPT.Slides(9).Shapes
Set objShape = .PasteSpecial(ppPasteEnhancedMetafile)(1)
With objShape
' set coordinates and such here
End With
End With
As to why you're getting the error message, try stopping the code after you've put something on the clipbard. Then switch to PowerPoint, use Paste Special to see what paste options are available. If EMF isn't one of them, that's your problem ... you're not putting anything in EMF format on the clipboard.
I had a similar issue, but I found a different solution; it may be specific to what I was doing though.
I setup a program where I would:
(manual) Copy an entire webpage that was a report on several performance metrics
(manual) Pasted it in to excel
Run the program to extract the values I want and then clear contents of the sheet I pasted them on.
Eventually after many tests, it would fail with this same automation error when I tried to access the sheet:
Sheets("PDX Paste").Activate
I was able to activate every other sheet except that particular one, even using the index value instead of the direct name reference. After googling to no success I found out that the copy and paste from the website was also pasting invisible controls. When I found this out I had 1,300+ shapes when I only expected 1 (the button I use to trigger the program). It was actually only apparent when a glitch - presumably due to so much memory being used to store these controls - displayed for a few seconds.
I ran the following code independently and then appended it to the end of my program when I do the cleanup of the data. The code goes through the sheet and deletes any shape that isn't the same type as my button. It would have to be adapted if the shapes you want to delete are the same type as the shapes you want to keep. It also becomes simpler if you don't have any shapes to keep.
Dim wsh As Worksheet
Set wsh = ActiveSheet
Dim i As Integer
For i = wsh.Shapes.Count To 1 Step -1
If wsh.Shapes(i).Type <> wsh.Shapes("UpdateDataButton").Type Then
wsh.Shapes(i).Delete
End If
Next i
I'm not sure this would solve this problem, but hopefully this can help others and prevent loss of time figuring out what may be causing this relatively vague error message.

How to speed up copying cells in Excel vba

I have this Excel VB code, and everytime after it executes this line, it pauses for half a second:
Worksheets(ws.Name).Range("A" & i & ":G" & i).Copy _
Destination:=Worksheets("Sheet1").Range("A" & emptyCell)
Can someone tell me what it is doing, and how I can prevent it from taking so long?
I have Microsoft Excel 2007 on Windows XP Pro
Update: If I paste it by hand, it waits about the same amount of time.
It all depends on the specs of the hardware (hard drive, memory, network connection, ect). If its running on a network, that will be the biggest bottleneck. There is nothing inherently wrong with the code.
You can also try speeding up the routine by turning the workbooks calculation to manual, and turning off screen updating. If the calculation is automatic it will calculate on every copy call.
It's copying a range of cells from the worksheet assigned to the variable ws and pasting it to Sheet1.
The range it is copying is from A:G in the row number assigned to the variable i.
It is pasting the range into column A in Sheet1 in the row number assigned to the variable emptyCell.
You can prevent it by prefixing each line with an apostrophe.
However, you may first wish to explore WHY the code is doing that!