I am currently facing a very anoying issue with my Excel workbook, and I would like to ask you for help.
My workbook works as a quotation tool and generates a PDF afterwards.
First, you need to enter data from different sources (for e.g. PDFs, Excel Workbooks, etc.) to a Worksheet called Master. You can do some calculation with simple formulas and can export the results into another sheet called Calc_Overview. In this process, the data is also formatted to the right font, color and size.
With ActiveWorkbook.Worksheets("Master")
For i = 12 To .UsedRange.rows.Count
If .Cells(i, 2) <> "" Then
.Cells(i, 2).Copy Destination:=Target.Cells(k, 4)
.Range(.Cells(i, 4), .Cells(i, 5)).Copy _
Target.Range("F" + k, "G" + k).PasteSpecial xlPasteValues
.Range(.Cells(i, 6), .Cells(i, 7)).Copy _
Destination:=Target.Range("I" + k, "J" + k)
.Cells(i, 8).Copy _
Destination:=Target.Cells(k, 8)
k = k + 1
End If
Next i
End With
After this operation, the user can see a summary of his entered and calculated data. With another macro, which copies the rows into 3 different worksheets (depending on the kind), the calculation process is finished.
This is my copy function :
Function CopyTable(Typ As String, Counter As Integer, Count As Integer) As Integer
With Worksheets("Calc_Overview")
.Range("A" & Count & ":" & "D" & Count).Copy _
Destination:=Sheets(Typ).Range("A" & 5 + Counter)
.Range("G" & Count & ":" & "H" & Count).Copy _
Destination:=Worksheets(Typ).Range("E" & 5 + Counter)
End With
CopyTable = Counter + 1
End Function
Now my problem:
If you try to edit the data which was transfered into the final worksheet, it often happens that Excel crashes without any reason. After editing a cell value,pressing enter or clicking another cell will let Excel crash.
I already turned off the 'Automatically Flash Fill' and 'Show Quick Analysis' option, but the problem still occurs..
Has anyone a clue what might cause this problem? I already replaced the final worksheets by new ones without copying anything from the 'broken' ones - also without any success. I am using a Windows 10 PC (x64) with an 32-Bit Microsoft Office 2013.
I am really looking fowards to any tips or solutions regarding this problem!
Many thanks
Moritz
I have an update for you:
changing the copy method does not change anything - the problem still occurs.
This is how is looks after you edit the cell - the cell expands and excel is freezing without any reason with a very high cpu load
Related
Sorry, this is another VBA Solver looping problem. I've read many of the other questions/answers posted here and elsewhere, but being new to VBA (this is the first thing I am attempting), I'm unable to pinpoint my error.
I wish to set cell Ii to 0 while changing cells Ji and Ki (keeping results), where i are rows 3 to 21.
My current code does not come up with any errors, but the results only keep on the last row of the loop- please advise! I've tried using range() and range.offset (from other examples) instead of cells(), and also setting the active worksheet to no avail.
I am using Excel 2011 for Mac.
Sub SolveTwo()
'Not sure if this is necessary
Dim row As Integer
'Begin loop
For row = 3 To 21
'Test code shows it is stepping through loop
Cells(row, "U").Value = row
'Grab starting values from other columns
Cells(row, "J").Value = Cells(row, "S").Value
Cells(row, "K").Value = Cells(row, "T").Value
'Solver Code
SolverReset
SolverOptions Precision:=1e-05
SolverOk SetCell:=Cells(row, "I").Address, _
MaxMinVal:=3, ValueOf:=0, _
ByChange:=Cells(row, "J").Address & "," & Cells(row, "K").Address, _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
'Not sure if below is necessary
'SolverSave SaveArea:=Cells(row, "J").Address & "," & Cells(row,"K").Address
Next row
End Sub
Had the same problem, i.e. only retaining the solutions to the last solver call.
It's caused by Excel for Mac's solver operating asynchronously, and the solver macro only starts once the calling code has completed. Hence the solver parameters are reset repeatedly by the calling code, but the solver doesn't run until the last iteration.
There is no solution forthcoming currently but here are two workarounds. The first one is to have two modules: a regular module calling solver once, and a second class module which fires whenever the sheet calculates (solver kicks off a re-calc when finishing), and calls the second one. Iterate back and forth in a loop. See here for great solution by J Peltier which I've admittedly not tried: solution 1
Solution 2 which I used is to call solver from an Apple Script. Here's an example. The control flow in the macro uses worksheet cells for the loop counters etc, and my macro was called by shift-opt-cmd-O. My solver usually finished in 10 sec, so I waited 15.
on run {input, parameters}
-- Click “Microsoft Excel” in the Dock.
set timeoutSeconds to 2.0
set uiScript to "click UI Element \"Microsoft Excel\" of list 1 of application process \"Dock\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Press ⇧⌥⌘O
repeat 496 times
set timeoutSeconds to 2.0
set uiScript to "keystroke \"Ø\" using {shift down, option down, command down}"
my doWithTimeout(uiScript, timeoutSeconds)
delay 15
say "done"
end repeat
return input
end run
Hope that helps!
Try something like this:
Sub SolveTwo()
Dim myRow As Integer
For myRow = 3 To 21
With Worksheets(2)
.Cells(myRow, "U") = myRow
.Cells(myRow, "J") = Worksheets(1).Cells(myRow, "S")
.Cells(myRow, "K") = Worksheets(1).Cells(myRow, "T")
End With
'add your solver code here.
Next myRow
End Sub
It will generate some results in Worksheets(2), if the sheet you are executing is the first one. Furthermore, do not use Row as a variable name, because it is used in the VBEditor.
I've created a tracking document for my workplace, but my limited knowledge of Excel has only taken me so far. Hoping there's someone out there who enjoys cleaning up messy workbooks...
I have a document with two connected worksheets: 'Annuals', covering a lot of detail about the forms I'm designing; and 'Summary', which is really intended for management use. All the information in the 'Summary' page is a cell reference to 'Annuals' and the 'Summary' page is normally password protected so that the formulas can't be overwritten. You can download a dummy copy of the larger document I'm hoping to implement: Dropbox Link We use Excel 2010 at work, although the attached document was last edited in OpenOffice Calc.
The basic function of this document is to present varying degrees of information per worksheet, with most raw information being manually provided. I would like it to be dynamic enough that the user doesn't need to understand the importance of keeping cell references - they can just press a button to add their task to the list, and the program will populate the rest behind the scenes.
Aside from the terrible way that I've visually indicated the row label "hierarchy" in column C, I'm also having issues when someone wishes to insert a new row in the 'Annuals' page; I have not found an effective way to insert a row on the 'Summary' page at the same time while maintaining dynamic formulas and formatting. It has been difficult to write a macro to do this because I only want certain cells referenced on the 'Summary' page (although I'm 100% sure there's a way to do this).
This is my attempt:
Public sAddress As String
Sub AddRow()
R = ActiveCell.Row
ActiveCell.Offset(-1).EntireRow.Insert Shift:=xlDown
Range("E" & R & ":G" & R).Copy Destination:=Range("E" & R + 1 & ":G" & R + 1)
Range("T" & R).Copy Destination:=Range("T" & R + 1)
End Sub
Sub DeleteRow()
ActiveCell.EntireRow.Delete
End Sub
Sub AddSummary()
On Error Resume Next
Worksheets("Summary").Activate
If sAddress > "" Then Sh.Range(sAddress).Select
R = ActiveCell.Row
ActiveCell.Offset(-1).EntireRow.Insert Shift:=xlDown
Range("E" & R & ":G" & R).Copy Destination:=Range("E" & R + 1 & ":G" & R + 1)
Range("T" & R).Copy Destination:=Range("T" & R + 1)
End Sub
Sub Variab()
sAddress = ActiveCell.Address
End Sub
Sub Everything()
Call AddRow
Call Variab
Call AddSummary
End Sub
Maybe a pivot table could be implemented? However, I have no idea how to make a pivot table actually work, let alone make it user-friendly or pretty. I tried - and failed - to make a pivot table that I could reference in the Summary page and hide from other users, but I didn't get very far.
I know that you're probably shuddering at my workbook, but if you can think of better methodology behind the information I'm trying to track (amongst many users, with varying degrees of computer literacy) then please let me know!
Click on Summary tab
Shift-click on Annuals tab
Insert the row.
That's it! It will insert a blank row on both sheets.
If you want to copy data from an existing row into the newly created row (as in your macro), select both tabs as above, highlight a row, copy it, and paste it into the new row. You will see that the copy command was applied to both sheets, just as if you had done the copy command on each sheet.
I'm trying to copy cells from one sheet to another without copying stuff to the clipboard but it must copy the formatting across.
Here are the ways I've tried at the moment, any ideas how to accomplish my needs?
LastRow = ThisWorkbook.Sheets("Nas").Range("A65536").End(xlUp).Row
'Option 1, works but it's using the clipboard :/
ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow).Copy
ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0).PasteSpecial
'Option 2, works but doesn't take formatting (ie text, general, time, date... etc)
Set Src = ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow)
Set Dst = ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0).Resize(Src.Rows.Count, Src.Columns.Count)
Dst.Value = Src.Value
'Option 3, works but doesn't take formatting (ie text, general, time, date... etc)
ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0) = ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow).Values
Your example code defines the formatting to be carried across as "text, general, time, date... etc". While the number formatting (a subset of the Properties of a cell) can easily be accommodated, delving further into enumerating the vast number of properties and subproperties of a Cells object is counter-productive when all relevant (non-default) properties can be carried across easily with a copy/paste operation using the clipboard.
With ThisWorkbook.Sheets("Nas")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets("Test").Range("A6:F" & LastRow).Offset(-5, 0) = _
.Range("A6:F" & LastRow).Value
ThisWorkbook.Sheets("Test").Range("A6:F" & LastRow).Offset(-5, 0).NumberFormat = _
.Range("A6:F" & LastRow).NumberFormat
End With
Note that transferring the value of the cell across is performed with .Value or .Value2 not .Values.
I have seen this question popup occasionally and IMHO, the source of the question is likely a) a teacher that thinks this is a cutesy way to get students to appreciate just how many properties, subproperties and internal sub-subproperties a Range.Cells object (Range Members (Excel)) contains or b) some idiot with a fresh MBA that wants to prove they are smarter than the IT department. Enumerating through every possible formatting property a cell could contain is just a fool's errand (again IMHO) when the clipboard is available.
If you do attempt this yourself, don't forget Conditional Formatting and Comments, both of which can easily be attributed as part of a cell's formatting.
I'm trying to get this code to work for summing cells:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum("
&Worksheets("Sheet1").Range("A3").Offset(2*i,j).Address & ":" &
Worksheets("Sheet1").Range("A7").Offset(2*i,j).Address & ")"
It keeps giving me the right cells but from the wrong sheet. So for the first iteration I get sum(A3:A7) in cell C3 of Sheet2 but the A3:A7 stays referenced to Sheet2 not Sheet1.
Thanks!
You need to specify the name of the sheet in the formula too. Your code will work if you write it like this:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum(Sheet1!" & _
Worksheets("Sheet1").Range("A3").Offset(2 * i, j).Address & ":" & _
Worksheets("Sheet1").Range("A7").Offset(2 * i, j).Address & ")"
Try this code - it uses the External:=True parameter of .Addressto retrieve the full address. While this also includes the workbook name, Excel will remove this automatically so you end up with Sheet1!A3. Also note that I used the range A3:A7 as source as .Address can handle multi-cell ranges and you don't need to take care of it manually:
Sheets("Sheet2").Range("C3").Offset(i, j).Formula = "=SUM(" & _
Sheets("Sheet1").Range("A3:A7").Offset(2 * i, j).Address(External:=True) & ")"
Be aware that hard coding references such as A3 can lead to bugs in the long run, as the user (or even the developer at some stage) might modify the sheet structure. It is best practice to use named ranges, i.e. create a named range for each cell/range you refer to and then access it in VBA with SheetX.Range("rngStartCell") or similar!
I'm having some trouble with VLOOKUP in my VBA. Here's an example of the code I'm using:
Sub Macro15()
'
' Macro15 Macro
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range("B1:B" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-1],'https://internal_sharepoint_address
/[Vendor_Information.xlsx]Sheet1'!R3C3:R150C18,4,FALSE)"
Range("C1:C" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-2],'https://internal_sharepoint_address
/[Vendor_Information.xlsx]Sheet1'!R3C3:R150C18,5,FALSE)"
With Range("B1:C" & LR)
.Value = .Value
End With
End Sub
The problem is that the values in Columns B & C (the VLOOKUP formulas) return a value of #N/A.
However, if I stop the code before converting the formula to values (the "With Range("B1:C" & LR)" line), the VLOOKUP formula returns the correct values.
Also strange - if I clear the contents of Columns B & C and re-run the above code, the values return fine. If I try to add a second cycle to the VBA, however, it does NOT work.
Any wisdom that anyone can provide would be a huge help. I've been stuck on this for a long time, and I'm just at my wit's end.
Thanks all,
David
You'll probably need to add in a step that runs a calculation cycle before you try to replace with the value:
Application.Calculate
Edit from comment: I would imagine that retrieving lookup data from a linked workbook on a Sharepoint site would take awhile. Maybe add some delay loops? Can you make two separate macros (one ending with the formulas, and a second one starting at the Paste Values), and run them separately with a pause in between?