Go to next visible cell using offset - excel

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

Related

Copy Ranges not containing exceptions

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

VBA: Looping a condition through a range that compares values from other columns until the list ends

Public Sub MainTOfomat()
Dim ShippingQty As Range
Dim ReceivedQty As Range
ActiveSheet.Columns("A:P").AutoFit
ActiveSheet.Range("A:P").AutoFilter Field:=13, Criteria1:="No"
ActiveSheet.Range("K:L").AutoFilter Field:=2, Criteria1:="<>"
Set ShippingQty = Range("K2")
Set ReceivedQty = ShippingQty.Offset(0, 1)
ShippingQty.Select
Do Until IsEmpty(ActiveCell)
If ShippingQty.Value = 0 Then
ShippingQty.Offset(0, 5) = "Needs Fulfillment"
ElseIf ShippingQty.Value > ReceivedQty.Value Then
ShippingQty.Offset(0, 5) = "Needs Receipt"
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
The code is program is supposed to loop though each row in the column and fill in the statement based on the result of the condition for values in two other columns. The problem is that the loop goes through, but only the first line actually changes, and the auto filter code before the loop gets skipped.
Here is your macro fixed up.
As mentioned before your ShippingQty range and ReceivedQty do not change with the activecell. When moving to the next cell, that is the activecell. The filter range need to be the same. A:P is filtered, when changing to K:L ,field 2 actually becomes column B, so if you want to filter out non-blanks in column L you need the field 12.
Sub YourMacro()
Dim ShippingQty As Range
Dim ReceivedQty As Range
ActiveSheet.Columns("A:P").AutoFit
With ActiveSheet.Range("A:P")
.AutoFilter Field:=13, Criteria1:="No"
.AutoFilter Field:=12, Criteria1:="<>"
End With
Set ShippingQty = Range("K2")
Set ReceivedQty = ShippingQty.Offset(0, 1)
ShippingQty.Select
Do Until IsEmpty(ActiveCell)
If ActiveCell.Rows.Hidden = False Then
If ActiveCell.Value = 0 Then
ActiveCell.Offset(0, 5) = "Needs Fulfillment"
ElseIf ActiveCell.Value > ActiveCell.Offset(, 1).Value Then
ActiveCell.Offset(0, 5) = "Needs Receipt"
End If
End If
ActiveCell.Offset(1, 0).Select
Loop
ActiveSheet.AutoFilterMode = 0
End Sub
You can use this option as well without using selects.
Sub Option1()
Dim rng As Range, c As Range
Dim ws As Worksheet
Set ws = ActiveSheet
Application.ScreenUpdating = 0
With ws
Set rng = .Range("K2:K" & .Cells(.Rows.Count, "K").End(xlUp).Row)
With .Range("A:P")
.AutoFilter Field:=13, Criteria1:="No"
.AutoFilter Field:=12, Criteria1:="<>"
End With
For Each c In rng.SpecialCells(xlCellTypeVisible)
If c = 0 Then c.Offset(, 5) = "Needs Fulfillments"
If c > c.Offset(, 1) Then c.Offset(, 5) = "Needs Receipts"
Next c
.AutoFilterMode = False
End With
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

Copying active row of Sheet1 to Sheet2 based on cell condition & avoid duplicates

Copying active row of Sheet1 to Sheet2 based on cell condition (Column F="Yes") and also prevent duplication.
I tried the following
Private Sub CommandButton1_Click()
Dim CustomerName As String, Customeraddress As String, Customercity As String, Custtel As String, Custzip As String
Worksheets("sheet1").Select
CustomerName = Range("A2")
Customeraddress = Range("B2")
Customercity = Range("C2")
Custtel = Range("D2")
Custzip = Range("E2")
Worksheets("sheet2").Select
Worksheets("Sheet2").Range("B4").Select
If Worksheets("Sheet2").Range("B4").Offset(1, 0) <> "" Then
Worksheets("Sheet2").Range("B4").End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = CustomerName
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Customeraddress
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Customercity
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Custtel
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Custzip
Worksheets("Sheet1").Select
Worksheets("Sheet1").Range("C4").Select
End Sub
So now I need to know how to check if sheet1 Column F="Yes" for that particluar Customer. Then only copy from Sheet1 to Sheet2. If customer info is already present in Sheet2 then dont duplicate if the user clicks the button on sheet1 active row.
Try this code:
Private Sub CommandButton1_Click()
Dim lastrow As Long
'if value in column F not equal "YES" - do nothing and exit sub
If UCase(Range("F" & ActiveCell.Row).Value) <> "YES" Then Exit Sub
With ThisWorkbook.Worksheets("Sheet2")
lastrow = Application.Max(4, .Cells(.Rows.Count, "B").End(xlUp).Row + 1)
'if CustomerName is already in column B of sheet2 - do nothing and exit sub
If WorksheetFunction.CountIf(.Range("B1:B" & lastrow), _
Range("A" & ActiveCell.Row).Value) > 0 Then Exit Sub
.Range("B" & lastrow).Resize(, 5).Value = _
Range("A" & ActiveCell.Row).Resize(, 5).Value
End With
End Sub
And, please, read this post: How to avoid using Select/Active statements:)

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