What exactly is the function of Application.CutCopyMode property in Excel - vba

Well first of all I found couple of answers while googling but most of the forums are blocked in my Office Network hence asking this question here! One more intention is to get an answer in plain English :P
I understand if we set Application.CutCopyMode = False then the Copied/Cut results will be vanished (i.e. memory will be cleared) but when should we use this and when not to use this? Can anyone please help?

By referring this(http://www.excelforum.com/excel-programming-vba-macros/867665-application-cutcopymode-false.html) link the answer is as below:
Application.CutCopyMode=False is seen in macro recorder-generated code when you do a copy/cut cells and paste . The macro recorder does the copy/cut and paste in separate statements and uses the clipboard as an intermediate buffer. I think Application.CutCopyMode = False clears the clipboard. Without that line you will get the warning 'There is a large amount of information on the Clipboard....' when you close the workbook with a large amount of data on the clipboard.
With optimised VBA code you can usually do the copy/cut and paste operations in one statement, so the clipboard isn't used and Application.CutCopyMode = False isn't needed and you won't get the warning.

Normally, When you copy a cell you will find the below statement written down in the status bar (in the bottom of your sheet)
"Select destination and Press Enter or Choose Paste"
Then you press whether Enter or choose paste to paste the value of the cell.
If you didn't press Esc afterwards you will be able to paste the value of the cell several times
Application.CutCopyMode = False does the same like the Esc button, if you removed it from your code you will find that you are able to paste the cell value several times again.
And if you closed the Excel without pressing Esc
you will get the warning 'There is a large amount of information on the Clipboard....'

There is a good explanation at
https://stackoverflow.com/a/33833319/903783
The values expected seem to be xlCopy and xlCut according to xlCutCopyMode enumeration (https://msdn.microsoft.com/en-us/VBA/Excel-VBA/articles/xlcutcopymode-enumeration-excel), but the 0 value (this is what False equals to in VBA) seems to be useful to clear Excel data put on the Clipboard.

Related

How to find active cells range in excel VBA

I am trying to open multiple webpage tab from my selected cells. I would like to find the selected cell (based on the mouse) starting and ending row and column information for further use of vba macro.
Thanks in advance...
You can do this:
x = ActiveCell.Address
MsgBox (x)
but really, you should try to avoid using selection where possible. The reason for this is because users can (and I've found, will) click in to other spreadsheets as the code is running and so what you have intended as the selection, may no longer be the actual selection. It also affects the longevity of the code because it's much more difficult to fix if something breaks.

Pasting same cell on different rows as value is replaced

This sort of follows up from my previous few questions on the same workbook.
I have two sheets, the first being Car Search, which contains a form (NOT a VBA form, just a normal table that appears like a form) to fill in. The second sheet is Raw Data, and will contain all the information entered in the Car Search sheet. It will be displayed row by row (see 2nd image).
In the Raw Data sheet, I am using the formula =""&'Car Search'!B3 to copy the contents of cell B3 in the Car Search spreadsheet.
My question is: If I had a new Car ID value, how can that automatically be entered into the row below?
Essentially, I am trying to use the form to capture all data for new cars coming in, and then I would like all that data to appear in the second sheet in their respective rows/columns.
Any help much appreciated! :)
EDIT:
Good news!
You need to use VBA:
Sub Range_Copy_Examples()
Worksheets("Car Search").Range("B3").Copy Worksheets("Raw Data").Range("=OFFSET(Sheet3!B1,0,0,COUNTA(B1:B300)+1,1)")
End Sub
However there is a small bug where it keeps pasting across past values, so I have suggested an alternative macro below
Re-edit:
A more mechanical way to make macro work (hopefully someone can improve on it) looks like this:
Sub Macro1()
Sheets("Car Search").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Raw Data").Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Worksheets("Car Search").Activate
End Sub
This works as follows:
in Car Search, have cell B3 selected the whole time
in Raw Data, select the cell with the last value in the column where you want you Car Search!B3 data pasted (you only have to do this once)
press Run on the macro (easy enough to just record one yourself)
go back to Car Search and change the value of B3, press Ctrl-Enter to keep the same cell selected after changing value, then press the same macro button without changing a thing.
If someone could add so that when pressing Ctrl-Enter on Car Search!B3 the Raw Data gets automatically added without having to manually run the macro, it would be fully automated!

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.

Blank Fields Copied as #N/A

I am working with an Excel 2003 VBA script that copies the content from worksheets in an external Excel file to worksheets in our own:
ThisWorkbook.Sheets(strTarget).UsedRange.Value = Workbooks(strFile).Sheets(strSource).UsedRange.Value
In a large number of blank cells, this function is posting #N/A instead of the blank values, which causes errors further down in the VBA code. This only occurs on some sheets, while others copy over just fine, sometimes by adding these values in extra rows and other times in extra columns. I have attempted to clear the error with IsNA to no avail:
If (IsNA(Workbooks(strFile).Sheets(strSource).UsedRange.Value)) Then
ThisWorkbook.Sheets(strTarget).UsedRange.Value = ""
Else: ThisWorkbook.Sheets(strTarget).UsedRange.Value = Workbooks(strFile).Sheets(strSource).UsedRange.Value
Is there a simple way to remove these #N/A values during the copy or even clear them after the fact?
Any other advise on how to handle this issue would also be greatly appreicated, as I'm not a VBA dev myself, but simply the lucky soul who got to pick this up when the only person with Excel and VBA experience left our team. Thanks in advance!
The problem comes when the ranges aren't the same size. You should resize appropriately:
With Workbooks(strFile).Sheets(strSource).UsedRange
ThisWorkbook.Sheets(strTarget).UsedRange.Resize(.Rows.count, .Columns.count).Value = .Value
End With
It's probably safer to clear the target sheet first using:
ThisWorkbook.Sheets(strTarget).UsedRange.ClearContents

Excel VB Copy and Pasting Hyperlinks

I am having trouble copying and pasting the hyperlinks from one sheet to another using VB.
I am using a database connection to pull the hyperlinks into Sheet1 and then using a macro to copy and paste the database data into another sheet, however the copy and pasting doesnt paste the "Clickable" link, only plain text.
How do I go about copying a hyperlink using VB, is this even possible??
Sheets("Sheet1").Select
Range("J19:K100").Select
Application.CutCopyMode = False
**Selection.SpecialCells(xlCellTypeVisible).Copy**
Sheets("Sheet2").Select
Range("C5:D100").Select
ActiveSheet.Paste **Link:=True**
Any help is much appreciated!
Thanks!
UPDATE
I am using a database connection, which holds the URL/Hyperlinks. I have tried your code for filtered table, however this only works if the links are directly input into the cell. i.e entered through right click on cell > Hyperlink.
Has anyone achieved copying URL's from a database connection in excel before?
Thanks.
You can use following paste special option to paste links.
ActiveSheet.PasteSpecial xlPasteAll