Copy and Paste a Column into a Row in excel using vba - vba

I am trying to write a macro that copies a range of cells (AA4:AA15)(e.g. AA4, AA5,AA6...AA15)
and pastes these values into a new range (C3:N3)(e.g. C3, D3, E3,...N3). The values are found using a formula. I tried using the code seen below, but it only pasted the first value in my copy range, not all of the values. Any help is appreciated.
Range("C3:N3").Value = Range("AA4:AA15").Value

If you did this manually, you would use Paste Special->Transpose. So try:
Sub Macro1()
Range("AA4:AA15"). Select
Selection.Copy
Range("C3").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
End Sub
(Note that I'm only selecting the first cell C3, not the entire range C3:N3)
Excel has a great macro recorder that can help you learn VBA. Simply turn it on and do some stuff and the recorder will create a VBA macro with those exact actions.

Related

VBA code issue for copying and pasting same columns from multiple sheets in excel workbook

Having a problem with the code.
The excel workbook has 4 sheets. The workbook is regularly updated.
-Sheet1 is where I want the data pasted to.
-Sheets2-4 have the data that I am trying to get.
-The range "A2:B2" is where the data is at in Sheets2-4. Data needs to be pasted in the same range in Sheet1. No data pasted.
The below code results in A2:B2 only selecting in Sheet2-4 and the same range being selected and copied in Sheet1.
Any help would be appreciated.
Sub test()
Worksheets.Select
Range("A2:B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Your code is copy/pasting from sheet1 to sheet1.
The first line of your code selects all sheets.
Then it selects the current range of first sheet, and applies the same selection to all sheets.
When you copy the selection on multiple sheets, only first sheet gets copied.
Then you paste the sheet1, to sheet1.
You cant copy from multiple sheets in one command.
If you could, then pasting values from 3 sheets into the same destination range in 1 sheet is also not possible.
Your questions states that you only need range A2:B2, but your code selects the used range below the code which is unnecessary if you only need range A2:B2.
Here is some code to copy/paste from a single sheet:
Sub CopyPaste()
Worksheets("Sheet2").Range("A2:B2").Copy Destination:=Worksheets("Sheet1").Range("A2:B2")
End Sub
if you would prefer copying the activesheet, then remove the sheet reference from the above code:
Sub CopyPaste()
Range("A2:B2").Copy Destination:=Worksheets("Sheet1").Range("A2:B2")
End Sub

Excel Macro is only copying "false" value of a logic statement

I used the Macro Recorder to create a macro to copy files from one worksheet to a second worksheet and reset a form to default values.
When the macro is run the "false" value ("NO") of a logic statement is copied regardless of the value in the cell at the time the macro is run. If I change the value in the false statement from "NO" to any other value (i.e. "Blue") it copies over the new value ("Blue").
Here is the formula for the logic statement:
=IF(AND(D15>VLOOKUP(C13,CCT,3,FALSE),D15<VLOOKUP(C13,CCT,4,TRUE)),"YES","NO")
where CCT is a list.
Here is the code for the macro:
Range("D17").Select
Selection.Copy
Sheets("Worksheet2").Select
Range("G3").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Worksheet1").Select
You might try Worksheet.Activate after you select different sheets. Worksheet.Calculate might be in order as well.
I think that using Range... implies Activesheet.Range. Hope it helps

Flipping an Excel spreadsheet to opposite axis programmatically

I have an Excel spreadsheet where the data is displayed in columns. For example:
I want to flip this spreadsheet so that the header cells are in Column A, and the data in Column B, C, and D. For example:
Is it possible to accomplish this through an Excel feature/programmatically? I'd rather not flip the data manually.
I am using Excel for Mac 2007, but a solution using any version would be greatly appreciated.
I have found that the simplest method to accomplishing this is using the Transpose feature, as suggested by chris neilsen in the comments.
Here are the steps to using Transpose using Office for Mac 2007:
Copy/Cut the Data --> Edit --> Paste Special... --> Transpose
This macro will create a new Sheet with the tranposed data:
Sub TransposeData()
'
' TransposeData Macro
'
'
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End Sub
You need to be at the left upper corner of the data and then run the macro.

VBA Copy Paste is Removing Borders

I'm copying and pasting values from one workbook to another using VBA code. However, when I paste, the borders in the destination worksheet are being deleted. How can I maintain the borders when pasting?
Below is my code:
With wsSource
.Range(.Range("A2"), .Range("C2").End(xlDown)).Copy wsDestination.Range("A3")
End With
I have read that the PasteSpecial method could be of use, but I don't know how to implement it in the above code.
Thanks!
Try this
With wsSource
.Range(.Range("A2"), .Range("C2").End(xlDown)).Copy
wsDestination.Range("A3").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
Whenever in doubt, record a macro ;)

Copy and paste from one workbook to another

I have code in one workbook, this should open another workbook, copy and paste into the workbook with the code. I can select the data but can't paste it.
I have tried many different variations of code getting errors or it doesn't do anything. An example is run in template.xls, which is where I want to paste the data:
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet.Sheets("Data")
.range("A1:H3").Select.copy
selection.copy
End With
I don't know how to use the selection since this will copy from the template, I tried using a full stop before selection.
I can copy the entire sheet from dlsheet into a new workbook, if someone could tell me how to copy it to the template and not a new workbook then this would also do the trick.
dlsheet.Sheets("Data").Copy
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
dlsheet.Sheets("Data").range("A1:H3").copy
ThisWorkbook.ActiveSheet.Paste Destination:=ThisWorkbook.ActiveSheet.Range( "A1:H3")
Try this
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet
.Sheets("Data").Range("A1:H3").Copy
.Sheets("Data").Range("A1").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With