How to run a loop through filtered list in excel - excel

i am trying to run this loop/code through a filtered list in excel where the row numbers are not in sequence eg the first row that meets the filtered criteria could be row 3, followed by row 7 then row 34 for instance.
this is my current code which runs for every row in the stated range but it will not work through for filtered list. How would you modify the code such that this loop can run through a filtered list?
Sub specialloop()
Dim i As Integer
Dim j As Integer
Dim input_var As String
lastrow = Cells(Rows.Count, 1).End(xlUp).row
rowinput = InputBox("input row number to start from")
j = rowinput
For i = j To lastrow
Cells(i, 26).Select
input_var = InputBox("degree verify")
ActiveCell.Value = UCase(input_var)
ActiveCell.Offset(0, 8).Select
input_var = InputBox("med invoice date")
ActiveCell.Value = UCase(input_var)
ActiveCell.Offset(0, 1).Select
input_var = InputBox("med clear")
ActiveCell.Value = UCase(input_var)
ActiveCell.Offset(1, -9).Select
Next i
End Sub

The quick and dirty way would just be to add a condition in your loop for the RowHeight. If that is 0 then the row is filtered.
For i = j To lastRow
If ActiveSheet.Rows(i).RowHeight > 0 Then
Cells(i, 26).Select
input_var = InputBox("degree verify")
ActiveCell.Value = UCase(input_var)
ActiveCell.Offset(0, 8).Select
input_var = InputBox("med invoice date")
ActiveCell.Value = UCase(input_var)
ActiveCell.Offset(0, 1).Select
input_var = InputBox("med clear")
ActiveCell.Value = UCase(input_var)
ActiveCell.Offset(1, -9).Select
End If
Next i
You might want to look at how-to-avoid-using-select-in-excel-vba

How to iterate over visible data
For this purpose you can use Range.SpecialCells method:
Sub SpecialLoop()
' Loop over visible cells in Data range
' and fill in some data (see Shift... constants below)
Dim Data as Range
Dim Cell as Range
Dim FirstRow as Long, LastRow as Long
Dim input_var As String
Const ShiftDegree = 25
Const ShiftInvoice = ShiftDegree + 8
Const ShiftMedClear = ShiftInvoice + 1
With ActiveSheet
FirstRow = InputBox("input row number to start from")
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set Data = .Range(.Cells(FirstRow, 1), .Cells(LastRow, 1))
End With
For Each Cell in Data.SpecialCells(xlCellTypeVisible)
input_var = InputBox("degree verify")
Cell.Offset(0, ShiftDegree).Value = UCase(input_var)
input_var = InputBox("med invoice date")
Cell.Offset(0, ShiftInvoice).Value = UCase(input_var)
input_var = InputBox("med clear")
Cell.Offset(0, ShiftMedClear).Value = UCase(input_var)
Next Cell
End Sub
Notes
Row numbers should be Long, not Integer
Here I assume that meaningful filtered data are collected in the first column
Try if not Cells(i, 26).EntireRow.Hidden then ... as an alternative

Related

Loop the code till the cell is empty in excel

I stuck with this problem:
I have this code and it works but I struggle now.
I want the loop this whole code till in Table1 the cell D1 is empty.
Sub strule()
Dim myCellRange As Range
Worksheets("Table1").Select
Code = Range("D1")
Wert = Range("E10")
Worksheets("Table2").Select
Worksheets("Table2").Range("A1").Select
lMaxRows = Cells(Rows.Count, "A").End(xlUp).Row
Range("A" & lMaxRows).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = Code
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Wert
Sheets("Table1").Select '
Rows("1:10").Select
Selection.Cut
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
End Sub
I've had a guess at what you want... could be completely wrong though.
First of all your original code with all the selecting & activating removed:
Sub strule()
Dim WrkSht1 As Worksheet
Set WrkSht1 = Worksheets("Table1")
'Worksheets("Table1").Select
Dim Code As String
Code = WrkSht1.Range("D1")
Dim Wert As String
Wert = WrkSht1.Range("E10")
Dim WrkSht2 As Worksheet
Set WrkSht2 = Worksheets("Table2")
'Worksheets("Table2").Select
'Worksheets("Table2").Range("A1").Select
Dim lMaxRows As Long
lMaxRows = WrkSht2.Cells(Rows.Count, "A").End(xlUp).Row
WrkSht2.Cells(lMaxRows + 1, 1) = Code 'Lastrow+1 in column A.
WrkSht2.Cells(lMaxRows + 1, 2) = Wert 'Lastrow+1 in column B.
'Range("A" & lMaxRows).Select
'ActiveCell.Offset(1, 0).Select
'ActiveCell.Value = Code
'ActiveCell.Offset(0, 1).Select
'ActiveCell.Value = Wert
WrkSht1.Rows("1:10").Delete shift:=xlUp
'Sheets("Table1").Select '
'Rows("1:10").Select
'Selection.Cut
'Application.CutCopyMode = False
'Selection.Delete Shift:=xlUp
End Sub
Now what I think you want:
Sub strule1()
Dim WrkSht1 As Worksheet
Set WrkSht1 = Worksheets("Table1")
Dim WrkSht2 As Worksheet
Set WrkSht2 = Worksheets("Table2")
Dim lLastRow1 As Long
lLastRow1 = WrkSht1.Cells(Rows.Count, "A").End(xlUp).Row
Dim x As Long
Dim lLastRow2 As Long
Dim Code As String
Dim Wert As String
For x = 1 To lLastRow1 Step 10
Code = WrkSht1.Cells(x, 4) 'Loop 1 grabs from row 1, loop 2 from row 11
Wert = WrkSht1.Cells(x + 9, 5) 'Loop 1 grabs from row 10, loop 2 from row 20
lLastRow2 = WrkSht2.Cells(Rows.Count, "A").End(xlUp).Row
WrkSht2.Cells(lLastRow2 + 1, 1) = Code 'Lastrow+1 in column A.
WrkSht2.Cells(lLastRow2 + 1, 2) = Wert 'Lastrow+1 in column B.
Next x
WrkSht1.Rows("1:" & x).Delete shift:=xlUp
End Sub

Based on color and value fetching-Compiles but no output

I am working on a dynamic worksheet which the total rows and columns of content will be changing.
What I try to do is, making an active cell going through the worksheet. It starts from the very last column that has content (I used UsedRange here), and from the 7st row down to the last row not blank.
When 1) The active cell has a color filling of index 16 or 36; 2) The active cell has no value, the cell will fetch the value storing in the matching row E.
The loop will end when hitting column E (I haven't been able to go that far yet).
I will attach my code below for all possible help, since it complies but does not return any result...Thank you again!
Sub catchCurrentAutomated()
Dim column As Integer
Dim row As Integer
Dim Cell As Range
row = 7
column = ActiveSheet.UsedRange.Columns.Count
Set Cell = ActiveCell
While range("A" & row) <> ""
If Cell.Interior.ColorIndex = 16 And _
IsEmpty(Cell.Value) = True Then
Cell.Value = Cells(ActiveCell.row, "E").Value
ElseIf Cell.Interior.ColorIndex = 36 And _
IsEmpty(Cell.Value) = True Then
Cell.Value = Cells(ActiveCell.row, "E").Value
End If
row = row + 1
column = column - 1
Wend
End Sub
Something like this should work (untested)
Sub catchCurrentAutomated()
Dim col As Long '<< use Long not Integer
Dim row As Long
Dim c As Range, ws As Worksheet, lr As Long, indx
Set ws = ActiveSheet
col = ws.UsedRange.Columns.Count
lr = ws.Cells(Rows.Count, 1).End(xlUp).row 'last occupied cell in ColA
Do While col > 5
For row = 7 To lr
With ws.Cells(row, col)
indx = .Interior.Color.Index
If (indx = 16 Or indx = 36) And Len(.Value) = 0 Then
.Value = ws.Cells(row, "E").Value
End If
End With
Next row
col = col - 1 'next column to left
Loop
End Sub

Search column headers and insert new column if header does not already exist using Excel VBA

I have a spreadsheet that is updated regularly. The user will update two columns on sheet(create) with container type (this is the header name) and the quantity, which will be transferred to sheet(Tracking). I am trying to figure out how to search sheet2(Tracking for existing headers (container types), if found then quantity will be updated within that column for the next available row. If header is not found, therefore a new column is added to the right with that new header name, as well as updating the quantity.
I did find some good example such as the below. However not sure how to apply it. Maybe there could be a way to loop it to search the headers.
Sub TrackR()
Dim cl As Range
For Each cl In Range("1:1")
If cl = sheets(“Create”).range(“J11:J36”) Then
cl.EntireColumn.Insert Shift:=xlToRight
End If
cl.Offset(0, 1) = "New Conatainer Name"
Next cl
Application.ScreenUpdating = False
Sheets("Tracking").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Date
'Trailer No.
Sheets("Create").Range("L8").Copy
Sheets("Tracking").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'total container qty
Sheets("Create").Range("G43").Copy
Sheets("Tracking").Range("D" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Supplier
Sheets("Create").Range("K4").Copy
Sheets("Tracking").Range("C" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'quantities
Sheets("Create").Range("L11").Copy
Sheets("Tracking").Range("E" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Sheets("Create").Range("L12").Copy
Sheets("Tracking").Range("F" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Sheets("Create").Range("L13").Copy
Sheets("Tracking").Range("G" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Sheets("Create").Range("L14").Copy
Sheets("Tracking").Range("H" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Sheets("Create").Range("L15").Copy
Sheets("Tracking").Range("I" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = False
End Sub
Not sure, try this ... ~
Sub TrackB()
Dim wsCreat As Worksheet: Set wsCreat = Sheets("Create")
Dim wsTracking As Worksheet: Set wsTracking = Sheets("Tracking")
Dim cl As Range, lastHCell As Range, header As Range, i As Integer, j As Integer,k as integer, str As Variant
With wsTracking
Set header = .[a1:xx1]: Set lastHCell = header.End(xlToRight)
iLstRow = .[a10000].End(xlUp).Offset(1, 0).Row
'Update default data [A:D]
.Range("A" & iLstRow) = Date
For Each str In Array("L8", "C4", "G43")
.Cells(iLstRow, i + 2) = wsCreat.Range(str): i = i + 1
Next
'add Column if not Match
For Each cl In wsCreat.[B11:B37, E11:E37]
Dim k: k = Application.Match(cl, header, 0)
If IsError(k) And cl <> vbNullString Then _
lastHCell.Offset(0, 1).EntireColumn.Insert Shift:=xlToRight, CopyOrigin:=True: _
Set lastHCell = lastHCell.Offset(0, 1): lastHCell.Value2 = cl
Next cl
'Update input Data
i = 5
Dim arr As Variant: arr = Array("B11:B37", "E11:E37")
Dim arrResult As Variant: arrResult = Array("C10" , "F10")
Dim cell As Range: k = 0
For k = 0 To UBound(arr)
j=1
For Each cell In wsCreat.Range(arr(k)).Cells
If cell.Value2 <> vbNullString Then
.Cells(iLstRow, Application.Match(cell, header, 0)) = wsCreat.Range(arrResult(k)).Offset(j, 0)
End If
j = j + 1
Next cell
Next
End With
End Sub
Untested but something like this should work:
Sub TrackR()
Dim wsTrack As Worksheet, wsCreate As Worksheet, cont, qty, h As Range
Dim c As Range, m, rw As Range, rngHeaders As Range, col As Long
Set wsCreate = ThisWorkbook.Worksheets("Create")
Set wsTrack = ThisWorkbook.Worksheets("Track")
'get the next empty row on the Tracking sheet
Set rw = wsTrack.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
'fill in the common cells in the row
rw.Cells(1).Value = Date
rw.Cells(2).Value = wsCreate.Range("L8").Value
rw.Cells(3).Value = wsCreate.Range("K4").Value
rw.Cells(4).Value = wsCreate.Range("G43").Value
'now loop over the containers and add each one
Set rngHeaders = wsTrack.Cells(1, "E").Resize(1, 5000) 'or whatever would cover your data
For Each c In wsCreate.Range("J11:J36").Cells
cont = c.Value
qty = c.Offset(0, 2).Value
If Len(cont) > 0 And Len(qty) > 0 Then
m = Application.Match(cont, rngHeaders, 0) 'any existing match ?
If IsError(m) Then
'no match - find the first empty cell and add the container
Set h = rngHeaders.Cells(rngHeaders.Cells.Count).End(xlToLeft).Offset(0, 1)
h.Value = cont
col = h.Column 'column number for the added header
Else
'matched: get the column number
col = rngHeaders.Cells(m).Column
End If
rw.Cells(col).Value = qty '<< add the quantity
End If
Next c
End Sub

How to copy columns from one worksheet to another on excel with VBA?

I am trying to copy certain column from one worksheet to another but when I apply my code, I get no errors but also no results. I get blank paper. I applied this methodolgy on copying a certain row and it was copied to another worksheet perfectly.
This is regarding the successful attempt to copy row.
The code works just fine:
Sub skdks()
Dim OSheet As Variant
Dim NSheet As Variant
Dim i As Integer
Dim LRow As Integer
Dim NSLRow As Integer
OSheet = "Tabelle3" 'Old Sheet Name
NSheet = "Tabelle5" 'New Sheet Name
LRow = Sheets(OSheet).Cells(Rows.Count, 1).End(xlUp).row 'Last Row in Old Sheet
Sheets(OSheet).Activate
For i = 2 To LRow
'Finds last row in the New Sheet
If Sheets(NSheet).Cells(2, 1) = "" Then
NSLRow = 1
Else
NSLRow = Sheets(NSheet).Cells(Rows.Count, 1).End(xlUp).row
End If
'If cell has "certain # then..."
If Cells(i, 1).Value = Cells(13, 2).Value Then
Cells(i, 1).EntireRow.Copy
Sheets(NSheet).Cells(NSLRow + 1, 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If
Next i
End Sub
This little piece of code is the failed attempt to copy column to another worksheet.
Sub trial()
Dim OSheet As Variant
Dim NSheet As Variant
Dim j As Integer
Dim LColumn As Integer
Dim NSLColumn As Integer
OSheet = "Tabelle2" 'Old Sheet Name
NSheet = "Tabelle5" 'New Sheet Name
LColumn = Sheets(OSheet).Cells(1, Columns.Count).End(xlToLeft).Column 'Last Column in Old Sheet
Sheets(OSheet).Activate
For j = 2 To LColumn
'Finds last column in the New Sheet
If Sheets(NSheet).Cells(1, 2) = "" Then
NSLColumn = 1
Else
NSLColumn = Sheets(NSheet).Cells(1, Columns.Count).End(xlToLeft).Column
End If
'If cell has "certain # then..."
If Cells(2, j) = Cells(13, 2) Then
Cells(2, j).EntireColumn.Copy
Sheets(NSheet).Cells(2, 2).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If
Next j
End Sub
....
'If cell has "certain # then..."
If Cells(2, j) = Cells(13, 2) Then
debug.Print Cells(2, j).Address; " = "; Cells(13, 2).Address; " ---- COPY"
debug.print Cells(2, j).EntireColumn.address; Cells(2, j).EntireColumn.cells.count
debug.Print Sheets(NSheet).Cells(2, 2).Address
Cells(2, j).EntireColumn.Copy
Sheets(NSheet).Cells(2, 2).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If
....
With the line If Cells(2, j) = Cells(13, 2) Then you compare the different cells from row 2 (B2, C2, D2, ...) with the value of cell "B13". If the value is the same you copy this column to the new worksheet.
Is there any equal value in your data? If yes you should get an error message with your code.
You try to copy the values of an entire column to the range starting with "B2". Of cause there is not enough space for this.
=> Either you reduce the source range or you start the destination range on row 1!
To add to the paste destination size, if you really want to paste the entire column, you either need to start at the beginning of the column or choose the entire column. Also, I think you want to make the paste column increase with your NSLColumn
If Cells(2, j) = Cells(13, 2) Then
Cells(2, j).EntireColumn.Copy
Sheets(NSheet).Columns(NSLColumn + 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End If

VBA Data in Sheet 1 matching with any cell in column A sheet 2

I have two sheets Report and Stat. I need to match cell from Report with Stat and Stat with report.
I don't know what I'm missing :(
I try to loop with For, If, IF Not
Sub Test1()
Dim x As Integer
Application.ScreenUpdating = False
Rows = Range("A1", Range("A1").End(xlDown)).Rows.Count
Range("A2").Select
For x = 1 To Rows
If ActiveCell.Value = Stat.Range("A").Value Then ActiveCell.Offset(0, 11).Value = "Old"
If Not ActiveCell.Value = Stat.Range("A").Value Then ActiveCell.Offset(0, 11).Value = "New"
' Selects cell down 1 row from active cell.
ActiveCell.Offset(1, 0).Select
Next
Sheets("Stat").Select
Rows2 = Range("A1", Range("A1").End(xlDown)).Rows.Count
Range("A2").Select
For x = 1 To Rows2
If Not ActiveCell.Value = Report.Range("A").Value Then ActiveCell.Offset(0, 11).Value = "Cleared"
' Selects cell down 1 row from active cell.
ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True
End Sub
I need to match all cells in column A and try to match with any cell in column A in Stat Sheet.
If it match then offset 11 cell Report sheet to the right and add value "Old" to the cell.
If it doesn't match then Off offset 11 cell in Report sheet to the right and add value "New".
The last thing I need to match all cells in column A from Stat Sheet and try to match with any cell in column A in Report Sheet.
If it match then nothing
If it doesn't match then sheet Stat offset 11 to the right and add value "Cleared"
I'm still looking working on this but can't figure it out :/
This could help you:
Option Explicit
Sub Loop_Loop()
Dim LastrowReport As Long, LastrowStat As Long, i As Long, y As Long
Dim ValueReport As String, ValueStat As String
'Find Report sheet last row (Column A)
LastrowReport = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
'Find Stat sheet last row (Column A)
LastrowStat = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
'Loop value in sheet Report, column A (starting from second row)
For i = 2 To LastrowReport
'Value in sheet Report, Column A i row
ValueReport = Sheet1.Range("A" & i).Value
'Loop value in sheet Stat, column A (starting from second row)
For y = 2 To LastrowStat
'Value in sheet Stat, Column A y row
ValueStat = Sheet2.Range("A" & y).Value
'Check if ValueReport & ValueStat is equal
If ValueReport = ValueStat Then
MsgBox "Same Values!"
End If
Next y
Next i
End Sub
ERROR 4001
I follow your advise but this time I can't have "Old" "New" and "Clear" the word overlap in the same cell :(
Option Explicit
Sub Loop_Loop()
Dim LastrowReport As Long, LastrowStat As Long, i As Long, y As Long
Dim ValueReport As String, ValueStat As String
LastrowReport = Sheet10.Cells(Sheet10.Rows.Count, "A").End(xlUp).Row
LastrowStat = Sheet12.Cells(Sheet12.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastrowReport
ValueReport = Sheet10.Range("A" & i).Value
For y = 2 To LastrowStat
ValueStat = Sheet12.Range("A" & y).Value
If ValueReport = ValueStat Then
Activecell.offset(0, 11).Value = "Old"
If Not ValueReport = ValueStat Then
Activecell.offset(0, 11).Value = "New"
If Not ValueStat = ValueReport Then
Activecell.offset(0, 11).Value = "Clear"
End If
Next y
Next i
End Sub

Resources