Use only visible rows in Macros code after autofilter

Namratak

New Member
Joined
Jun 11, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have coded for a solver problem in macros. But the problem is the range in B column is auto filtered and the other rows are hidden. So as per inputs, only the rows in B column that satisfy the input are shown. It can start at any row. How do I include the code to use only visible cells in the column B for calculation? Pls help.

Dim Counter As Integer
Dim lastrow As String

Counter = 0
For Each Testname In Range("B3:B10000")
If IsEmpty(Testname) = False Then
Counter = Counter + 1

Else
Counter = Counter + 0
End If

Next Testname
SolverOk SetCell:="$T$1", MaxMinVal:=2, ValueOf:=0, ByChange:="$L$3:$L$counter", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverOk SetCell:="$T$1", MaxMinVal:=2, ValueOf:=0, ByChange:="$L$3:$L$counter", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve
End Sub
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
VBA Code:
For Each Testname In Range("B3", Cells(Rows.Count, 2).End(xlUp)).SpecialCells(xlCellTypeVisible)
 

Namratak

New Member
Joined
Jun 11, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi, this works. thankyou somuch! however I had another doubt. I wanted to calculate column L of no. of units needed of each type of row such that minimum cost occurs( if cost is cell T1) and the column G should be equal to or more than the column L that the macros calculated.
However my code just distributes the cost among all the visible rows. where am I going wrong in the coding?

Also, I appreciate your help!
DOUBT2.JPG
 

Namratak

New Member
Joined
Jun 11, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
this is the nearest ive come to:

VBA Code:
Sub Macro4()
'
' Macro4 Macro
'
'
Dim Counter As Integer
Dim lastrow As String
Counter = 0
For Each Testname In Range("B3", Cells(Rows.Count, 2).End(xlUp)).SpecialCells(xlCellTypeVisible)
If IsEmpty(Testname) = False Then
Counter = Counter + 1

Else
Counter = Counter + 0
End If

Next Testname
SolverOk SetCell:="$T$1", MaxMinVal:=2, ValueOf:=0, ByChange:="$L$3:$L$counter", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:=Range("$L$3:$L$counter"), Relation:=1, CellRef:=Range("$G$3:$L$counter")
SolverSolve
End Sub
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
I can't be much help on the Solver, I have never used it and have no idea how it works.
Regards, JLG
 

Watch MrExcel Video

Forum statistics

Threads
1,127,524
Messages
5,625,309
Members
416,092
Latest member
dodovisk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top