excel copy links PasteSpecial xlPasteAll Cells - excel

Dear Stackoverflow Members,
I am trying to copy data from one "old" workbook to a "new" one, but not only copying the data but also keeping the link to the original file. What I have right now is:
For i = 1 To Number
wkb.Sheets(1).Range("A" & i).Copy
wkbMain.Sheets(currentsheet).Cells(Nrow + i - 1, NCol).PasteSpecial xlPasteAll
Next i
I am aware that xlPasteall does what I want for ".Range()" but not for .Cells(), however Id like to keep the .Cells as it works so well together with the For Loop.
How do I get the Cells linking to Cells in another workbook?
Thank you very much and apologies if the question has been asked before. Unfortunatly I could not find it. This is my first question on stackoverflew, so please be gentle.
Best
Jasper

Related

How can a macro ask for what open spreadsheet to use?

Im a very novice VBAer so please forgive me for asking this, but i cannot find the answers no matter how much searching and experimenting ive done.
I am hoping to create a macro that will allow the user to match values between workbooks and update the matching data from one workbook to the other. For example, in Workbook1, column1 has values and column2 has data. In Workbook2 I would have values in column1, but no data. I would like the macro to update the data from Workbook1 column2 into Workbook2 column2. I have been able to do this using a simple VLOOKUP function, but I am not able to find how to update the macro to ask which two open workbooks and sheets to run the macro on. I am hoping to get it flexible enough to display an input box asking the user to choose which workbook/sheets to reference from, and which workbook/sheets to update the info into.
I have also not been able to figure out how to get the macro to stop when it sees a blank cell. The code i have just runs until row 5000, which is ok but I am hoping to find a better solution.
Here is the code i currently have:
Sub Match_Update()
' Match_Update
' Match 1st column and update column to right
'
'
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Book1]Sheet1!C1:C2,2,FALSE)"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A5000")
End Sub
I have tried to change the 'ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Book1]Sheet1!C1:C2,2,FALSE)"' to reference objects as the table_array, but have not had much luck.
Any suggestions would be much appreciated!!!

Copy from one sheet * by 2 and then paste

I have a workbook that copies data from one workbook to another. This aspect works fine. I have a few cells which are causing me an issue as i need the value to come through as double its original value.
So basically one workbook has the radius and when it comes into the next workbook it needs to be the diameter, so i need to do a x2 somehow. I have tried doing a few things with no luck, i have also looked on the web and there doesnt seem to be a clear cut answer.
My code is this: -
OpenBook.Sheets("Input 2").Range("C39").Copy
ThisWorkbook.Worksheets(1).Range(ItemCell & "36").PasteSpecial xlPasteValues
WHich works great as a simple copy from one workbook to paste to another. But i want to do something like this: -
OpenBook.Sheets("Input 2").Range("C39").Copy
ThisWorkbook.Worksheets(1).Range(ItemCell & "36").PasteSpecial xlPasteValues *2
Any ideas?
I have seen this on the web Operation:=xlMultiply but dont understand how it works. I also found a lot of sites and people sayings its impossible. I am hoping they are wrong.
Thank you in advance.
Steven
Skip the clipboard and transfer the value, multiplying by 2:
ThisWorkbook.Worksheets(1).Range(ItemCell & "36").Value = OpenBook.Sheets("Input 2").Range("C39").Value * 2

Cutting a region on one worksheet and pasting to another doesn't work. But copy does. Why?

I have been struggling with this problem for awhile now.
I've written a VBA routine that is supposed to find a region on one worksheet, then cut and paste it to another worksheet. If I run the following code it doesn't work:
' This does not work
DSheet.Range(SummaryDataAddr, DSheet.Cells.SpecialCells(xlLastCell)).Cut
PSheet.Range(SummaryDataLocation).PasteSpecial Paste:=xlPasteValues
But if I change the code to copy the region from the first worksheet, paste it to the second worksheet, then go back to the first worksheet and explicitly delete the region, it works.
' But this does work. Why?
DSheet.Range(SummaryDataAddr, DSheet.Cells.SpecialCells(xlLastCell)).Copy
PSheet.Range(SummaryDataLocation).PasteSpecial Paste:=xlPasteValues
DSheet.Range(SummaryDataAddr, DSheet.Cells.SpecialCells(xlLastCell)).Delete
Has anyone seen this kind of problem before? Is there a way to use the cut function and get it all to work?
To access a the spreadsheet with the VBA code in it go here:
https://www.dropbox.com/s/satv6z95tlqw7lr/CutBug.xlsm?dl=0
The routine is called "CreateDLDataPivot" (it's a pared down version of a larger program I'm working on).
Thanks for any help!
PasteSpecial Paste:=xlPasteValues does not work with Range.Cut; it only works with Range.Copy.
Range.Cut only clears the cells, it does not delete the cells.
Try a direct value transfer and clear.
with DSheet.Range(SummaryDataAddr, DSheet.Cells.SpecialCells(xlLastCell))
PSheet.Range(SummaryDataLocation).resize(.rows.count, .columns.count) = .value
.clear 'use .clearcontents to retain formatting
end with

Concatenating in VBA within Excel

I am trying to accomplish something apparently simple but not for me. I’m new to all this and I would like to see if I can get any far with this first project. Basically I have, say 3 text boxes with a string in it (name) and a button next to them. Then there is another text box which will hold all 3 names so that I could copy as one string into clipboard. Many tutorials out there refer to concatenating the strings however I’m still unable to find a decent answer. To be fully honest I am trying to achieve this task in VBA from within Excel 2010. So, my 3 names are actually in 3 different cells… Next to each of them I have a button with a macro attached (but vb will also be good for me) that will add the name into another cell. The buttons works ok but my problem is that I can’t get them to concatenate in Excel. Every button just overwrites what’s was there before. Any help? Suggestion? Tips? Mind you I can only be able to follow if the answer is simple and well explained as I am new to all of this. Thank you.
If you had 3 strings in A1, B1 and C1, you could get the concatenated string in D1 by putting in the formula `=CONCATENATE(A1,B1,C1)' in the cell D1. If you want to do it in VBA,
Public Sub MyConcatenation()
With ThisWorkbook.Worksheets(1)
.Cells(1, 4).Value = .Cells(1, 1).Value & .Cells(1, 2).Value & .Cells(1, 3).Value
End With
End Sub
If you won't take offense: You need to pick up an introductory book or website to learn VBA (it is not VB or VB.Net). It will be a long, uphill struggle to learn it through asking questions on forums.

How to remove #N/A that appears through out a worksheet? (VBA for excel Required)

I have to clear #N/A that happens through out my worksheet when ever i run my code. I'm not sure why and have been debugging for a while but to no avail. What i could do to remedy this problem is to delete it entirely from the page, where they happens randomly. If anyone know how to, do share a VBA code with me.
Codes of doing a simple copy and paste into another sheet
thevaluestocopy = Sheets("pivot").Cells(thefirstrow, 1) _
.Resize(thelastrow - thefirstrow, 1)
Worksheets("summary").Cells(3, 16) _
.Resize(thelastrow - thefirstrow + 1, 16) = thevaluestocopy
I have nested that code with different Column because my pivot table changes most of the time. And when i copied for the 2nd time, #N/A appears.. Have no idea why and i believe this works work fine.
You can run this line to remove any #N/A error values in the worksheet:
Cells.Replace "#N/A","",xlWhole
If the worksheet contains formulas you can try this:
Cells.SpecialCells(xlCellTypeFormulas,xlErrors).Clear
I don't know, but how about doing something like this instead? This is how I generally copy and paste, without problems.
Sheets("pivot").Range("your range").Copy
Worksheets("summary").Range("your range").PasteSpecial
UPDATE
If you still want to simply remove all the #N/As with your current code, you can use some code like this.
If WorksheetFunction.IsNA(Cells(row, column)) Then Cells(row, column).ClearContents

Resources