Copy Ranges not containing exceptions - excel

I have a macro that is supposed to identify cells containing data in a column, and then copy multiple columns from said cells row into another worksheet.
19 rows that fit the Criteria to be copied (and don't contain any words that come up in my 5 exceptions) aren't being copied.
I tried to go through the macro step by step, working with stopping points and changing around the macro itself.
My theory is there is something wrong with the Cells in the sheet it is supposed to copy from.
Sub Copy_Range()
Dim zelle, cell As Range
Dim i As Long
On Error Resume Next
Worksheets("Worksheet 4").Activate
Application.GoTo Worksheets("Worksheet 4").Range("C2:H1000")
Application.ScreenUpdating = False
Worksheets("Worksheet 4").Activate
Range("C2:C1000,D2:D1000,E2:E1000,F2:F1000,G2:G1000,H2:H1000").Clear
Worksheets("Worksheet 1").Activate
Range("A6").Activate
'This part Shows an alert when theres no Data entered in column A
If WorksheetFunction.CountA(Range("A6:A1000")) = 0 Then
Dim click As Integer
click = MsgBox(prompt:="There was no data Entered in Column A", Buttons:=vbExclamation)
Cells(1, 1).Select
Exit Sub
End If
Set Tbl2 = ThisWorkbook.Worksheets("Worksheet 1").ListObjects("Tabelle33")
LastRow4 = Tbl2.ListColumns(1).Range.Rows.Count
Set cell = Cells(ActiveCell.Row, ActiveCell.column)
'This part is supposed to look through Column A in Worksheet 1
'If there is data entered in column A of a row the Macro copies the data entered in column 1, 2, 3, 5, 6 and 7 of that row into Worksheet 4,
'UNLESS the Data entered in Column A is one of 5 exceptions.
For Each zelle In Worksheets("Worksheet 1").Range(Cells(Rows.Count, cell.column), Cells(cell.Row, cell.column))
If ActiveCell.Value = "" Then
Selection.End(xlDown).Select
**ElseIf ActiveCell.Value = "Exception 1" Then**
**Selection.End(xlDown).Select**
**ElseIf ActiveCell.Value = "Exception 2" Then**
**Selection.End(xlDown).Select**
**ElseIf ActiveCell.Value = "Exception 3" Then**
**Selection.End(xlDown).Select**
**ElseIf ActiveCell.Value = "Exception 4" Then**
**Selection.End(xlDown).Select**
**ElseIf ActiveCell.Value = "Exception 5" Then**
**Selection.End(xlDown).Select**
Else
Union(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 2), Cells(ActiveCell.Row, 3), Cells(ActiveCell.Row, 5), Cells(ActiveCell.Row, 6), Cells(ActiveCell.Row, 7)).Copy
Application.GoTo Worksheets("Worksheet 4").Cells(2, 3)
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Selection.PasteSpecial Paste:=xlPasteAll
Range("C2:H2").Select
Selection.Insert Shift:=xlDown
Worksheets("Worksheet 1").Activate
ActiveCell.Offset(1, 0).Select
End If
Next
Worksheets("Worksheet 4").Activate
Range("C2:H1000").Interior.Color = xlNone
End Sub
Edit: the problem seems to be the ** Starred ** lines in my code aka. my "Exceptions"
I have since removed that snippet and am working on a new bit of code that filters through column A and then deletes the Exceptions after the fact, instead of not copying them from the start.

You are missing Dim zelle AS RANGE
While defining variables on the same line each varaible must be defined separately so
Dim zelle as range, cell as range
Also try
replacing If ActiveCell.Value by If zelle.value

Try this code:
Dim LR as long
Dim cell as range
LR = Thisworkbook.worksheets("name of your worksheet").range("A" & rows.count).end(xlup).row
for cell in range("A1","A"& LR)
if cell.value =
'add here all your exceptions scenarios
else
'copy the data code
end if
next cell

Related

VBA - How to copy and data from a worksheet in a certain condition to the last worksheet

I'm new with VBA and I am trying to create a macro for work to make everyone's life easier. My goal is to copy rows (or just copy the data in the first column when the second column is "0") from one worksheet named "Bulk Update" with the condition of column B having the value "0" to the last worksheet, at the bottom of the worksheet after the data. I don't know how to reference the last worksheet name. Here is the code that I made (please don't judge me as I am still new and googling around), which I know is completely wrong...
Public Sub CNPPrevOOS()
Worksheets("Bulk Update").Select
a = Worksheets("Bulk Update").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Bulk Update").Cells(i, 2).Value = "0" Then
Selection.Copy
ThisWorkbook.Worksheets(ThisWorkbook.Sheets.Count).Range("A1").Value = 100
Range("A30000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
End If
Next
End Sub
You could try the below code.
The data is being filtered for Column 2 = 0. Only those rows are copied and pasted in the last worksheet
Public Sub CNPPrevOOS()
Worksheets("Bulk Update").Select
a = Worksheets("Bulk Update").Cells(Rows.Count, 1).End(xlUp).Row
'Filters the data where column 2 = 0
ActiveSheet.Range(Cells(1, 1), Cells(a, 2)).AutoFilter Field:=2, Criteria1:="0", Operator:=xlFilterValues
'Select only the filtered cells and copy
Range(Cells(2, 1), Cells(a, 1)).SpecialCells(xlCellTypeVisible).Select
Selection.Copy
ThisWorkbook.Worksheets(ThisWorkbook.Sheets.Count).Select
ActiveSheet.Paste Destination:=Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)
End Sub

How to Fix Run-time Error 424 "Object Required" in Excel VBA

I'm working on an Excel project where I am trying to produce certain rows from "Sheet 1" that contains a word called "external" in column C and then copy and paste that row into "Sheet 3"
I understand that there is a thing called "filter" but that is not an option.
This project is for my team at work that wants to be able to extract rows and columns that are shown as "external" and then be able to paste them and other information to another sheet that contains that information.
Private Sub CommandButton1_Click()
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Sheet1").Cells(i, 3).Value = "External" Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet3").Activate
b = Worksheets("Sheet3").Cells(Row.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select
End Sub
The expected result was to display all rows that contained the word "External" in Sheet 1 Column C into a new sheet and have all its information displayed in Sheet 3.
Excel Worksheet for Reference:
First, declare all your variables. Next, you can try changing If Worksheets("Sheet1").Cells(i, 3).Value = "External" Then to If Worksheets("Sheet1").Range("C" & i).Text = "External" Then. See here:
Private Sub CommandButton1_Click()
Dim a As Long
Dim i As Long
Dim b As Long
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Sheet1").Range("C" & i).Text = "external" Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet3").Activate
b = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select
End Sub

How to set a dynamic end cell while using an ActiveCell.Offset for the Start Cell?

I'm using a table to pull information from separate work books.currently the table has a set end cell, but I'm pulling into too much information. I want to set the end cell to the last row of the data in column D. I need help modifying the code to set the end cell to a dynamic range.
I've already tried to use lastRow = .Cells(.Rows.Count, col).End(xlUp).Row but I keep getting
compile error
at the preceding .Offset that is invalid or unqualified reference
Sub GetData()
Dim strWhereToCopy As String, strStartCellColName As String
Dim strListSheet As String
Dim strCopySheet As String
strListSheet = "List"
On Error GoTo ErrH
Sheets(strListSheet).Select
Range("B2").Select
'this is the main loop, we will open the files one by one and copy their data into the masterdata sheet
Set currentWB = ActiveWorkbook
Do While ActiveCell.Value <> ""
strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value
strCopyRange = ActiveCell.Offset(0, 2) & ":" & lastRow =
.Cells(.Rows.Count, col).End(xlUp).Row
strWhereToCopy = ActiveCell.Offset(0, 4).Value
strCopySheet = ActiveCell.Offset(0, 6).Value
strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1)
Application.Workbooks.Open strFileName, UpdateLinks:=False,
ReadOnly:=True
Set dataWB = ActiveWorkbook
Sheets(strCopySheet).Select
Range(strCopyRange).Select
Selection.Copy
currentWB.Activate
Sheets(strWhereToCopy).Select
lastRow = LastRowInOneColumn(strStartCellColName)
Cells(lastRow + 1, 1).Select
Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone
Application.CutCopyMode = False
dataWB.Close False
Sheets(strListSheet).Select
ActiveCell.Offset(1, 0).Select
Loop
Exit Sub
ErrH:
MsgBox "It seems some file was missing. The data copy operation is not
complete."
Exit Sub
End Sub

Go to next visible cell using offset

I have an autofiltered table in excel. I have to copy paste values based on certain conditions and I have to perform this on all visible cells in a particular column. I have written the code and it works well but the only thing is that it takes a lot of time as there are many rows. Can anyone please help me how to thrash time required? Here's the code. Thanks!
Sub TrialAnotherOne()
Windows("Epson Itemcodes.xlsm").Activate
Range("A" & i).Select
Selection.Copy
Windows("Epson ASINs.xlsx").Activate
Range("U1048576").End(xlUp).Offset(0, -12).Select
If ActiveCell.Value <> "Itemcode" Then
If ActiveCell.Value = "" Then
ActiveSheet.Paste
Else
If ActiveCell.Value = Workbooks("Epson Itemcodes.xlsm").Sheets("Sheet1").Range("A" & i).Value Then
ActiveSheet.Paste
Else
ActiveCell.Value = "Conflct"
End If
End If
Else
Windows("Epson Itemcodes.xlsm").Activate
Range("I" & i).Value = "No match found"
End If
If ActiveCell.Value <> "Itemcode" Then
With ActiveSheet
Do
ActiveCell.Offset(-1, 0).Activate
Do While ActiveCell.EntireRow.Hidden = True
ActiveCell.Offset(-1, 0).Activate
Loop
If ActiveCell.Value <> "Itemcode" Then
If ActiveCell.Value = "" Then
ActiveSheet.Paste
Else
If ActiveCell.Value = Workbooks("Epson Itemcodes.xlsm").Sheets("Sheet1").Range("A" & i).Value Then
ActiveSheet.Paste
Else
ActiveCell.Value = "Conflct"
End If
End If
Else
Exit Do
End If
Loop
End With
End If
End Sub
Range Copy, Cut, and Delete automatically selects only the visible cells of a filtered range.
Sub CopyFilteredColumn()
Dim Target As Range
'Size the Target range to fit the table
'Define the starting row "C1:J19"
'Extend the Target range to the last row .Range("C" & .Rows.Count).End(xlUp)
'Column C is used because it will never have blank cells
With Worksheets("Source Sheet")
Set Target = .Range("C1:J19", .Range("C" & .Rows.Count).End(xlUp))
End With
Target.AutoFilter Field:=1, Criteria1:=">40", Operator:=xlAnd
'Header and data
'Copy the visible cells of the 3rd column of the table
Target.Columns(3).Copy Worksheets("Target Sheet").Range("A1")
'Data only - Includes 1 blank cell at the end
Target.Offset(1).Columns(3).Copy Worksheets("Target Sheet").Range("C1")
End Sub

Excel Macro Find Text value Cut and Paste then shift cells up

I have a nightmare task to migrate from one accounting package to another.
I have 9340 rows in columns A,B and G which needs to be ordered in a certain way before it can be imported by new system.
Before:
After:
I ran a macro that does what I want but only for selected range. How do I make macro work for entire sheet?
Sub Macro1()
Range("B206").Select
Selection.Cut
Range("A207").Select
ActiveSheet.Paste
Rows("206:206").Select
Selection.Delete Shift:=xlUp
Range("A206").Select
Selection.Copy
Range("A206:A216").Select
ActiveSheet.Paste
Range("C216").Select
Application.CutCopyMode = False
Selection.Cut
Range("G216").Select
ActiveSheet.Paste
End Sub
This will likely fail in some respect. Your setup is more complicated than I have time to re-create. Please run this code on a copy of your data. It basically moves things around and then deletes all the rows that have blanks in column B. You should delete the header junk above the first "Opening" row:
Sub test()
Dim ws As Excel.Worksheet
Dim LastRow As Long
Dim cell As Excel.Range
Set ws = ActiveSheet
With ws
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
For Each cell In .Range("B1:B" & LastRow)
If Left(cell.Value, Len("Opening")) = "Opening" Then
cell.Offset(1, -1).Value = cell.Value
cell.ClearContents
Else
cell.Offset(0, -1) = cell.Offset(-1, -1).Value
End If
If Left(cell.Value, Len("Closing")) = "Closing" Then
cell.Offset(0, 6).Value = cell.Offset(0, 1).Value
cell.Offset(0, 1).ClearContents
End If
Next cell
.Range("B" & .Rows.Count).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub

Resources