using a variable to store a range

rinneii

New Member
Joined
Mar 20, 2019
Messages
18
This is the section of code I'm working on:

Set rep_v = Rows(ActiveCell.Row)
With rep_v
rep = rep_v.Value
lib = rep_v.Offset(rep, 2, 0).Value
ust = rep_v.Offset(lib, 3, 0).Value
If lib + ust >= hurdle * rep Then
county = county & "; "
End If
End With

obviously the bolded part is wrong
I need to have the .Value there, but I can't figure out how to get the rep_v value in it.
 

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
Try this

Code:
    Set rep_v = ActiveCell
    With rep_v
        rep = .Value
        lib = .Offset(0, 2).Value
        ust = .Offset(0, 3).Value
        If lib + ust >= hurdle * rep Then
            county = county & "; "
        End If
    End With

When you use With it is no longer necessary to put the object, you just start with .

Offset(row, column)

If it is not what you need, then you could explain what you need, what data you have and what you expect from the result. Please use sample data to explain.
 
Upvote 0
I got a type mismatch error, the debugging highlighted the lib. I am trying to get a list of counties where the sum of the libertarian and US taxpayer bote was at least as great as a given percentage (user inputed) of the republican vote. This is my full code:

Dim inp1 As String, inp2 As String, inp3 As String
Dim county As String, c_list As String, mbox As String
Dim target As Double, hurdle As Double
Dim endLoop As Long, i As Long, rep As Long
Dim lib As Long, ust As Long


Sub CountyInfo()


county = ""
inp1 = "Enter percentage comparison for republican votes:"
inp2 = "compared to the sum of libertarians and US taxpayers."
inp3 = inp1 & inp2


target = InputBox("Enter a number.", "Data", 1)
hurdle = target / 100






For i = 2 To endLoop
endLoop = ActiveSheet.Range.End - 5
Next i


Set rep_v = ActiveCell
With rep_v
rep = .Value
lib = .Offset(0, 2).Value
ust = .Offset(0, 3).Value
If lib + ust >= hurdle * rep Then
county = county & "; "
End If
End With






If Len(county) > 1 Then
MsgBox ("Countries with lib and ust " & hurdle & "; " & c_list)
Else: MsgBox ("No counties found.")
Exit Sub
End If



c_list = Mid(county, 2, Len(county) - 2)


End Sub


I will include some data from my workbook.
 
Upvote 0
ElectionDateCountyNameCountyCleanPartyNamePartyDescriptionCandidateLastNameCandidateFirstNameCandidateVotes
11/6/2018ALCONAAlconaREPRepublicanSchuetteBill3409
11/6/2018ALCONAAlconaDEMDemocraticWhitmerGretchen1982
11/6/2018ALCONAAlconaLIBLibertarianGelineauBill40
11/6/2018ALCONAAlconaUSTUS TaxpayersSchleigerTodd42
11/6/2018ALCONAAlconaGRNGreenKurlandJennifer41
11/6/2018ALCONAAlconaNLPNatural LawButkovichKeith21
11/6/2018ALGERAlgerREPRepublicanSchuetteBill1997
11/6/2018ALGERAlgerDEMDemocraticWhitmerGretchen1855
11/6/2018ALGERAlgerLIBLibertarianGelineauBill41
11/6/2018ALGERAlgerUSTUS TaxpayersSchleigerTodd28
11/6/2018ALGERAlgerGRNGreenKurlandJennifer31
11/6/2018ALGERAlgerNLPNatural LawButkovichKeith8
11/6/2018ALLEGANAlleganREPRepublicanSchuetteBill28648
11/6/2018ALLEGANAlleganDEMDemocraticWhitmerGretchen19497
11/6/2018ALLEGANAlleganLIBLibertarianGelineauBill839
11/6/2018ALLEGANAlleganUSTUS TaxpayersSchleigerTodd366
11/6/2018ALLEGANAlleganGRNGreenKurlandJennifer367
11/6/2018ALLEGANAlleganNLPNatural LawButkovichKeith136
11/6/2018ALPENAAlpenaREPRepublicanSchuetteBill7031
11/6/2018ALPENAAlpenaDEMDemocraticWhitmerGretchen5595
11/6/2018ALPENAAlpenaLIBLibertarianGelineauBill161

<colgroup><col><col span="2"><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
Forget the code a bit, explain with your words what you need to do and what you expect from it. Use the base you put to exemplify your explanation.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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
Back
Top