VBA Excel Copying Cell contents to another via column and row offset - vba

Here is my code
Sub A_Copy_To_Prior_Row_column()
'
' A_Copy_To_Prior_Row_column Macro
'
'
Application.CutCopyMode = False
Selection.Copy
Range("R183").Select
ActiveSheet.Paste
End Sub
Where R183 is I need to not a specific cell, but a cell based on minus 1 row, and +17 columns of the selected/highlighted cell I wish to copy

Sub A_Copy_To_Prior_Row_column()
'
' A_Copy_To_Prior_Row_column Macro
'
'
Application.CutCopyMode = False
With Selection
.Copy
ActiveSheet.Paste Destination:=.Offset(-1, 17)
End With
End Sub

activecell.Offset(-1, 17).Value = activecell.Value

Related

VBA code to copy a cell from sheet_A to sheet_B

I have two sheets:
SheetA has a list of employee Nr.
Sheet B has a form that needs to be filled out AND printed with each employee numbers on it (then vlookup formulas fill out the rest)
Now I can copy paste each employee ID manually, but there are 330+ employees, that is a bit too much.
I would like to copy cell A2 in Sheet_A, paste it into cell A2 Sheet_B AND print the form, then go to cell A3 in Sheet_A copy it, paste it into A2 in Sheet_B and so on... I would like to repeat this process 337 times.
I created this macro, but I don't know how to make it always choose the next cell in Sheet_A AND repeat itself 337 times. (or depending on how many employees we have at a certain time)
Sub Copy_Cell()
' Copy_Cell Macro
Sheets("Sheet A").Select
Range("A2").Select
Selection.Copy
Sheets("Sheet B").Select
Range("A2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
End Sub
You just need to loop through each of your rows:
Sub Copy_Cell()
Dim r As Long
'Use a "With" block to save having to constantly type "Worksheets("Sheet A")"
'inside the block
With Worksheets("Sheet A")
'Loop through all values in column A, thus saving the trouble of
'hard-coding the last row number to be used
For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
'Just copy the value directly from one worksheet to another, thus
'avoiding copy/paste
Worksheets("Sheet B").Range("A2").Value = .Cells(r, "A").Value
Worksheets("Sheet B").PrintOut Copies:=1, _
Collate:=True, _
IgnorePrintAreas:=False
Next r
End With
End Sub
Sub Copy_Cell() ' Copy_Cell Macro
Dim i as Integer
For i = 1 To 337
Sheets("Sheet A").Activate
ActiveSheet.Cells(i + 1, 1).Select
Selection.Copy
Sheets("Sheet B").Activate
Range("A2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
Next i
End Sub Image Image
Sub Copy_Cell()
Dim row as Integer
Do While row <= 337
Sheets("Sheet A").Activate
Sheets("Sheet A").Cells(row + 1, 1).Copy
Sheets("Sheet B").Activate
Range("A2").Select
ActiveSheet.Paste
ActiveWindow.SelectedSheets.PrintOut Copies:=1
row = row + 1
Loop
End sub

How to stop Loop in Vba

I have created a Looping VBA to update values according to data in Columns A,B,C,D. But I need to stop this Looping once 'D' Column is empty or has no values.
Sub Macro1()
'
' Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
Range("C2").Select
Range("C2").End(xlDown).Offset(1, -2).Select
Selection.ClearContents
Range("C2").End(xlDown).Offset(0, -2).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Range("C2").End(xlDown).Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "Logged out"
ActiveCell.Offset(1, 0).Select
Loop
End Sub
There are many ways to accomplish this task. Here is one method.
Sub Test1()
'UpdatebyExtendoffice20161222
Dim x As Integer
Application.ScreenUpdating = False
' Set numrows = number of rows of data.
NumRows = Range("D1", Range("D1").End(xlDown)).Rows.Count
' Select cell D1.
Range("D1").Select
' Establish "For" loop to loop "numrows" number of times.
For x = 1 To NumRows
' Insert your code here.
' Selects cell down 1 row from active cell.
ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True
End Sub
If isEmpty(Range(x)) then exit do end if

VBA Excel copy data from 2 different worksheets in a third one

I would like to copy the data from 'Sheet1' ($A:$N ; may fluctuate), select the range of the data and paste it in 'Sheet3'.
I also need to copy the data from 'Sheet2' without the first row (same headers as 'Sheet1') and paste it underneath the data of 'Sheet1' that is now in 'Sheet3'.
Sub CopyPaste()
Sheets("PC_VIEWS").Select
Range("A1:Q231").Select
Selection.Copy
Sheets("PC_LTC_VIEWS").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Selection.End(xlDown).Select
Range("A232").Select
Sheets("LTC_VIEWS").Select
Range("A1").Select
Application.CutCopyMode = False
Range("A1:M1264").Select
Selection.Copy
Sheets("PC_LTC_VIEWS").Select
ActiveSheet.Paste
End Sub
I am open to other solutions!
This code is enough. Try it.
Public Sub CopyAndPaste()
Dim firstRowCount, secondRowCount As Integer
'Copy from "PC_VIEWS" sheet.
Sheets("PC_VIEWS").Select
'Getting the last row from "PC_VIEWS" sheet.
firstRowCount = Range("A:Q").SpecialCells(xlLastCell).row
Range("A1:Q" & firstRowCount).Select
Selection.Copy
'Paste to "PC_LTC_VIEWS" sheet.
Sheets("PC_LTC_VIEWS").Select
Range("A1").Select
ActiveSheet.Paste
'Reset clipboard
Application.CutCopyMode = False
'Copy from "LTC_VIEWS" sheet.
Sheets("LTC_VIEWS").Select
'Getting the last row from "LTC_VIEWS" sheet.
secondRowCount = Range("A:Q").SpecialCells(xlLastCell).row
Range("A2:Q" & secondRowCount).Select
Selection.Copy
'Paste to "PC_LTC_VIEWS" sheet.
Sheets("PC_LTC_VIEWS").Select
Range("A" & firstRowCount + 1).Select
ActiveSheet.Paste
'Reset clipboard
Application.CutCopyMode = False
End Sub

We can't paste Excel ranges because the copy area and paste area aren't the same size

I would like to loop through column A in Worksheet1 and find the first cell which has a specified text "Oil Production". This cell is the first cell in the array I wish to copy to Worksheet2. This cell and the size of the array will change from time to time, hence the code I have used. I then paste it into cell B7 in Worksheet2 which will never change.
This is my formula. I get the error at line ActiveSheet.Paste
Sub Test()
Application.ScreenUpdating = False
For Each Cell In Sheets("Sheet1").Range("A:A")
If Cell.Value = "Oil Production" Then
ActiveSheet.Cells.Select
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
Selection.Copy
Sheets("Sheet2").Select
Range("B7").Select
ActiveSheet.Paste
End If
Next
End Sub
resize the area:
Sub Test()
Dim MyRowCount As Long, MyColCount As Long
Application.ScreenUpdating = False
For Each Cell In Sheets("Sheet1").Range("A1:A" & Range("A" & Rows.count).end(xlup).row) 'This make it poll the used data rather than the whole column
If Cell.Value = "Oil Production" Then
ActiveSheet.Cells.Select
With Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).column))
.Copy
MyRowCount = .Rows.Count
MyColCount = .Columns.Count
End With
Sheets("Sheet2").Select
Range("B7").Resize(MyRowCount, MyColCount).PasteSpecial xlPasteAll
'Do you need to flick back to Sheet1 after pasting?
End If
Next
End Sub
Also I took out a bunch of selects for you.
Range("A1").Select
Selection.Paste
can be written as
Range("A1").PasteSpecial XLPasteAll
You can chop out most selects this way, you can see I have also done it with the Range you are copying

how to capture cell address as a variable and use in VB code?

Need a code snippet; if some kind guru could help, please. I need to express the following cursor movement sequence in XL VBA.
After entering a formula in cell A1 (Col-A is otherwise empty), I need to copy the formula to all cells in the range A1:AN, where N is the last row of the table.
I recorded a macro to do the following (code below):
1) enter the formula (in Cell A1)
2) copy the formula
3) go Right to B1
4) go to the last populated cell in Col-B [using Ctrl+Down] (easiest way to find the last row)
5) go Left to Col-A
6) select all cells from current to A1
7) paste the formula to the selection
The part I need help with is a way to capture the cell address in step 5 as a variable so that I can use this macro on a series of files having a variable number of rows.
Here is the recorded macro. In this example, the last row in the table is 7952.
Sub test()
ActiveCell.FormulaR1C1 = "=LEFT(RC[1],3)"
ActiveCell.Select
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, -1).Range("A1").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveCell.Offset(-7951, 0).Range("A1:A7951").Select
ActiveCell.Activate
ActiveSheet.Paste
End Sub
Kindly copy the below code to the worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
If Target.Address = "$A$1" And Target.Count = 1 And Target.HasFormula Then
Dim lastRow As Long
lastRow = Range("A65000").End(xlUp).Row
Dim rng As Range
Set rng = Range("A2:A" & lastRow)
' Target.Copy
' rng.PasteSpecial xlPasteFormulas
'OR
' rng.Formula = Target.Formula
' OR
rng.FormulaR1C1 = Target.FormulaR1C1
End If
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
I'm not sure if your end cell is always going to be the same, meaning you may want to "un" hard code the rows, but you could try this.
Sub test()
Range(Cells(1, 1), Cells(7951, 1)) = "=LEFT(RC[1],3)"
End Sub
If you are always going to put equations in column A based on the number of rows used in column B you could try this.
Sub test()
' dimension the variable type
Dim lastRow As Long
' select cell "B1"
Cells(1, 2).Select
' jump to the last consecutive row in column B
Selection.End(xlDown).Select
' collect the row number into a variable
lastRow = ActiveCell.Row
' paste the equation into the variable length range
Range(Cells(1, 1), Cells(lastRow, 1)) = "=LEFT(RC[1],3)"
End Sub
Thanks Todd and user2063626,
I decided on a simpler approach. I only needed to obtain the last row in order to set my selection area; the number of the last row is not used in the actual values to be written. The files to be manipulated are flat ascii exports; the column layout is constant, only the number of rows is variable.
After writing the formula to A1, I move down column B and test for a value one cell at a time; if TRUE, copy the formula to the left adjacent cell; if FALSE, end process.
Sub FillClientCodes()
Range("A1").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[1],3)"
ActiveCell.Select
Selection.Copy
ActiveCell.Offset(0, 1).Select
ActiveCell.Offset(1, 0).Select
CheckCell:
ActiveCell.Activate
If ActiveCell.Value <> 0 Then
ActiveCell.Offset(0, -1).Select
ActiveCell.Activate
ActiveSheet.Paste
ActiveCell.Offset(0, 1).Select
ActiveCell.Offset(1, 0).Select
GoTo CheckCell
Else: GoTo EndOfData
End If
EndOfData:
End Sub
It's not elegant - it runs slower than a single select and paste - but it works, and it will work on all the files I need to process. Thanks again.