Excel VBA - Macro that runs for entire range of cell - excel

I have this below macro code that performs a transpose a range of cells.
Sub Macro45()
'
' Macro45 Macro
' r3
'
' Keyboard Shortcut: Ctrl+e
'
Range("F2:G8").Select
Selection.Copy
Range("H2").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End Sub
I am trying to see how could I repeat the same process for entire range of rows. My dataset has about 10000 rows and I want to perform the same tasks over the entire range.

It seems you taking particular steps of 7 rows. So maybe try:
Sub Test()
Dim lr As Long, x As Long
With ThisWorkbook.Worksheets("Sheet1") 'Change accordingly
'Find last used row
lr = .Cells(.Rows.Count, 6).End(xlUp).Row
'Step through data and transpose values
For x = 2 To lr Step 7
.Cells(x, 8).Resize(2, 7).Value = Application.Transpose(.Range(.Cells(x, 6), .Cells(x + 6, 7)).Value)
Next x
End With
End Sub
Or if you really interested in copy-paste values and format:
Sub Test()
Dim lr As Long, x As Long
With ThisWorkbook.Worksheets("Sheet1") 'Change accordingly
'Find last used row
lr = .Cells(.Rows.Count, 6).End(xlUp).Row
'Step through data
For x = 2 To lr Step 7
.Range(.Cells(x, 6), .Cells(x + 6, 7)).Copy
.Cells(x, 8).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Next x
End With
End Sub

Here's another solution:
Sub main()
Dim rngSrc As Range
Set rngSrc = Range("F2:G8")
While (rngSrc.Cells(1, 1).Value2 <> "")
transpose rngSrc
Set rngSrc = rngSrc.Offset(7, 0)
Wend
End Sub
Sub transpose(rngSrc As Range)
rngSrc.Copy
rngSrc.Cells(1, 2).Offset(0, 1).PasteSpecial Paste:=xlPasteAll, _
Operation:=xlNone, SkipBlanks:=False, transpose:=True
End Sub

Related

How to copy (from COL) and paste (to ROW) (replacing data in specific places)?

I have a slight problemo. The code I have put together takes data from column A on sheet(1) and copies it (transposing it to row) to sheet(2). Although, before I paste it in (here is the point when I have started recording a macro), I go "control+right" to find the first populated cell (in row 2 (sheet(2)) and then I select paste in the data transposing it and replacing the data that I already have there (to row).
The problem is that I have 1000 columns that I need to transpose in this way to rows (in each case row starts at various points on sheet(2)) and replace the old data. Can someone please advise? It would require some kind of loop (I know that).
Sub test()
Dim ws1, ws2 As Worksheet, lr As Long
Set ws1 = ThisWorkbook.Sheets(1): Set ws2 = ThisWorkbook.Sheets(2)
lr = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row
ws1.Range("A2:A" & lr).Copy
With ws2
'I have recorded this part
ws2.Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End With
End Sub
Some screens for clarification:
Here ws2 where data begins at various points in each row (I need data from ws1 to be copied to ws2 and replace the data exactly where it begins in each row):
Try the next code, please:
Sub testCopyTranspose()
Dim ws1, ws2 As Worksheet, lr As Long, lastCol As Long, LastCol2 As Long, i As Long
Set ws1 = ThisWorkbook.Sheets(1): Set ws2 = ThisWorkbook.Sheets(2)
lastCol = ws1.cells(1 & Columns.count).End(xlToLeft).Column
For i = 1 To lastCol
lr = ws1.cells(Rows.count, i).End(xlUp).Row
LastCol2 = ws2.cells(i + 1, 1).End(xlToRight).Column
ws1.Range(ws1.cells(2, i), ws1.cells(lr, i)).Copy
ws2.cells(i + 1, LastCol2).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=True
Next i
End Sub

Copy Pasting table to variable range based on condition and fixed offset

The macro is suppose to copy a fixed table 1 ("E19:Q34") and paste it to a range which is 15 column offset of cell E19 i.e. "T19" and would be called 'Table 2'. the next time the macro runs it should be able to detect the table and further move ahead 15 columns to "AI19" and so on..
Sub Macro()
Application.ScreenUpdating = False
Dim Rng, rng1, rng2 As Range, ws As Worksheet,
Set ws = ActiveWorkbook.ActiveSheet
Set Rng = ActiveSheet.Range("E19")
Set rng1 = Rng.Offset(0, 15)
Set rng2 = ActiveSheet.Range("E19:Q34") 'fixed base range
'Copy the range with text and paste it after finding the right location
rng2.copy
rng1.Select
For Each rng1 In rng1.Cells
If rng1.Value = "" Then
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ws.Paste
Application.CutCopyMode = False
Exit For
Else
Range(rng1).Address = ActiveCell.Offset(0, 15) 'attempting to change the reference of rng1
' MsgBox rng1
End If
Next rng1
End sub
Try this:
Sub Macro()
Const COL_OFFSET As Long = 15
Dim rng, ws As Worksheet, cols As Long
Set ws = ActiveSheet
Set rng = ws.Range("E19:Q34")
Application.ScreenUpdating = False
rng.Copy
cols = COL_OFFSET
'find the next empty slot
Do While Application.CountA(rng.Offset(0, cols)) > 0
cols = cols + COL_OFFSET
Loop
With rng.Offset(0, cols)
.PasteSpecial Paste:=xlValues
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
End With
End Sub

Copy and Paste of data to last row

I have the code below and it all works well with MyCopy10. But the next code MyCopy100 is not copying the data in last row of sheet Actual Email. I am not sure as were problem is.
here is my Code:
Sub MyCopy10()
Dim myCols As Variant
Dim lastRow As Long
Dim c As Long
Sheets("Eamil-10").Activate
'Set columns you want to loop through in an array
myCols = Array("A", "B", "C", "D")
' Loop through columns array
For c = LBound(myCols) To UBound(myCols)
' Find last row in column A with data
lastRow = Sheets("Eamil-10").Cells(Rows.Count, myCols(c)).End(xlUp).Row
' Copy data from Model sheet to summary sheet
Sheets("Eamil-10").Range(Cells(1, myCols(c)), Cells(lastRow,
myCols(c))).Copy
Sheets("Actual Email").Cells(1, c + 1).PasteSpecial Paste:=xlValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Next c
' Sheets("Summary").Activate
End Sub
Code:
Sub MyCopy100()
Dim myCols As Variant
Dim lastRow As Long
Dim c As Long
Sheets("Email-100").Activate
' Set columns you want to loop through in an array
myCols = Array("A", "B", "C", "D")
' Loop through columns array
For c = LBound(myCols) To UBound(myCols)
' Find last row in column W with data
lastRow = Sheets("Email-100").Cells(Rows.Count, myCols(c)).End(xlUp).Row
' Copy data from Model sheet to summary sheet
Sheets("Email-100").Range(Cells(1, myCols(c)), Cells(lastRow,
myCols(c))).Copy
Sheets("Actual Email").Cells(1, c + 1).PasteSpecial Paste:=xlValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Next c
' Sheets("Summary").Activate
End Sub
Try the code below, all Range and Cells are qualified with Sheets("Email-100").
Code
Option Explicit
Sub MyCopy100()
Dim myCols As Variant
Dim lastRow As Long
Dim c As Long
' Set columns you want to loop through in an array
myCols = Array("A", "B", "C", "D")
With Sheets("Email-100")
' Loop through columns array
For c = LBound(myCols) To UBound(myCols)
' Find last row in column with data
lastRow = .Cells(.Rows.Count, myCols(c)).End(xlUp).Row
' Copy data from Model sheet to summary sheet
.Range(.Cells(1, myCols(c)), .Cells(lastRow, myCols(c))).Copy
Sheets("Actual Email").Cells(1, c + 1).PasteSpecial Paste:=xlValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Next c
End With
End Sub

Copy columns until last row from one sheet and paste to the next empty row of another sheet

I'm using the below VBA code which is copying a range from Sheet1 and paste it in the same sheet. However i need to paste the data in the next available row of sheet2.
Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("A1:A5").Copy
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Sheets("Sheet2").Activate
Cells(lastrow + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
Please help me out..
Try this:
Private Sub CommandButton1_Click()
Dim lastrow As Long
Dim rng1 As Range
Dim rng2 As Range
lastrow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
Set rng1 = Sheets("Sheet1").Range("A1:A5")
Set rng2 = Sheets("Sheet2").Range("A" & lastrow + 1)
rng1.Copy
rng2.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
Your code is good but one line you need to change it, that is place Sheets("Sheet2").Activate line before lastrow = Range("A65536").End(xlUp).Row
Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("A1:A5").Copy
Sheets("Sheet2").Activate
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Cells(lastrow + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
First activate the sheet2 and then find last row
lastrow = Range("A65536").End(xlUp).Row

Copy and Paste to next available column

I'm trying to track weekly quantities I have in my spread sheet. So far I've made a macro to copy and paste the info where I need it. But it will only paste it to the spot I chose while recording the macro. I'd like it to paste the info into the next available column.
I'd also like to schedule the macro to run once a week on Friday morning.
Macro I'm using now.
Sub CopyPaste()
'
' CopyPaste Macro
'
'
Range("G4:G33").Select
Selection.Copy
Range("B35").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
I've tried putting & lastrow into the range, but it gets a compile error. Any help would be greatly appreciated.
At first sight maybe slightly more complex, but in a way a more pretty way of tackling the movement of values is to avoid using the clipboard with code like this:
Sub CopyPaste()
'
' CopyPaste Macro
'
'
Dim targetRng As Excel.Range
Dim destRng As Excel.Range
Set targetRng = Range("G4:G33")
Dim lc As Long
With Excel.ThisWorkbook.Sheets("Sheet1")
lc = .Cells(35, .Columns.Count).End(Excel.xlToLeft).Column
Set destRng = .Range(.Cells(35, lc), .Cells(35, lc)).Offset(0, 1).Resize(targetRng.Rows.Count, targetRng.Columns.Count)
destRng.Value = targetRng.Value
End With
End Sub
The above can be simplified to the following so you don't need to worry about using the last row variable:
Sub CopyPaste()
'
' CopyPaste Macro
'
'
Dim targetRng As Excel.Range
Dim destRng As Excel.Range
Set targetRng = Range("G4:G33")
With Excel.ThisWorkbook.Sheets("Sheet1")
Set destRng = .Cells(35, .Columns.Count).End(Excel.xlToLeft).Offset(0, 1).Resize(targetRng.Rows.Count, targetRng.Columns.Count)
destRng.Value = targetRng.Value
End With
End Sub
You can work out the column number of the last column like this:
Sub CopyPaste()
'
' CopyPaste Macro
'
Dim lastCol As Long
' this finds the number of the last column
lastCol = Cells(35, Columns.Count).End(xlToLeft).Column
Range("G4:G33").Copy
' Range("B35").Select
' no need to select. paste into the cell in row 35, one to the right of the last column
Cells(35, lastCol + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
You could also add the 1 right in the lastCol definition, like
lastCol = Cells(35, Columns.Count).End(xlToLeft).Column + 1
Range("G4:G33").Copy
Cells(35, lastCol).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
For scheduling the macro look at these two questions here and here

Resources