Flipping an Excel spreadsheet to opposite axis programmatically - vba

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.

Related

vba code to copy worksheets and paste as values to a new workbook

I'm not very good at vba so excuse my amateur question.
I have an active workbook open containing 3 tabs. I want to build a macro that opens up another workbook and pastespecial values the data from my three tabs into the three tabs on the second workbook.
This is my coding which keeps breaking on the paste special line.
Sub NewVersion_Click()
Dim y As Workbook
ThisWorkbook.Sheets("Fact Find").Range("A5:I283").Copy
Set y = Workbooks.Open("location")
y.Worksheets("Fact Find").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'ThisWorkbook.Sheets("Entity Fact Find").Range("A4:F237").Copy
'y.Worksheets("Entity Fact Find").Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'ThisWorkbook.Sheets("Suitability Assessment Form").Range("A4:E108").Copy
'y.Worksheets("Suitability Assessment Form").Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ActiveWorkbook.Close
End Sub
Any help will be really appreciated.
Thanks
Arvin
Here is your paste special for worksheet fact find.
Modify the code to copy the other sheets, but no need to open the file again.
Which file are you trying to close?
Set wb = ThisWorkbook
wb.Sheets("Fact Find").Range("A5:I283").Copy
Workbooks.Open ("location")
Workbooks("location").Worksheets("Fact Find").Range("A1").PasteSpecial xlPasteValues
wb.Sheets("Entity Fact Find").Range("A4:F237").Copy
Workbooks("location").Worksheets("Entity Fact Find").Range("A1").PasteSpecial xlPasteValues

Excel VBA Vlookup where user chooses what cells

I am creating a macro to do a vlookup between 2 different sheets within the same workbook, but sometime the layout of the cells changes so I wish to be able to add a dialog box that will allow the user to select what they are looking for and what list they would like to compare against but am not sure how to do this.
This is my code thus far:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-59],ONCE!C[-49],1,0)"
Range("BI2").Select
Selection.AutoFill Destination:=Range("BI2:BI208032")
Range("BI2:BI208032").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False
Thanks in advance
You can use InputBox like this:
myValue = InputBox("Give me some input")

Copy/PasteSpecial Offset macro is slow when switching between worksheets

Intro: I've been working on this macro for quite awhile and have had success getting it to do what I need it to do, however I'm not doing it the most efficient way as my coding background is minimal. Because of this the macro is extremely slow due to switching back and forth from one worksheet "FB MATE DATA" to another "Sheet 2 (2)" copying and pasting data.
Background: A CMM machine spits out measurement data into a spreadsheet that I need to copy and paste from one sheet to another in neat X and Y columns to be overlayed in a scatter plot. The problem is that the data is in an unconventional pattern that requires the use of an offset, and different sheets have data that starts in different rows so the macro must account for that. The reason it is so slow is because the way i have achieved this is by constantly activating each worksheet back and forth, over and over to maintain control over the active cell instead of just referencing an offset from a fixed cell due to the offset changing occasionally in a row. The code i have now:
cycles down through column A until it finds a blank cell
shifts down to the cell immediately below it and starts copying data over. (This is very important because the first few rows are data irrelevant to me, and they are separated from the data i seek by a space.)
copies data from the range (G:FZ) in an offset (e.g. G21, K21, O21, S21, W21, AA21, AE21, AI21, AM21, AQ21, AU21, AY21, BG21) into a neat X|Y table from D73:E:93. Notice the irregular pattern (due to info extraneous to my plot)
Every two represent an X|Y pair (e.g. X: G21 pasted into D73 in Sheet 2, Y: K21 pasted into E73 in Sheet 2, etc.)
Problem: For each row of data, I need to copy certain columns in an interval that changes (first column, fourth column, ninth column, twelvth column, etc.) and paste it into two X|Y columns on another page.
The data grab will not always start in G21. Sometimes the first data point may be G24, or G10, or G15, hence why the macro searches for a blank row first instead of pulling from a fixed position. Below is a sample of cycling through the first four columns.
Code:
Sub LocatorTest()
'Select first row of eligible spare pallet data
Range("B73").Select
Application.Goto (ActiveWorkbook.Sheets("FB MATE DATA").Range("A1"))
Dim c
For Each c In Range("A1:A100").Cells
If c = "" Then
c.Select
Exit For
End If
Next
ActiveCell.Offset(1, 6).Range("A1").Select
Selection.Copy
Sheets("Sheet2 (2)").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C73").Select
Worksheets("FB MATE DATA").Activate
ActiveCell.Offset(rowOffset:=0, columnOffset:=4).Activate
Selection.Copy
Sheets("Sheet2 (2)").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B74").Select
Worksheets("FB MATE DATA").Activate
ActiveCell.Offset(rowOffset:=0, columnOffset:=8).Activate
Selection.Copy
Sheets("Sheet2 (2)").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C74").Select
Worksheets("FB MATE DATA").Activate
ActiveCell.Offset(rowOffset:=0, columnOffset:=12).Activate
Selection.Copy
Sheets("Sheet2 (2)").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Any help you can provide into maybe a different way of achieving this or making it run quicker would be greatly appreciated.
o wow, this can use some MAJOR improvements.
but to make it easy, you might want to put this at the start:
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
and this at the end of your macro:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
This will give it huge speed boost already. Next; you should try to reference to the cells without activating...

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

Copy and Paste a Column into a Row in excel using 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.