Trying to copy and paste some variable data - excel

i'm working on a existing excel file with lots of macros and want to copy and paste variable data from 5 different sheets to 5 other sheets without copying blank cells. this is what i made so far and gives me Runtime error 1004:
Sub Macro1()
Sheets("Hulp_IO").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("IO").Select
Range("B2").Select
ActiveSheet.Paste
Sheets("Hulp_Modbus_PMSX_Lees_Tags").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modbus_Lees_Tags_PMSX").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("Hulp_Modbus_PMSX_Schrijf_Tags").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modbus_Schrijf_Tags_PMSX").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("Hulp_Modbus_Pakscan_Lees_Tags").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modbus_Lees_Tags_PackScan").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("Hulp_Modbus_Pakscan_Schrijf_Tag").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Modbus_Schrijf_Tags_PackScan").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("Start").Select
End Sub

Here is your code largely reviewed, because the .Select command is really ressource-greedy and now it is far more readable!
I don't know on which line you had the error with your code but it is an important information, so add it even if this solve your problem! ;)
Here is the code :
Sub Nito_Nascimento()
Dim WsFrom As Worksheet, _
WsTo As Worksheet
Set WsFrom = Sheets("Hulp_IO")
Set WsTo = Sheets("IO")
WsFrom.Range("A1", WsFrom.Range("A" & WsFrom.Rows.Count).End(xlUp)).Copy
WsTo.Range("B2").PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=True, _
Transpose:=False
Set WsFrom = Sheets("Hulp_Modbus_PMSX_Lees_Tags")
Set WsTo = Sheets("Modbus_Lees_Tags_PMSX")
Application.CutCopyMode = False
WsFrom.Range("A1", WsFrom.Range("A" & WsFrom.Rows.Count).End(xlUp)).Copy
WsTo.Range("A2").PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=True, _
Transpose:=False
Set WsFrom = Sheets("Hulp_Modbus_PMSX_Schrijf_Tags")
Set WsTo = Sheets("Modbus_Schrijf_Tags_PMSX")
Application.CutCopyMode = False
WsFrom.Range("A1", WsFrom.Range("A" & WsFrom.Rows.Count).End(xlUp)).Copy
WsTo.Range("A2").PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=True, _
Transpose:=False
Set WsFrom = Sheets("Hulp_Modbus_Pakscan_Lees_Tags")
Set WsTo = Sheets("Modbus_Lees_Tags_PackScan")
Application.CutCopyMode = False
WsFrom.Range("A1", WsFrom.Range("A" & WsFrom.Rows.Count).End(xlUp)).Copy
WsTo.Range("A2").PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=True, _
Transpose:=False
Set WsFrom = Sheets("Hulp_Modbus_Pakscan_Schrijf_Tag")
Set WsTo = Sheets("Modbus_Schrijf_Tags_PackScan")
Application.CutCopyMode = False
WsFrom.Range("A1", WsFrom.Range("A" & WsFrom.Rows.Count).End(xlUp)).Copy
WsTo.Range("A2").PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=True, _
Transpose:=False
Application.CutCopyMode = False
Sheets("Start").Activate
Set WsFrom = Nothing
Set WsTo = Nothing
End Sub

Related

Loop through list and append results VBA

I have a main sheet, its a dashboard style sheet pulling in information from linked sheets(its used to spot check). All the results for the sheet is driven by one cell(an ID), I have a list of IDs that I want to flow through the cell and then copy the one line of results and append it to some other sheet. I recorded the function to try to explain what im doing.
Sheets("Data Input").Select
Range("L3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=List!R[1]C[-11]"
Rows("32:32").Select
Selection.Copy
Sheets("results").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I then did it multiple times to show how the whole process would look:
Sheets("Data Input").Select
Range("L3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=List!R[2]C[-11]"
Rows("32:32").Select
Selection.Copy
Sheets("results").Select
Range("A4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data Input").Select
Range("L3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=List!R[3]C[-11]"
Rows("32:32").Select
Selection.Copy
Sheets("results").Select
Range("A5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data Input").Select
Range("L3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=List!R[4]C[-11]"
Rows("32:32").Select
Selection.Copy
Sheets("results").Select
Range("A6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
So overall theres three sheets. Data input where the results and functions live, List where contains a list of IDs and Results where I just need to append the one row from Data Input(row32)
You can do something like this:
Dim wsList As Worksheet, wsData As Worksheet, wsResult As Worksheet
Dim c As Range, rwDest As Range
Set wsList = ThisWorkbook.Worksheets("List")
Set wsData = ThisWorkbook.Worksheets("Data Input")
Set wsResult = ThisWorkbook.Worksheets("Result")
Set rwDest = wsResult.Rows(3) 'first destination row
For Each c In wsList.Range("A2:A100").Cells 'for example
If c.Value <> "" Then
wsData.Range("L3").Value = c.Value
wsData.Calculate
rwDest.Value = wsData.Rows(32).Value 'copy row values
Set rwDest = rwDest.Offset(1, 0) 'next destination row
End If
Next c

Combining Variables in a Range

I am having trouble combining RangeToPaste which is Range("M3:R") and x which is Range("A1").Value.
Sub AutoFillProjection()
Sheets("Projection").Select
x = Range("A1").Value
y = Range("A2").Value
Dim RangeToCopy As Range
Dim RangeToPaste As Range
Set RangeToCopy = Range("M2:R2")
Set RangeToPaste = Range("M3:R")
RangeToCopy.Select
Selection.Copy
RangeToPaste & x.Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
RangeToPaste & x.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False

Cells not being copied to the next available cell

I am trying to record a macro that copies values from 4 cells then pastes them on another sheet that serves as a sort of log. I cannot get the values to paste in a new row though despite using the "Relative References" button when recording the macro. Is there something I can add to the code below to make the values paste in the next available row?
'''
Sub Again()
'
' Again Macro
'
'
Range("B5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
ActiveCell.Offset(0, -3).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Test").Select
Range("C5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Test").Select
Range("D5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Test").Select
Range("E5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
'''
Sub add_value()
Dim wbA As Workbook
Dim wsA As Worksheet
Set wbA = ActiveWorkbook
Set wsA = wbA.Worksheets("Sheet1")
Dim nrow As Long
nrow = 6
Do Until wsA.Range("B" & nrow).Value = ""
wsA.Range("B" & nrow).Value = wsA.Range("B3").Value
wsA.Range("C" & nrow).Value = wsA.Range("C3").Value
Exit Sub
nrow = nrow + 1
Loop
End Sub
This is actually working, now i just have to figure out how to offset it

Why can I not reference a sheet name all of a sudden after over a year of running this Macro?

'-2147319767 (80028029)':
Been using this code for over a year now. Suddenly today, it gets the above run-time error when calling out certain sheet names or calling out Activesheet.
Absolutely no idea why it decided not to function today.
'''
Sheets("WIP Shortage").Select
Range("A:CB").Select
Selection.Delete Shift:=xlUp
Range("CC1").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("CC1").Select
Selection.NumberFormat = "yyyy m-d;#"
ChDir "S:\Skim Kits\WIP Shortage Report"
Workbooks.Open Filename:= _
"S:\Skim Kits\WIP Shortage Report\" & Range("CC1").Text & " GEUD_WIP_Job_Shortage_Shop_Fl_ELO.xlsx"
Range("CC1").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("CC1").Select
Selection.NumberFormat = "yyyy m-d;#"
Range("CC2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("CC2").Select
Selection.NumberFormat = "yyyy-m-d;#"
Cells.Select
Selection.Copy
Windows("Availability-Shortages" & Range("CC2").Text).Activate
Cells.Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("CD2").Select
ActiveCell.FormulaR1C1 = "=ISOWEEKNUM(RC[-76])"
Selection.AutoFill Destination:=Range(Selection, Selection.End(xlDown))
Windows(Range("CC1").Text & " GEUD_WIP_Job_Shortage_Shop_Fl_ELO.xlsx").Activate
ActiveWindow.Close
' Paste Thiswk Lastwk formula as values on QMI Targets
Application.Calculation = xlManual
Sheets("WIP Shortage").Select
Range("CE2").Select
ActiveCell.Formula = "=CD2+1"
Application.Calculation = xlAutomatic
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.Calculation = xlManual
Sheets("QMI TARGETS").Select
Range("AL2").Select
ActiveCell.Formula = "=SUMIFS('WIP Shortage'!L:L,'WIP Shortage'!K:K,A2,'WIP Shortage'!E:E,""OP"",'WIP Shortage'!CD:CD,ISOWEEKNUM(NOW()))"
Selection.Copy
Range("AL2:AL300").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.Calculation = xlAutomatic
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.Calculation = xlManual
Range("AM2").Select
ActiveCell.Formula = "=SUMIFS('WIP Shortage'!L:L,'WIP Shortage'!K:K,A2,'WIP Shortage'!E:E,""OP"",'WIP Shortage'!CE:CE,ISOWEEKNUM(NOW()))"
Selection.Copy
Range("AM2:AM300").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.Calculation = xlAutomatic
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'''
This is an excerpt from the entire code, but that first line is the first of 4 places it faults out. If I debug and manual select the sheet and move down the to the next line and run, it goes fine until I try to call out active sheet on line 23.
This is the code that precedes it and it runs fine. You'll notice it calls out my "today" sheet just fine and even renames it.
'''
Sheets("Today").Select
If Range("C5") = "Fri" Then
Sheets("Fri").Select
If Range("C5") = "Fri" Then
Sheets("Fri").Select
ActiveWindow.SelectedSheets.Delete
End If
End If
'''

Autofilter Run-time error '91' VBA

I need your help.
I would like to Autofilter a column of a table with the following value: begins with AF.
And then copy and paste some column to another sheet.
I have written a code but I alwasy get an error when the code reach the following line:
.AutoFilter Field:=rng0.Column, Criteria1:=SearchFor
The error is: Object variable or with block is not set.
I have no idea what is wrong with the code. Please help me.
Sub AF_update()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
SearchCol0 = "Prefix+short name"
SearchCol1 = "Site type"
SearchCol2 = "SLA Target"
SearchCol3 = "Mean Rtt (ms)"
SearchCol4 = "Max Rtt (ms)"
SearchCol5 = "Threshold 95%"
SearchCol6 = "Threshold 99%"
SearchFor = "=AF*"
Dim rng0, rng1, rng2, rng3, rng4, rng5, rng6 As Range
Dim lastrow As Long
Set rng0 = ActiveSheet.UsedRange.Find(SearchCol0, , xlValues, xlWhole)
Set rng1 = ActiveSheet.UsedRange.Find(SearchCol1, , xlValues, xlWhole)
Set rng2 = ActiveSheet.UsedRange.Find(SearchCol2, , xlValues, xlWhole)
Set rng3 = ActiveSheet.UsedRange.Find(SearchCol3, , xlValues, xlWhole)
Set rng4 = ActiveSheet.UsedRange.Find(SearchCol4, , xlValues, xlWhole)
Set rng5 = ActiveSheet.UsedRange.Find(SearchCol5, , xlValues, xlWhole)
Set rng6 = ActiveSheet.UsedRange.Find(SearchCol6, , xlValues, xlWhole)
Set Target = ThisWorkbook.Worksheets("AF")
Set Source = ThisWorkbook.Worksheets("RAW DATA")
Target.Select
Range("A2").Select
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
Selection.ClearContents
Source.Select
If ActiveSheet.AutoFilterMode = True Then
Range("a1").AutoFilter
End If
Range("A1").Select
With Selection
.AutoFilter Field:=rng0.Column, Criteria1:=SearchFor
End With
rng0.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng1.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng2.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng3.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng4.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng5.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("F2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng6.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("G2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
lastrow = Cells(Rows.Count, 5).End(xlUp).Row
Range("A2:G" & lastrow).Sort key1:=Range("E2:E" & lastrow), order1:=xlDescending, Header:=xlNo
Source.Select
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
MsgBox "Operation Completed!"
End Sub
I've cleaned up your code; primarily removing the reliance on .Select¹ and .Activate¹ but also taking your groups of variables and creating arrays for each group. This allowed loops that greatly shortened the code while allowing for full functionality.
Sub AF_update()
Dim v As Long, vSearchCols As Variant, vCols As Variant, FilterFor As String
Dim Source As Worksheet, Target As Worksheet
'Application.ScreenUpdating = False
'Application.DisplayAlerts = False
'Application.EnableEvents = False
FilterFor = "AF*"
Set Source = ThisWorkbook.Worksheets("RAW DATA")
With Source
'array of 'SearchCol' values on a zero-based index
vSearchCols = Array("Prefix+short name", "Site type", "SLA Target", "Mean Rtt (ms)", _
"Max Rtt (ms)", "Threshold 95%", "Threshold 99%")
ReDim vCols(0 To UBound(vSearchCols)) 'make them the same size
For v = LBound(vSearchCols) To UBound(vSearchCols)
vCols(v) = .Rows(1).Cells.Find(What:=vSearchCols(v), LookIn:=xlFormulas, LookAt:=xlWhole).Column
Next v
End With
Set Target = Worksheets("AF")
With Target
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
Debug.Print .Cells(.Rows.Count - 1, .Columns.Count).Address(0, 0, external:=True)
.Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).ClearContents
End With
End With
With Source
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
.AutoFilter Field:=vCols(0), Criteria1:=FilterFor
'check to see if there is anything to copy across
With .Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
'there is something to transfer; loop through the ranges
For v = LBound(vCols) To UBound(vCols)
.Columns(vCols(v)).Copy
Target.Cells(2, v + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next v
End If
End With
End With
End With
With Target
With .Cells(1, 1).CurrentRegion
With .Resize(.Rows.Count, 7)
.Cells.Sort Key1:=.Columns(5), Order1:=xlDescending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
End With
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
MsgBox "Operation Completed!"
End Sub
You may wish to step through the code with repeated F8 taps. I've temporarily commented out your Application environment changes.
When dealing with a block or 'island' of data originating from A1, the Range.CurrentRegion property is a fast and effective method of isolating the data when referenced with a With ... End With statement.
I had to guess on which worksheet your macro code started. I chose the RAW DATA worksheet.
¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Resources