Copy and pasting data onto a different worksheet in excel - vba

I am trying to copy data from range C2:G2 in sheet1 tab onto sheet2 tab range C4:C8. Below is my code but it just pastes the value in cell c2 of sheet 1 repeatedly onto sheet 2 range C4:C8. Can you please help?
Also , I need to copy the value of cell c12 in sheet 1 onto e4 of sheet 2, f12 in sheet 1 onto e5 of sheet 2, i12 in sheet 1 onto e6 of sheet 2, L12 in sheet 1 onto e7 of sheet 2, O12 in sheet 1 onto e8 of sheet 2
Thanks in advance
Andy
Sub ticker1() 'COPY DATA FROM ROW ONE SHEET INTO A COLUMN on another sheet
Sheets("Sheet2").Range("C4:C8").Value = Sheets("Sheet1").Range("C2:G2").Value
End Sub

Your code was very close. In fact, it may have worked. I'm not sure. Using select & activate is inevitably an issue. Avoid it..
This isn't really an answer. Just some food for thought as you're learning to program. My comments start with {--
Option Explicit ' {--option explicit forces you to declare your variables. Do this. Always.
Sub ticker()
'{group your variable declarations together}
Dim numberOfCellsToCopy As Integer
Dim currentCopyColumn As Integer
Dim currentPasteRow As Integer
Dim i As Integer
' {--use a worksheet object instead of selecting}
Dim srcWs As Worksheet
Dim destWs As Worksheet
'assign where to insert the columns
currentCopyColumn = 2
currentPasteColumn = 2
numberOfCellsToCopy = 5
' {--Set worksheet objects}
Set srcWs = ThisWorkbook.Worksheets("HistoricalDataRequest")
Set destWs = ThisWorkbook.Worksheets("SomeOtherWorksheet")
For i = 2 To numberOfCellsToCopy 'loop increases by 1??? {--YES. It does.}
' {--indent your code so it's easier to see program flow
' {--I have no idea what you're trying to do here.... sorry. You're only copying one cell
' --so I really can't figure what you're trying to accomplish}
'Range(Cells(2, currentCopyColumn), Cells(2, currentCopyColumn)).Select 'number =row
'Selection.Copy 'number =row
'Range(Cells(7, currentPasteColumn), Cells(7, currentPasteColumn)).Select
'ActiveSheet.Paste
' {--copy/paste can be done in one line using a range object
' --range uses a cell name.}
srcWs.Range("B2").Copy Destination:=destWs.Range("B7")
' { --or more in line with your method like this}
srcWs.Range(srcWs.Cells(2, currentCopyColumn)).Copy Destination:=destWs.Range(destWs.Cells(7, currentPasteColumn))
'increase the column index values to go to the next one
currentCopyColumn = currentCopyColumn + 1
currentPasteColumn = currentPasteColumn + 4
Next i ' {--next i will increment the i var for you}
End Sub

based on your edited code, this should work:
Sheets("Sheet5").Range("C4:C8").Value = Application.WorksheetFunction.Transpose(Sheets("Sheet2").Range("C2:G2").Value)
As your arrays are in different orientations, the transpose function will align them correctly.
There are a few other ways of doing this, but this is probably the most direct (if you do not need too much flexibility with your ranges).

Related

VBA macro to fill in cell based on 2 different fields that match to a list of values

I have a Button I want to assign a macro to that, when clicked, enters in the number 1 into a specific cell on another sheet within the same workbook. This macro needs to a) match the value in cell B8 to a range of values in another sheet column (G11:G110) and also b) match the value in cell C4 to a range of values in another sheet rows (L4:FR4).
So if the value in B8 = 01234 it will do a vlookup type function on G11:G110. Once it finds its match, it then looks at the value in C4 = "Application" and finds it match in L4:FR4. I should note that there are no duplicate values in any field and the values in L4:FR4 are static, whereas the values in cells G11:G110 change upon refresh.
I have tried Select Case, If...And...Then... and the only thing I got to work was simply saying
If B8 = G11 Then blah blah L11
If B8 = G12 Then blah blah L12
Sub RectangleRoundedCorners1_Click()
Dim sourceSht As Worksheet: Set sourceSht = DataEntry
Dim destSht As Worksheet: Set destSht = Labels
Dim Selection As Range: Set Selection = DataEntry.Range("C5")' This is the # 1 I was talking about'
Dim Acct As Range: Set Acct = DataEntry.Range("B8")
Dim SpecErr As Range: Set SpecErr = DataEntry.Range("C4")
If (SpecErr.Value = Labels.Range("L4")) And (Acct.Value = Labels.Range("G11")) Then
Selection.Copy Destination:=Labels.Range("L11")
End If
If (SpecErr.Value = Labels.Range("M4")) And (Acct.Value = Labels.Range("G11")) Then
Selection.Copy Destination:=Labels.Range("M11")
End If
End Sub
I ran out of space in the code doing it like this, as I need to go from L4 to FR4, and then do this 100 times by adjusting G11 to G12 & L11 to L12 etc.
The sheet holding the main data and Button I called DataEntry, and the sheet that I need to lookup on and have the 1 entered on is called Labels.
I think this is what you need - you can just loop through all the columns (in your case, 12 to 175 represent L to FR), and then loop through rows 11 to 110. Once you get a match, the destination cell is the corresponding column and row to i and j:
Sub RectangleRoundedCorners1_Click()
Dim sourceSht As Worksheet: Set sourceSht = DataEntry
Dim destSht As Worksheet: Set destSht = Labels
Dim Selection As Range: Set Selection = DataEntry.Range("C5") ' This is the # 1 I was talking about'
Dim Acct As Range: Set Acct = DataEntry.Range("B8")
Dim SpecErr As Range: Set SpecErr = DataEntry.Range("C4")
Dim i As Long, j As Long
For i = 12 To 175 'L to FR
For j = 11 To 110
If SpecErr.Value = Labels.Cells(4, i) And Acct.Value = Labels.Cells(j, 7) Then
Selection.Copy Destination:=Labels.Cells(i, j)
End If
Next j
Next i
End Sub
Does this have to be VBA? You could do this with a formula. In sheet 'Labels' cell L11, use this formula and copy over and down:
=IF(AND(DataEntry!$B$8=$G11,DataEntry!$B$4=L$4),1,"")
If it must be VBA, then this can be done basically as a one-liner:
Sub RectangleRoundedCorners1_Click()
On Error Resume Next 'Ignore errors if any fields are not filled out
Sheets("Labels").Cells(Evaluate("MATCH(DataEntry!$B$8,Labels!$G:$G,0)"), Evaluate("MATCH(DataEntry!$B$4,Labels!$4:$4,0)")).Value = Sheets("DataEntry").Range("B5").Value
On Error GoTo 0 'Clear "On Error Resume Next" condition
End Sub

Excel copy cell values to other worksheet

I'm working on a macro to copy certain rows (if column A isn't blank) of worksheet 'A' to worksheet 'B'. After a little bit of research the following code suddenly appeared. Only thing that I don't seem to work out is to copy the cell values instead of the linked formula, I tried to implement the 'copy/paste special' command, but I don't get the specific coherent code language.
Sub Samenvattend()
'
' Samenvattend Macro
'
' Sneltoets: Ctrl+Shift+S
'
Dim a As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet
' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("gedetailleerde meetstaat")
Set Target = ActiveWorkbook.Worksheets("samenvattende meetstaat")
j = 1 ' Start copying to row 1 in target sheet
For Each a In Source.Range("A1:A10000") ' Do 10000 rows
If a <> "" Then
Source.Rows(a.Row).Copy Target.Rows(j)
j = j + 1
End If
Next a
End Sub
Thanks :)
If you're just trying to set the values of two cells equal, you can skip copy/paste, and simply set the ranges' values equal to one another.
This also lets you skip using the Clipboard and tends to be a little faster.
Just remember when doing this, it's [DESTINATION range].value = [ORIGIN range].value whereas with copy/paste, it's [ORIGIN range].copy [DESTINATION range].
For Each a In Source.Range("A1:A10000") ' Do 10000 rows
If a <> "" Then
Target.Rows(j).value = Source.Rows(a.Row).Value
j = j + 1
End If
Next a

how to copy & paste the data from one column to another between two sheets of excel workbook...without overwriting the destination column content..?

how to copy & paste the data from one column to another between two sheets of excel workbook ... without overwriting the destination column content?
I am using below code to copy & paste but every time I run it it is overwriting the existed content. I want to be pasted from next row of the column.
Sub DirectCopySample()
Application.ScreenUpdating = False
Sheets("Updating Sheet").Range("A:A").Copy Destination:=Sheets("Sheet1").Range("G:G")
Sheets("Updating Sheet").Range("B:B").Copy Destination:=Sheets("Sheet1").Range("F:F")
Sheets("Updating Sheet").Range("C:C").Copy Destination:=Sheets("Sheet1").Range("B:B")
Application.ScreenUpdating = True
End Sub
Don't copy the entire column. Copy a specific 1-cell-wide range of X rows (where X is your data) and define all your variables based on the current size of the data. For instance if you want to copy column A from sheet1 to the end of column B in sheet2.
Sub CopyColumn()
Dim wsCopy As Worksheet
Set wsCopy = Sheets("<Sheet Name>")
Dim wsPaste As Worksheet
Set wsPaste = sheets("<Sheet Name>")
'/ Much better to make your worksheets variables and then reference those
Dim lngFirstRow As Long
Dim lngFinalRow As Long
Dim lngCopyColumn As Long
Dim lngPasteColumn As Long
Dim rngCopy As Range
Dim rngPasteCell As Range
lngCopyColumn = 1 '/ ("A" Column)
lngDestinationColumn = 2 '/ ("B" Column)
wsCopy.Activate
lngFirstRow = 1
lngFinalRow = Cells(1048576, lngCopyColumn).End(xlUp).Row
'/ Starts at the bottom of the sheet, stops at the first cell with data in it, returns that cell's row
Set rngCopy = Range(Cells(lngFirstRow, lngCopyColumn), Cells(lngFinalRow, lngCopyColumn))
'/ Defines the range between those 2 cells
rngCopy.copy
wsPaste.Activate
lngFinalRow = Cells(1048576, lngPasteColumn).End(xlUp).Row
Set rngpaste = Cells(lngFinalRow + 1, lngPasteColumn)
'/ Pastes to the row 1 cell below the last filed cell in Column B
rngpaste.Paste
End Sub
#Grade 'Eh' Bacon outlined the correct process in his or her comment.
The crux of the issue is finding the size of the ranges you are copying from and pasting to. My current favorite method of doing so is the code snippet below:
copyLastrow = Sheets("Updating Sheet").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
That will find the last non-empty row in your worksheet. So if for some reason column A has 100 rows, B has 200 rows, and C has 300 rows it will return 300 as the last row.
On the paste side of things, you could use the same method and add 1 to it so you paste into the first empty row, but if the columns have different numbers of rows you will end up with many blank rows in the shorter columns before your data is pasted at the bottom.
A work around this is the following code:
pasteLastrowG = Sheets("Sheet1").Range("G" & Rows.Count).End(xlUp).Row + 1
This will start at the bottom of column G and head up until it hits a row with data in it and then add 1 so that you are pasting into the first blank row of the column. You could then create variables for columns H and I that do the same thing.
Putting it all together your code would look something like this in the end:
copyLastrow = Sheets("Updating Sheet").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
pasteLastrowG = Sheets("Sheet1").Range("G" & Rows.Count).End(xlUp).Row + 1
'pasteLastrowH ...
'pasteLastrowI ...
Sheets("Updating Sheet").Range("A2:A" & copyLastrow).Copy Destination:=Sheets("Sheet1").Range("G" & pasteLastrowG)
'Copy and paste B code here
'Copy and paste C code here

Copy range of cells including border formatting x number of times

I have a sheet which contains rows of information which include borders. My information is in rows 1 to 3 on sheet2. I would like to make a macro to copy this information onto sheet3 and paste it into row1 on sheet3 then into row4 on sheet3 then in row8 on sheet2 and then do the same further down the sheet and copy it over and over again until I have the correct amount of copies that I have specified in a cell C5 on sheet1. So if I have x in a cell C5 on sheet1 I would like to be able to paste the row of information including boarders onto sheet3 x times. I have a basic understanding of VBA and have looked at other examples but cannot work out how to do this and retain the borders.
I hope this is an explanation that makes sense. Is this possible?
Thanks Greg
Try this
Notes:
Change copy area on Sheet 2: FIRST_ROW, FIRST_COL, TOTAL_ROWS, and TOTAL_COLS
Cell C5 is expected to be on Sheet 1
Sheet 3 will be deleted every time you run this macro
It's hard to read to keep it shorter, but I can provide details if interested
Option Explicit
Public Sub copyRange()
Const FIRST_ROW As Long = 1: Const TOTAL_ROWS As Long = 3
Const FIRST_COL As Long = 1: Const TOTAL_COLS As Long = 3
Dim totalCopies As Long, i As Long, sh As Worksheet
Application.ScreenUpdating = False
Sheets("Sheet3").Cells.Delete
totalCopies = Sheets("Sheet1").Range("C5").Value2
With Sheets("Sheet2") 'COPY
.Range( _
.Cells(FIRST_ROW, FIRST_COL), _
.Cells(FIRST_ROW + TOTAL_ROWS - 1, TOTAL_COLS) _
).Copy
End With
For i = 1 To totalCopies 'PASTE * value in C5
Sheets("Sheet3").Cells(((TOTAL_ROWS + 1) * (i - 1)) + 1, FIRST_COL).PasteSpecial
Next
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
End Sub

Excel Macro to search Text in column A and copy adjacent cell from column B to another workbook

I am new to VBA and excel macro, I want to create a macro which will search for text in Column and if found will copy corresponding value from column B to another workbook.
Example: Macro will search for fixed "TextA3" from column A and if found will copy value from B3 (which is TextB3 in below example)and will paste to another workbook.
TextA1 TextB1
TextA2 TextB2
TextA3 TextB3
TextA4 TextB4
TextA5 TextB5
TextA6 TextB6
TextA7 TextB7
Tried searching this website but unable to figure out how to copy adjacent cell.
I am working with below code:
Sub Luxation2()
Dim K As Long, r As Range, v As Variant
K = 1
Dim w1 As Worksheet, w2 As Worksheet
Set w1 = Sheets("raw data")
Set w2 = Sheets("data manipulation")
w1.Activate
For Each r In Intersect(Range("A:A"), ActiveSheet.UsedRange)
v = r.Value
If InStr(v, "Ticket No. ") > 0 Then
r.Copy w2.Cells(K, 1)
K = K + 1
End If
Next r
End Sub
Many thanks! in advance.
I myself am new to Excel-VBA but I guess since there is no answer yet I'll try to answer.
I am not entirely sure if I fully comprehend your problem. What I understood was that you want to copy a cell next to the cell you have selected.
You could try to use
w2.cells(K, 1).offset(Column, Row)
I hope I could help and it is not entirely wrong, otherwise please correct me :)