I have this macro which works ok on the first row, but once it has completed I want it to run again on the next row down and paste the result on the next row down on the "results" sheet and continue the process through the whole document until it reaches the last record - (there are approx. 5300 records in my spreadsheet)
Sub Macro2()
' Macro2 Macro
Range("A2:BW2").Select
Selection.Copy
Sheets("Lookup").Select
Range("F3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("F3:V3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Result").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
So basically I am copying the first row of data on sheet named "amps_job_history", it them pastes this data into a sheet called "lookup", once the data is pasted there a formula does a calculation that marries the data up with data from another worksheet. I then want to copy the original data plus the extra 3 columns that have been connected to the data with the formulas and the paste it into the sheet called "result". I then want it to go back to the first sheet "amps_job_hisotry" move down to the next row of data and repeat the process and when it pastes the data into the "result" page it need to past on the next row down and so on and so on until it reaches the last record.
I think this loop is what you are looking for.
Sub Macro2()
' Macro2 Macro
Dim rw As Long
With Worksheets("amps_job_history")
For rw = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
With Intersect(.Range("A:BW"), .Rows(rw))
Worksheets("Lookup").Range("F3").Resize(.Rows.Count, .Columns.Count) = .Value
End With
With Worksheets("Lookup")
With .Range("F3:V3")
Worksheets("Result").Range("A1").Offset(rw - 1, 0).Resize(.Rows.Count, .Columns.Count) = .Value
End With
End With
Next rw
End With
End Sub
The rows from the source data in the amps_job_history and the destination Result worksheet are shifted down one more row on each loop. The transitional F3:V3 range in the Lookup worksheet remains the same through out.
I've use direct value transfer rather than copy, paste special, values and the With ... End With statement provide explicit parent worksheet referencing without the use of the Range .Select or Range .Activate methods.
Related
I'm trying to join the information of different sheets, into only one sheet. In my code, i first create a variable j that will have the value of a cell in Sheets("Folha2") (this cell only count how many rows are filled in the first column of the Sheets("Folha1"), to understand in which line can i start to paste the data from another sheet). So i only paste the data from the sheet "Portugal" to Sheet "Folha1", and after i try to paste the data from the sheet "Itália" starting in cells( j, 1).
The error is
Method or data member not found
What did I do wrong?
Sub Macro2()
Dim j As Integer
j = Sheets("Folha2").Range("A1").Value + 1
Range(Sheets("Portugal").Range("A1"), Sheets("Portugal").Range("A1").End(xlToRight).End(xlDown)).Copy
Range(Sheets("Folha1").Cells(j, 1)).Paste
Range(Sheets("Itália").Range("A1"), Sheets("Itália").Range("A1").End(xlToRight).End(xlDown)).Copy
Range(Sheets("Folha1").Cells(j, 1)).Paste
End Sub
This might do it:
Sub Macro4()
Range(Sheets("Portugal").Range("A1"), Sheets("Portugal").Range("A1").End(xlToRight).End(xlDown)).Copy
Sheets("Folha1").Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range(Sheets("Itália").Range("A2"), Sheets("Itália").Range("A2").End(xlToRight).End(xlDown)).Copy
Sheets("Folha1").Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
Not quite sure what you're doing, but if I'm right you can reduce your code to this:
Sub Macro2()
Dim j As Long
'j = Sheets("Folha2").Range("A1").Value + 1
Sheets("Portugal").Range("A1").CurrentRegion.Copy Sheets("Folha1").Cells(Rows.Count, 1).End(xlUp)(2)
Sheets("Italia").Range("A1").CurrentRegion.Copy Sheets("Folha1").Cells(Rows.Count, 1).End(xlUp)(2)
End Sub
In Excel 2007 there was no issue but when I hit the below line in Excel 2016, it now takes over a minute each time. There are only 300 rows in the column. All I want to do is cut a column and paste it next to another column.
Selection.Insert Shift:=xlToRight
Sample code is as follows, but I have 30 odd of these so it is taking half an hour.
Columns("E:E").Select
Selection.Cut
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Any ideas why?
Do you have formulas and maybe even external references? Then please try this:
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever
ActiveWorkbook.UpdateRemoteReferences = False
Application.Calculation = xlManual
ActiveSheet.Columns("E:E").Cut
ActiveSheet.Columns("C:C").Insert Shift:=xlToRight
ActiveWorkbook.UpdateLinks = xlUpdateLinksAlways
ActiveWorkbook.UpdateRemoteReferences = True
Application.Calculation = xlCalculationAutomatic
' If it's faster, then uncomment following line additionally
' Application.CalculateFull
I had to deal with an Excel xlsx file generated by a Payroll system. Not sure what is causing the slowness on column insert. Inserting a column into a 88,000 row file takes about 25 seconds.
I discovered that if I copy the entire worksheet to a new sheet as values and number formats, the insert column step will run almost instantly. The copy entire worksheet portion takes just 3 seconds! There are no formulas in the file nor conditional formatting.
This is the logic I used:
' Copy source worksheet
Dim rng As Range
Set rng = Worksheets("Sheet1").Cells
rng.Copy ' note that wks.Cells.Copy is very slow
' add new worksheet
Dim newWks As Worksheet
Set newWks = Sheets.Add(After:=ActiveSheet)
newWks.Name = "Values Only"
' paste values and number format into new worksheet
newWks.Range("A1").Select
' xlPasteValues is fast too
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' if I also include a xlPasteFormats then the insert column will become very slow once again
' Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' now the column insert is blazingly fast!
newWks.Columns(14).Insert
newWks.Cells(1, 14).Interior.ColorIndex = 35
' etc.
I created the below and i'm an absolute noob, i literally just pieced different bits of info together to get it working and it did, until i added the selecting the cell if it has 'r' in it and moving it to 'sheet7' and now i get an (object required) error when it runs.
I really need some help on this and if you are feeling generous, i would like to repeat the exercise with several other letters and sheets, so if you could demonstrate an additional one too, i'm sure i could work out the rest.
Thanks in advance
Sub Macro1()
'
' Macro1 Macro
'
'
Range("I16").Select
Selection.Copy
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Application.CutCopyMode = False
Selection.Copy
Range("J20").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=-27
Range("I16").Select
If Cell.Value = "R" Then
Range("J20").Select
Selection.Copy
Sheets("Sheet7").Select
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
End Sub
Generally when writing macros it's good practice to avoid selecting cells. Selecting a cell is really something that only a human does, but in VBA we can just refer to the cell directly without touching it.
Rewriting your macro to avoid all the touching:
Sub Macro1()
'Make a variable to store the cell found
Dim lastCell as Range
'find the last cell in Column A of the active sheet
lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1)
'Paste in the I16 value
lastCell.value = RangE("I16").value
'Grab whatever is hanging out in Column B next to the last cell and stick it in J20
Range("J20").value = lastCell.Offset(0,1).value
'Test to see if I16 has value "R"
If Range("I16").value = "R" Then
'Find the last row in Sheet7, Column B and store it to the variable
lastCell = Range("B" & Rows.Count).End(xlUp).Offset(1)
'Copy J20 value to the lastCell in Sheet 7, Column B
lastCell = Range("J20").value
End if
End Sub
I'm not sure where you were getting the error you reported. It's probably specific to your workbook, so we'd have to be sitting in front of it to track it down. This rewrite may correct it though.
Also, it's not clear what else is happening in this process. My guess is that Column B of the Active Sheet has a formula in it that does something to the value we paste from I16. After it's pasted and calculated we grab that and stick it in J20 and if I16 is equal to "R" then we put that calculated J20 value over Sheet7. If that sounds about right, then the macro above should do the trick.
Also, if that IS what's happening, then perhaps you could share the formula you have in Column B. We can probable to do that calculation within VBA and save a ton of steps in this macro.
I'm making my first macro in order to save having to perform 2500 copy-pastes. I have a long and complicated worksheet that takes two variables as inputs and returns a single value, and another sheet with 2500 pairs of these variables.
To keep things in the same sheet, I've linked the formula sheet inputs to J2 and K2 on my variable sheet, and the output to L2. My goal is to populate a third column next to the first two with the results for that row, by copying the two values to J2 & K2, then copying from L2 to the appropriate cell in the third column. As my macro is currently, it returns to the same cell in the third column every time, based on an offset from L2 as the last active cell.
I've tried searching for help on how to either increment the last paste operation, or to keep the active cell referencing the start point of the macro in order to keep things in the same row, but was unsuccessful. Any help would be appreciated.
ActiveCell.Range("A1:B1").Select
Selection.copy
Range("J2:K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("L2").Select
Application.CutCopyMode = False
Selection.copy
ActiveCell.Offset(43, -5).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sub TT()
Dim sht As Worksheet, c As Range
Set sht = ActiveSheet
For Each c In sht.Range("A1:A2500").Cells
sht.Range("J2").Value = c.Value
sht.Range("K2").Value = c.Offset(0, 1).Value
sht.Calculate
c.Offset(0, 2).Value = sht.Range("L2").Value
Next c
End Sub
I am new to this forum and just starting to learn programming. I had a question that hopefully will be useful to others as well. I have data saved in two columns and 30 rows that ends with a summation of each column at the bottom.
What I would like to do is create a VB program that saves the Sum, moves that value to a new row, and then clears all of the previous data so that it can be re-entered and again saved in a new location.
This program will be used to track emissions of vapor from a storage tank, monthly and as the data is entered each month I was hoping it could be saved specific to that month and then cleared and ready to enter the data into the next month. Thank you for any help or ideas you could provide.
This is the code I have come up with thus far,
Sub Macro4()
Range("F15").Select
ActiveWindow.SmallScroll Down:=15
Range("C30:D30").Select
Selection.Copy
Range("C33").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("C33").Select
End Sub
Sub Button2_Click()
ActiveWindow.SmallScroll Down:=18
Range("C30:D30").Select
Selection.Copy
Range("C33").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C28:D29").Select
Range("D29").Activate
Application.CutCopyMode = False
Range("C2:D29").Select
Range("D29").Activate
Selection.ClearContents
End Sub
Something like this should work (adjust ranges to suit)
Sub Button2_Click()
Dim rngDest as Range
With ActiveSheet
'find next empty row
Set rngDest= .cells(rows.count,3).end(xlup).offset(1,0)
'copy values
rngDest.resize(1,2).value= .Range("C30:D30").Value
'clear input range
.Range("C28:D29").ClearContents
End With
End Sub