Copy from one sheet * by 2 and then paste - excel

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

Related

How to insert a column in Excel using VBA?

Please help me with the code to insert a column in excel using vba. Here is what I am doing -
Sheet1.Range("A:A").EntireColumn.insert
That code works fine for me.
Sheet1.Range("A:A").Insert works also. Range("A:A") is already referencing an EntireColumn.
There are a few things to check if it's not working for you:
You're referencing an object called Sheet1. Is that definitely the codename of the worksheet you want to change? Make sure you understand the difference between sheet name and codename. You could try referencing it by the sheet's name instead: Worksheets("Tabname").Range("A:A")...
Is the worksheet protected? That would give you an error if it is.
Is there any data in the right-most column of the spreadsheet? That would also cause an error as Excel doesn't know what to do with it. If you're not 100% sure, select the entire right-most column of the sheet and hit delete to remove anything that might cause an issue.
Lastly, can you insert a column manually? i.e. select left most column, [right-click] and [Insert]?
I think you got the Sheets property wrong, this works for me :
Sheets(1).Range("A:A").EntireColumn.Insert
You should clearly mention what you are trying to do; what type of problem you are facing.
However, you should try out the below code. hope this will help
Sheet1.Range("A:A").EntireColumn.Insert Shift:=xlToRight
while inserting a new row or column make sure to refer to the entire row or column. Check if there are any marge cells at the right of column A. That can possibly be causing the problem.
Sub TryMe()
Columns(1).Insert
End Sub
Also, this is a very generic question. If you can Google something and figure it out in a fraction of the time that it takes to craft a question and post it on SO, just Google it. If your question is very unique, and after hitting multiple dead ends using Google, then ask the SO community. I found the 3 links below using a Google search that took around 1 second.
https://www.automateexcel.com/vba/insert-row-column/
https://excelchamps.com/vba/insert-column/
https://www.educba.com/vba-insert-column/

excel copy links PasteSpecial xlPasteAll Cells

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

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.

Macro to compare dates from different file

I am an old man trying to compare dates from two different files in Excel.
My code is:
Dim i As Integer
For i = 1 To 7
IF Data_for_Processing.xls (“SOLARLOG”). Cells (i,”A”).Value = Day_Conversion_chart.xls (Sheet1).Cells (i+2, “B”) Then
Cells(7+I, “B”)=”Equal”
Else: Cells(7+i, “B”) = “NotEQ”
End If
Next i
Will anyone help?
First of all, I would recommend following #simoco 's advice - Reading the documentation will provide the tools for solving future problems and give you the basics. As well as that, I would recommend using vba help. And another very useful tool for trying commands could be recording macros and analyzing them later on the editor.
So, first you need the code to be inside a macro. It will look like this (I chose the name TestMacro):
Sub TestMacro()
'Code here.
End sub
You should take into account that when your macro is running, it does so from the sheet you are working in, so any partial references to cells, etc. will refer to the book you are in and the sheet you are in when you run the macro. It is possible to select another sheet or book, and if you do so manually or on the code, references will be applied on that selected book or sheet.
What I call partial references here are those that read simply "ThisCell" instead of "ThisBook.ThisSheet.ThisCell". I would say that using partial references, though, is appropriate in a vast majority of cases.
The way your code is organized, be careful to run it from the workbook where you want the data to be in the end, let's say, in your 'main' workbook, so that the final values will be written there..
Another comment: whenever you want to use another file, this file must be open (as far as I know), while you are using it. In your code, you don't open any file, so the macro will not work if you don't open ALL referenced workbooks manually prior to running the macro.
When you want to reference something inside something, you mostly use ".". Please read the documentation - You will get a better idea of how this works. For this example:
Book.Sheet.Cell.Value is the structure you are using.
A book can be referenced as Workbooks("Name.xls") if it is open.
A sheet or worksheet can be referenced as Sheets("Name") or Worksheets("Name"), and also with numbers, like Sheets(1) or Worksheets(1). The differences can be seen on vba editor help. I chose Sheets(...) for your example.
Be careful with the symbols. I guess this was probably my problem, but I have to mention it just in case: When I copied your code, instead of double quotes (""), I got something different, that Excel would not recognize. If for any reason you are using different symbols, Excel might not understand.
A cell can be referenced in various ways too. The Range object is used extensively, and if you want to use "A1", "C44", etc., it's probably better to go for it. I like using Cells(,) as you did, when possible. As far as I know, this works nice with numbers (Cells(1,2), for example), which may be very convenient too. I kept this on your code, changing "A" and "B" and writing 1 and 2, respectively.
With all these changes incorporated:
Comments:
'NOTICE THAT ALL WORKBOOKS MUST BE OPEN.
'["A"] changed to [1]
'[Sheet1] changed to [1]
'["B"] changed to [2]
'Data_for_Processing.xls(“SOLARLOG”).Cells(i, 1).Value
'becomes Workbooks("Data_for_Processing.xls").Sheets(“SOLARLOG”).Cells(i,1).Value
'Day_Conversion_chart.xls(1).Cells(i + 2, 2).Value
'becomes Workbooks("Day_Conversion_chart.xls").Sheets(1).Cells(i+2,2).Value
'["B"] changed to [2]
And one possible solution:
Sub TestMacro()
Dim i As Integer
For i = 1 To 7
If Workbooks("Data_for_Processing.xls").Sheets("SOLARLOG").Cells(i, 1).Value _
= Workbooks("Day_Conversion_chart.xls").Sheets(1).Cells(i + 2, 2).Value Then
Cells(7 + i, 2) = "Equal"
Else: Cells(7 + i, 2) = "NotEQ"
End If
Next i
End Sub
This worked on my Excel example - I hope it is of some help.
Regards.

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