Copy a specific cell data into column of other sheet - excel

I have two worksheets, named "Monthly" and "Index", in a workbook.
In Index, cell A1 will have a value after some calculations.
I need to copy that value into "Monthly" Column "J".
It shall be one by one using next row coding.
Private Sub CommandButton2_Click()
Dim i As Integer
a = Worksheets("Monthly").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
Range("K" & i).Copy Worksheets("Rule of 78").Range("D6")
'Range("A1").Offset(i - 1, 0).Copy Range("C1")
Range("I" & i).Copy Worksheets("Rule of 78").Range("D7")
Range("E" & i).Copy Worksheets("Rule of 78").Range("D8")
Range("A" & i).Copy Worksheets("Rule of 78").Range("D10")
Worksheets("Index").Range("C2").Copy " How to paste in Monthly sheet
column J, for every row, C2 is different"
Next i
End Sub

Does "A1" have a formula? If so Excel is probably copying a formula, thus the error.
Try:
Worksheets("Index").Range("A1").Copy
Worksheets("Monthly").Range("J" & i).PasteSpecial xlPasteValues
Edit: "A1" or "C2", I didn't get wich is giving you the error.

Related

Copy Paste a variable range with variable row number i to another cell

The following code is working in my mind but not in vba:
Dim i As Integer
If Range("H11") = i Then Range("U" & i & ":X" & i).Select
Selection.Copy
lrij = Cells(Rows.Count, "A").End(xlUp).Row
Range("A" & lrij + 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Range("H11").ClearContents
Range("A1").Activate
i is a variable row number.
The purpose is if I enter "2" for example in Cell "H11", the Range ("U2:X2") will be copy pasted in Column "A", row under row.
braX already helped me with writing the variable range correctly, but I'm lacking the point here I guess. He advised me to use the "Long" data type.
Thank you in advance.

I do not understand why my VBA code doesn't work for Excel tables with only one row

clever minds.
I've been working on my code for a while now and there is something weird I do not understand.
But first, let me explain to you what is the situation and what I want my VBA code to do.
I have an Excel spreadsheet with several sheets. One sheet = one business unit of my company.
In each of these sheets, there is a table with data related to the business unit.
I've been coding a VBA program whose goal is to take the data from each table and put them in the same table in a sheet named "All".
It "works" but there is one scenario that can break my code. It is when one of my tables has only one row of data.
Everything works when my tables have no data at all or at least 2 or more. But not if one of them has only one row. And I don't understand why.
Here is my full code:
Sub Final_Macro()
'
' Total_Test1 Macro
'Voir clearcontent pour effacer
Dim LastRow As Long
Dim LastRowCount As Long
Sheets("All").Select
Range("A3:DZ50000").ClearContents
Application.Goto Reference:="Ref_EPG" 'Goes to the Ref_EPG table and selects it
If Not IsEmpty(ActiveCell.Value) Then
Selection.Copy 'Copies the Ref_EPG table
Sheets("All").Select 'Goes to the "All" tab
Range("A3").Select 'Selects cell A3
ActiveSheet.Paste 'Paste the Reference Numbs in the rows
Range("B3").Select 'Selects cell B3
Application.CutCopyMode = False 'Clears the clipboard
ActiveCell.FormulaR1C1 = _
"=IF(LEN(VLOOKUP(RC1,EPG_Table,EPG!R1C,FALSE))=0,"""",VLOOKUP(RC1,EPG_Table,EPG!R1C,FALSE))" 'Sets up the VLOOKUP function and if there is nothing in the source cell, returns blank instead of 0.
LastRowCount = Sheets("All").Range("A" & Rows.Count).End(xlUp).Rows.Count 'Determines the last row used in column A
LastRow = Sheets("All").Range("A" & Rows.Count).End(xlUp).Row
If LastRowCount > 1 Then
Sheets("All").Range("B3").AutoFill Destination:=Sheets("All").Range("B3:B" & LastRow) 'Autofills the VLOOKUP function from B3 to the last used row of Ref_Numbers
End If
End If
'Now let's do the same with Insulation
Dim LastRow2 As Long
Application.Goto Reference:="Ref_IG" 'Goes to the Ref_IG table and selects it
If Not IsEmpty(ActiveCell.Value) Then
Selection.Copy 'Copies the Ref_IG table
Sheets("All").Select 'Goes to the "All" tab
Range("A" & Rows.Count).End(xlUp).Offset(1).Select 'For Ref_Numbers This function is used to automatically go to the last cell of the table that doesn't have content in it. Should help avoiding overwriting rows
ActiveSheet.Paste 'Paste the Reference Numbs in the rows
'ActiveCell.Offset(0, 1).Select this line is a possible alternative to the next one. The result is the same but just works differently
Range("B" & Rows.Count).End(xlUp).Offset(1).Select 'Selects the last cell of column B that is not used yet
ActiveCell.FormulaR1C1 = _
"=IF(LEN(VLOOKUP(RC1,IG_Table,IG!R1C,FALSE))=0,"""",VLOOKUP(RC1,IG_Table,IG!R1C,FALSE))" 'Sets up the VLOOKUP function and if there is nothing in the source cell, returns blank instead of 0.
Application.CutCopyMode = False 'Clears the clipboard
LastRow2 = Sheets("All").Range("A" & Rows.Count).End(xlUp).Rows.Count 'Determines the last row used in column A
Range("B" & Rows.Count).End(xlUp).Offset(0).Select
If LastRow2 - LastRow > 1 Then
ActiveCell.AutoFill Destination:=Sheets("All").Range(ActiveCell.Address & ":B" & LastRow2) 'Autofills the VLOOKUP function from the last B cell used to the last used row of Ref_Numbers
End If
End If
'Now let's do the same with Gypsum
Dim LastRow3 As Long
Application.Goto Reference:="Ref_GYPCLG" 'Goes to the Ref_GYP table and selects it
If Not IsEmpty(ActiveCell.Value) Then
Selection.Copy 'Copies the Ref_GYPCLG table
Sheets("All").Select 'Goes to the "All" tab
Range("A" & Rows.Count).End(xlUp).Offset(1).Select 'For Ref_Numbers This function is used to automatically go to the last cell of the table that doesn't have content in it. Should help avoiding overwriting rows
ActiveSheet.Paste 'Paste the Reference Numbs in the rows
Range("B" & Rows.Count).End(xlUp).Offset(1).Select 'Selects the last cell of column B that is not used yet
Application.CutCopyMode = False 'Clears the clipboard
LastRow3 = Sheets("All").Range("A" & Rows.Count).End(xlUp).Rows.Count 'Determines the last row used in column A
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell.FormulaR1C1 = _
"=IF(LEN(VLOOKUP(RC1,GYPCLG_Table,GYPCLG!R1C,FALSE))=0,"""",VLOOKUP(RC1,GYPCLG_Table,GYPCLG!R1C,FALSE))" 'Sets up the VLOOKUP function and if there is nothing in the source cell, returns blank instead of 0.
If LastRow3 - LastRow2 > 1 Then
ActiveCell.AutoFill Destination:=Sheets("All").Range(ActiveCell.Address & ":B" & LastRow3) 'Autofills the VLOOKUP function from the last B cell used to the last used row of Ref_Numbers
End If
End If
'Now let's do the same with Glass Solutions
Dim LastRow4 As Long
Application.Goto Reference:="Ref_GLASS" 'Goes to the Ref_GLASS table and selects it
If Not IsEmpty(ActiveCell.Value) Then
Selection.Copy 'Copies the Ref_GLASS table
Sheets("All").Select 'Goes to the "All" tab
Range("A" & Rows.Count).End(xlUp).Offset(1).Select 'For Ref_Numbers This function is used to automatically go to the last cell of the table that doesn't have content in it. Should help avoiding overwriting rows
ActiveSheet.Paste 'Paste the Reference Numbs in the rows
Range("B" & Rows.Count).End(xlUp).Offset(1).Select 'Selects the last cell of column B that is not used yet
Application.CutCopyMode = False 'Clears the clipboard
LastRow4 = Sheets("All").Range("A" & Rows.Count).End(xlUp).Rows.Count 'Determines the last row used in column A
'Range("B" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell.FormulaR1C1 = _
"=IF(LEN(VLOOKUP(RC1,GLASS_Table,GLASS!R1C,FALSE))=0,"""",VLOOKUP(RC1,GLASS_Table,GLASS!R1C,FALSE))" 'Sets up the VLOOKUP function and if there is nothing in the source cell, returns blank instead of 0.
If LastRow4 - LastRow3 > 1 Then
ActiveCell.AutoFill Destination:=Sheets("All").Range(ActiveCell.Address & ":B" & LastRow4) 'Autofills the VLOOKUP function from the last B cell used to the last used row of Ref_Numbers
End If
End If
'Now let's do the same with Holdings
Dim LastRow5 As Long
Application.Goto Reference:="Ref_HOLD" 'Goes to the Ref_GLASS table and selects it
If Not IsEmpty(ActiveCell.Value) Then
Selection.Copy 'Copies the Ref_INSU table
Sheets("All").Select 'Goes to the "All" tab
Range("A" & Rows.Count).End(xlUp).Offset(1).Select 'For Ref_Numbers This function is used to automatically go to the last cell of the table that doesn't have content in it. Should help avoiding overwriting rows
ActiveSheet.Paste 'Paste the Reference Numbs in the rows
Range("B" & Rows.Count).End(xlUp).Offset(1).Select 'Selects the last cell of column B that is not used yet
Application.CutCopyMode = False 'Clears the clipboard
LastRow5 = Sheets("All").Range("A" & Rows.Count).End(xlUp).Rows.Count 'Determines the last row used in column A
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell.FormulaR1C1 = _
"=IF(LEN(VLOOKUP(RC1,HOLD_Table,HOLD!R1C,FALSE))=0,"""",VLOOKUP(RC1,HOLD_Table,HOLD!R1C,FALSE))" 'Sets up the VLOOKUP function and if there is nothing in the source cell, returns blank instead of 0.
If LastRow5 - LastRow4 > 1 Then
ActiveCell.AutoFill Destination:=Sheets("All").Range(ActiveCell.Address & ":B" & LastRow5) 'Autofills the VLOOKUP function from the last B cell used to the last used row of Ref_Numbers
End If
End If
Sheets("All").Select
Dim LastRow6 As Long
Dim LastColumn As Long
LastColumn = Sheets("All").Range("B2:DC2" & Columns.Count).End(xlToLeft).Row
LastRow6 = Range("B" & Rows.Count).End(xlUp).Row
Range("B3:B" & LastRow6).Select
Selection.AutoFill Destination:=Sheets("All").Range(ActiveCell.Address & ":DC" & LastRow6), Type:=xlFillDefault 'Autofills the formulas in the column selected to the right untill column DB which is the last header of the table
Range("B3").Select
End Sub
And here's the part of the code that I want to change because it doesn't work when my table has only one row of data :
Dim LastRow5 As Long
Application.Goto Reference:="Ref_HOLD" 'Goes to the Ref_HOLD table and selects it
If Not IsEmpty(ActiveCell.Value) Then
Selection.Copy 'Copies the Ref_INSU table
Sheets("All").Select 'Goes to the "All" tab
Range("A" & Rows.Count).End(xlUp).Offset(1).Select 'For Ref_Numbers This function is used to automatically go to the last cell of the table that doesn't have content in it. Should help avoiding overwriting rows
ActiveSheet.Paste 'Paste the Reference Numbs in the rows
Range("B" & Rows.Count).End(xlUp).Offset(1).Select 'Selects the last cell of column B that is not used yet
Application.CutCopyMode = False 'Clears the clipboard
LastRow5 = Sheets("All").Range("A" & Rows.Count).End(xlUp).Rows.Count 'Determines the last row used in column A
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell.FormulaR1C1 = _
"=IF(LEN(VLOOKUP(RC1,HOLD_Table,HOLD!R1C,FALSE))=0,"""",VLOOKUP(RC1,HOLD_Table,HOLD!R1C,FALSE))" 'Sets up the VLOOKUP function and if there is nothing in the source cell, returns blank instead of 0.
If LastRow5 - LastRow4 > 1 Then
ActiveCell.AutoFill Destination:=Sheets("All").Range(ActiveCell.Address & ":B" & LastRow5) 'Autofills the VLOOKUP function from the last B cell used to the last used row of Ref_Numbers
End If
End If
This portion of my code is used multiple times throughout my program for each business unit of my company.
Everything runs fine in this code until I reach this part of the code:
If LastRow5 - LastRow4 > 1 Then
ActiveCell.AutoFill Destination:=Sheets("All").Range(ActiveCell.Address & ":B" & LastRow5) 'Autofills the VLOOKUP function from the last B cell used to the last used row of Ref_Numbers
End If
The idea here is that I want to put a VLOOKUP formula in my cell (in column B), and if my source table has several rows, the VBA program is supposed to drag the VLOOKUP formula down for each lines that I have in my source table.
To know how many cells needs be filled with the formula, I use two variables, LastRow5 and LastRow4.
By making the difference between these two variables, I know how many cells need to be filled when dragging the VLOOKUP formula down.
That is why I say, if LastRow5 - LastRow4 > 1. Because If there is only one line in my source table, there is no need to drag the formula down.
Do you have any idea why this code doesn't work when my source table has only one row of data? There is something wrong in my code but I don't know what.
Thank you for your help, if you need any additional information to better understand the situation, do not hesitate.
Your calculation of the last row is incorrect:
LastRow5 = Sheets("All").Range("A" & Rows.Count).End(xlUp).Rows.Count
should be
LastRow5 = Sheets("All").Range("A" & Rows.Count).End(xlUp).Row
There is no need to AutoFill, or to Select, or to use ActiveCell. Write the formula to the entire range in one step:
With Sheets("All")
Dim LastRow As Long
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
Dim FirstRow As Long
FirstRow = .Range("B" & .Rows.Count.End(xlUp).Offset(1).Row
.Range("B" & FirstRow & ":B" & LastRow).FormulaR1C1 = _
"=IF(LEN(VLOOKUP(RC1,HOLD_Table,HOLD!R1C,FALSE))=0,"""",VLOOKUP(RC1,HOLD_Table,HOLD!R1C,FALSE))"
End With

vba, sum based on column header

I need your help with VBA!
I want to write a code that will sum the "sales" column in different 7 sheets. The problem is that the column has a different location in each sheet and a dinamic rows' count. The sum should be in the last row + 1.
I am not very good at macros, but I guess I should start with checking i to 7 sheets. Then I should sum a range based on the header ("Sales"). I am lost about how to write all of this..
Try the next code, please:
Sub SumSales()
Dim sh As Worksheet, rngS As Range, lastRow As Long
For Each sh In ActiveWorkbook.Sheets 'iterate through all sheets
'find the cell having "Sales" text/value
Set rngS = sh.Range(sh.Cells(1, 1), sh.Cells(1, _
sh.Cells(1, Columns.count).End(xlToLeft).Column)).Find("Sales")
'if the cell has been found (the cell range is NOT Nothing...)
If Not rngS Is Nothing Then
'Determine the last row of the found cell column:
lastRow = sh.Cells(Rows.count, rngS.Column).End(xlUp).row
'Write the Sum formula in the last empty cell:
rngS.Offset(lastRow).formula = "=Sum(" & rngS.Offset(1).address & _
":" & sh.Cells(lastRow, rngS.Column).address & ")"
sh.Range("A" & lastRow + 1).Value = "Sum of sales is:"
Else
'if any cell has been found, it returns in Immediate Window (Being in VBE, Ctrl + G) the sheet names not having "Sales" header:
Debug.Print "No ""Sales"" column in sheet """ & sh.name & """."
End If
Next
End Sub

Copy and Pasting Special?

I am writing a code to perform a formula on column "K", change its format and then copy and paste it in column "A".
I am also trying to copy and paste column "I" to column "B". "i" determines the numebr of cells in column B.
here is my code so far:
Sub Test()
Dim i As Long
i = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
With Range("K3:K" & i)
.Formula = "=DATE(A3,G3,H3)"
.NumberFormat = "ddmmmyyyy"
.Copy
Range("A3:A" & i).PasteSpecial xlPasteFormats
End With
With Range("I3:I" & i)
.Copy
Range("B3:B" & i).PasteSpecial xlPasteFormats
End With
End Sub
Any ideas where I went wrong? I am new to VBA so this is most likely a minor mistake I am overlooking.
Edit: The adjusted formula is copying Column I to Column B properly, but Column K to Column A is wrong.
Lets say column K has the dates:
29Apr1921
08May1922
21Oct1923
Column A now has:
04Apr1905
05Apr1905
06Apr1905
The issue is that it was missing paste values and was only pasting the format. It also needed its own With statement after the Date formula was performed.
Sub Test()
Dim i As Long
i = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
With Range("K3:K" & i)
.Formula = "=DATE(A3,G3,H3)"
.NumberFormat = "ddmmmyyyy"
End With
With Range("K3:K" & i)
.Copy
Range("A3:A" & i).PasteSpecial xlPasteValues
Range("A3:A" & i).PasteSpecial xlPasteFormats
End With
With Range("I3:I" & i)
.Copy
Range("B3:B" & i).PasteSpecial
End With
End Sub

Excel 2010 VBA Help Copying Ranges of Columns

My code works almost as desired. It checks all sheets for a certain value in Column "F" and then copies its associated row to the active sheet. I can make the code copy the entire row, singular columns "A", or sequential ranges "A:C". I cannot seem to make it copy specific columns like "A" "C" & "F" which is what I need it to do.
Public Sub List()
Dim ws As Worksheet
Dim i As Integer
ActiveSheet.Rows("3:" & ActiveSheet.Rows.Count).Clear
Selection.Clear
For Each ws In Worksheets
If ws.Name <> ActiveSheet.Name Then
For i = 1 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
If ws.Cells(i, "F").Value = "Pending" Then
ws.Cells(i, "A").Columns("A:D").Copy Destination:=ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
End If
Next
End If
Next
As following from comments to Question, correct answer is to use following line:
ws.Range("A" & i & ", C" & i & ", E" & i).Copy Destination:=ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
instead
ws.Cells(i, "A").Columns("A:D").Copy Destination:=ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

Resources