How to copy data from 2 cells from workbook A and copy to workbook B in a cell and how do I start a for loop until last row/column - excel

I have two questions
How to combine data using two of the cells from workbookA and copy to workbookB on the same cell?
How do I start using for loop to copy it until the last row/column?
I have no clue on how to combine the data and I do not know where to place the variable inside the code for it to loop until its last column.
Dim Tlastrow As Integer
Tlastrow = Cells(1, Columns.Count).End(xlToLeft).Column
For r = 1 To Tlastrow
Workbooks("InputB.xls").Worksheets("HC_MODULAR_BOARD_20180112").Range("F3:G3").Copy _
Workbooks("Output.xls").Worksheets("Sheet1").Range("I3")
Next

Try this:
Option Explicit
Sub Paste()
Dim wsInput As Worksheet, wsOutput As Worksheet, LastRow As Long, C As Range
Set wsInput = Workbooks("InputB.xls").Worksheets("HC_MODULAR_BOARD_20180112")
Set wsOutput = Workbooks("Output.xls").Worksheets("Sheet1")
With wsInput
LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row 'Last Row with data
For Each C In .Range("F3:F" & LastRow) 'loop for every row with data
wsOutput.Cells(C.Row, "I").Value = C & " " & C.Offset(0, 1)
Next C
End With
End Sub
This code is assuming you want to paste every row from your input workbook to the output workbook on the same rows, but merging F and G columns. It's just pasting the values, not formulas or formats.

Related

Trying to copy cell format from one sheet to another, cell by cell

Sub Start()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Sheets("1")
Set ws2 = Sheets("2")
Dim LastRow As Long, i As Long
LastRow = Cells(Rows.Count, "D").End(xlUp).row
For i = 1 To LastRow
ws1.Range("D" & i).copy
ws2.Range("A2").PasteSpecial Paste:=xlPasteFormats
Next i
If ws1.Range("A" & i) = vbNullString Then
Exit Sub
End If
End Sub
I'm trying to make it so this code pastes the interior fill from column D, Into Column A, unless there is already interior fill present.
Any input appreciated.
Edit: I'm trying to copy the format (interior fill) from Sheet 1 and Sheet 2's Column A's (until the last row) and paste it to Sheet Column A. I'm trying to copy over once cell at a time, and ignore if there is already fill present in sheet 3, because my I dont want the format from sheets 1 and 2 to overwrite eachtoher.

copy selected columns in a row to another sheet if a cell meets a condition

(not in a range, not adjacent columns)
(in given order)
I have many rows on Sheet1. I would like to copy some columns of a row (not the entire row and not a range of columns) to Sheet2 (to the first empty row of Sheet2) if a cell satisfies a condition (the cell in the current row and A column has a value of y)
I would like to copy not the entire row from Sheet1 only the row with those columns that are given on Sheet3 (Column A), and the new column number (on Sheet2) is also given on Sheet3 (column B)
It would be simple if my task would be to copy the entire row, or the selected column would be in a range...but i would need to copy those columns that are specialized on Sheet3. I would be grateful for any help. Thanks in advance.
Sheet1 shows an example data sheet. The criteria is if Cells(Rows, 1).Value = "y"
Sheet2 shows the desired result.
Sheet3 shows the selected column number on Sheet1 and the new column number on Sheet2
Whilst this probably should be done using arrays more, here's some basic VBA code that loops the first sheet checking for "y" in the first column. When it finds it, it then loops the column mappings in the third sheet that have been saved into arrays to set the values on the second sheet:
Sub sTranasferData()
On Error GoTo E_Handle
Dim aOld() As Variant
Dim aNew() As Variant
Dim wsIn As Worksheet
Dim wsOut As Worksheet
Dim wsTrack As Worksheet
Dim lngLastRow As Long
Dim lngLoop1 As Long
Dim lngLoop2 As Long
Dim lngRow As Long
Dim lngTrack As Long
Set wsIn = Worksheets("Sheet1")
Set wsOut = Worksheets("Sheet2")
Set wsTrack = Worksheets("Sheet3")
lngLastRow = wsIn.Cells(wsIn.Rows.Count, "A").End(xlUp).Row
lngTrack = wsTrack.Cells(wsTrack.Rows.Count, "A").End(xlUp).Row
aOld() = wsTrack.Range("A2:A" & lngTrack).Value
aNew() = wsTrack.Range("B2:B" & lngTrack).Value
lngRow = 1
For lngLoop1 = 2 To lngLastRow
If wsIn.Cells(lngLoop1, 1) = "y" Then
For lngLoop2 = LBound(aOld) To UBound(aOld)
wsOut.Cells(lngRow, aNew(lngLoop2, 1)) = wsIn.Cells(lngLoop1, aOld(lngLoop2, 1))
Next lngLoop2
lngRow = lngRow + 1
End If
Next lngLoop1
sExit:
On Error Resume Next
Set wsIn = Nothing
Set wsOut = Nothing
Set wsTrack = Nothing
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "sTransferData", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Regards,

Excel copy and paste rows using loop

my questing is about trying to copy and paste 4 rows (4-7) under each row for the entire sheet, maybe using loop. I have pasted the copied rows under row 8 & 13 as an example (I would like to be able to do that for the remaining sheet until the rows are empty). Your expertise is greatly appreciated.
enter image description here
To implement:
Create a second sheet (in my code, referenced as Sheet2)
Paste the rows on this sheet that are to be inserted on the first sheet
Run Macro
Option Explicit
Sub EnterRows()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<-- Update
Dim i As Long, LR As Long, myRange As Range
LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set myRange = ThisWorkbook.Sheets("Sheet2").Range("1:4")
Application.ScreenUpdating = False
For i = LR To 2 Step -1
myRange.Copy
ws.Range("A" & i + 1).Insert xlDown
Next i
Application.ScreenUpdating = True
End Sub

Excel - Not to copy entire row just rows from Colum E to K to another sheet

I found this Excel VBA code that's very short and clean and it does its job to copy the entire row based on a condition. I added it to my vba project and it works well the only problem is - it copies the entire row.
I am trying to implement the code to only copy rows from Column E to K, but
I have not been successful in tweaking the code to what I am trying to do.
Basically, in worksheet "verification" if Column "AB" = "Final" then copy each row that has "Final" from Column E to K to the worksheet "upload".
I am pulling my hair to make it work, and I have search everywehre and no luck. I know in this platform, I can find the solution to my problem.
I will keep on playing with the code.
Sub CopyEachRowtoUpload()
Application.ScreenUpdating = False
Dim wsI As Worksheet, wsO As Worksheet
Dim LastRow As Long, i As Long, j As Long
Dim LastColumn As Long, a As Long, b As Long
Dim rng As Range
Set wsI = Sheets("Verification")
Set wsO = Sheets("Upload")
Set rng = wsI.Range("E:K")
'Last Row in a Column. Row need to start in row 2
LastRow = wsI.Cells(Rows.Count, "K").End(xlUp).Row
'Last Column in a Row. Rows from Column E to K is what I want to copy
LastColumn = wsI.Cells(5, Columns.Count).End(xlToLeft).Column
'Row start
j = 2
With wsI
'Loop through each row
For i = 1 To LastRow
If Range("AB" & i).Value = "Final" Then
wsI.Rows(i).Copy
wsO.Rows(j).PasteSpecial Paste:=xlPasteValues
j = j + 1
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
Just use the Range.Cells property together with the Range.Resize property to locate and shape the source and target.
With wsI
'Loop through each row
For i = 1 To LastRow
If .Range("AB" & i).Value = "Final" Then
wsO.Cells(j, "A").Resize(1, 7) = .Cells(i, "E").Resize(1, 7).Value
j = j + 1
End If
Next i
End With
Direct value transfer is preferred over Copy, Paste Special, Values if all you want is the cell values.
btw, you were missing a . in .Range("AB" & i).Value.

Vba comparing then copying two different Sheets

I realize there are a few different similar ideas on here. But I need help with this simple compare function.
My goal is to compare two different cells and if they are the same, replace it with its full non-abbreviated name.
Thank you for your time!!!
I.E
Sheet1 Sheet2
Column H Column A Column B
Dept Dept Department
This is what I have (Yes simple), but the cell H is not updating to the non-abbreviation:
Sub updateDeptNames()
'Format user ID from the email column
Dim ws As Worksheet, ws2 As Worksheet
Dim LastRow As Long, i As Long
Dim tmpArray() As String, tempDept As String
Set ws = ActiveWorkbook.Sheets("Student_Travel_DB") '--> This is the relevant sheet
Set ws2 = ActiveWorkbook.Sheets("gokoutd") '--> This is the relevant sheet
LastRow = 1000 ''Bug finding the last row, had to hard code it
For i = 2 To LastRow 'Iterate through all the rows in the sheet
For j = 2 To 112
tempDept = ws2.Range("A" & j).Value
If ws.Range("H" & i).Value = tempDept Then
ws.Range("H" & i) = ws2.Range("B" & j).Value
End If
Next j
Next i
End Sub
You can more easily use VLOOKUP either on your worksheet or with VBA:
Sub GetFullName()
Dim cl As Range, data As Range, lookUpRng As Range
Set data = Worksheets("Student_Travel_DB").Range("A1:A10")
Set lookUpRng = Worksheets("gokoutd").Range("A1:B10")
On Error Resume Next
For Each cl In data
cl = WorksheetFunction.VLookup(cl, lookUpRng, 2, False)
Next cl
End Sub
You'll need to change your range references.

Resources