Object required error in Set = - vba

I am trying to build some code that inserts a name and email address in to the next open row from the next row in the loop. I am getting an error in Set = Number of Emails line and I am not sure why.
Sub Email_Copy()
Dim Data As Worksheet
Set Data = ActiveWorkbook.Sheets("Data")
Dim Email_Database As Worksheet
Set Email_Database = ActiveWorkbook.Sheets("Email_Database")
Dim Number_Of_Emails As Integer
Set Number_Of_Emails = Data.Range("L6002")
Dim Total_Emails As Long
Set Total_Emails = Email_Database.Range("D2")
Dim X As Long
For X = 1 To Number_Of_Emails
Set Email_Database.Range("B3").Offset(Total_Emails, 0) = Data.Range("D3").Offset(Number_Of_Emails, 0)
End Sub

You declared Number_Of_Emails As Integer but then tried to set it to a range so it's a type mismatch. You can simply do
Number_Of_Emails = Data.Range("L6002").value
Edit: to plagarize Scott Holtzman's comment verbatim
Set is only required with Object Type variables (hence the error). A
Range variable is an Object Type. An Integer, being a number, is not
an object type. (String is not on object either). Worksheet is an
Object Type.

Related

Using a variable within a formula

I'm trying to use a variable within a formula but the result I am getting is FALSE, when it should be returning a Lookup value, starting from the bottom of a table and going up.
Private Sub NewTaxCode()
Dim TaxCode2 As Long
Dim B As Range
Dim TaxCode3 As Range
Dim TaxCode4 As Range
Worksheets("P11Combined").Activate
Set B = Range("A:A")
Worksheets("E'ee Details").Range("U1").Value = "=Match(RC[-4],P11Combined!C[-18],0)"
TaxCode2 = Worksheets("E'ee Details").Range("U1").Value
Set TaxCode3 = Range("A:A").Find(What:="TAX:", After:=B(TaxCode2))
Set TaxCode4 = Range(TaxCode3.Address).End(xlDown).Offset(1, 2)
TaxCode5 = Range(TaxCode4.Address).FormulaR1C1 = "=LOOKUP(2,1/(R[-12]C:R[-1]C<>"" ""),(R[-12]C[-1]:R[-1]C[-1]))"
The last line is where I am having the issues. The formula works fine on a spreadsheet but I can't get it to work in VBA.
The last line of your code is of the format
variable = logical_expression
and is setting a variable (TaxCode5) to be either True or False depending on whether or not the formula in the cell referred to by TaxCode4 is the same as the one in the code.
It does not modify the formula in the cell referred to by TaxCode4.
It appears you want:
Private Sub NewTaxCode()
Dim TaxCode2 As Long
Dim B As Range
Dim TaxCode3 As Range
Dim TaxCode4 As Range
Dim TaxCode5 As Variant
Set B = Worksheets("P11Combined").Range("A:A")
Worksheets("E'ee Details").Range("U1").FormulaR1C1 = "=Match(RC[-4],P11Combined!C[-18],0)"
TaxCode2 = Worksheets("E'ee Details").Range("U1").Value
Set TaxCode3 = B.Find(What:="TAX:", After:=B(TaxCode2))
Set TaxCode4 = TaxCode3.End(xlDown).Offset(1, 2)
TaxCode4.FormulaR1C1 = "=LOOKUP(2,1/(R[-12]C:R[-1]C<>"" ""),(R[-12]C[-1]:R[-1]C[-1]))"
TaxCode5 = TaxCode4.Value
'...
End Sub

Define a range using an existing named range & offset/resize

I am trying to use an existing named range to reference different rows (the columns should be the same). Right now I have the following:
Dim DetailONE, DetailONW, DetailW, DetailQc As Range
Set DetailONE = Sheet11.Range("CM12:EG13")
Set DetailONW = Sheet11.Range("EI12:GC13")
Set DetailW = Sheet11.Range("GE12:HO13")
Set DetailQc = Sheet11.Range("HQ12:IT13")
And then I refer to those named ranges but I get an Application defined or object defined error with the following. Does anyone know why? I don't know if I need the .Address part afterwards either or if I can forego this.
Dim OntarioWestDet, OntarioEastDet, WestDet, QuebecDet As Range
Set OntarioWestDet = Sheet11.Range(DetailONW.Address).Offset(3, 0).Resize(300, 0)
Set OntarioEastDet = Sheet11.Range(DetailONE).Offset(3, 0).Resize(300, 0)
Set WestDet = Sheet11.Range(DetailWe).Offset(3, 0).Resize(300, 0)
Set QuebecDet = Sheet11.Range(DetailQc).Offset(3, 0).Resize(300, 0)
This works for me (there is a typo in one of your names
Sub x()
Dim DetailONE As Range, DetailONW As Range, DetailW As Range, DetailQc As Range
Set DetailONE = Sheet11.Range("CM12:EG13")
Set DetailONW = Sheet11.Range("EI12:GC13")
Set DetailW = Sheet11.Range("GE12:HO13")
Set DetailQc = Sheet11.Range("HQ12:IT13")
Dim OntarioWestDet As Range, OntarioEastDet As Range, WestDet As Range, QuebecDet As Range
Set OntarioWestDet = DetailONW.Offset(3, 0).Resize(300)
Set OntarioEastDet = DetailONE.Offset(3, 0).Resize(300)
Set WestDet = DetailW.Offset(3, 0).Resize(300)
Set QuebecDet = DetailQc.Offset(3, 0).Resize(300)
End Sub

Excel vba -- Object required error at Sub line

So I am getting an error at the beginning of my code, an error I didn't use to get last time I opened and edited my VBA code. Any ideas? Here is part of it. When I try to step through the code, I get the error: "Object required" and my sub line (first line) is highlighted. Any ideas how I can fix this?
Sub ManagerCashflow()
'---------------------------Declare all the variables---------------------------
'------Define object names------
'Dim i As Integer
'Dim c As Integer
Dim AUM_Cash_Projections_folder_pathname As String
Dim AUM_Cash_Projections_FOLDER_YEARMONTH_pathname As String
Dim AUM_Cash_Projections_filename_DATE As String
Dim AUMCshf_wb As Workbook
Dim MngrCshF_wb As Workbook
'Dim CshF_lr As Integer
'Dim PE_r As Integer
'Dim lstmanager_r As Integer
'------Set/call the objects to a destination------
'Worksheets
'Manager Cashflow
Set MngrCshF_wb = ThisWorkbook
Set MCF_Current_ws = MngrCshF_wb.Sheets("Sheet1")
'AUM Cash Projections
Set AUM_Cash_Projections_folder_pathname = "https://iportal.casey.org/Risk Management/CFP Reporting/AUM Cash Projection"
Set AUM_Cash_Projections_FOLDER_YEARMONTH_pathname = Right(MCF_Current_ws.Cells(2, 1).Value, 7)
Set AUM_Cash_Projections_filenamedate = MCF_Current_ws.Cells(2, 1).Value
Set AUMCshf_wb = Workbooks.Open(AUM_Cash_Projections_folder_pathname + "/" + AUM_Cash_Projections_FOLDER_YEARMONTH_pathname + "/" + AUM_Cash_Projections_filenamedate)
Set CshF_ws = AUMCshf_wb.Sheets("CashFlow + Projections")
'Master Data with all of the current managers
Set CurrAssets_ws = AUMCshf_wb.Sheets("Master Data")
'... a bunch of other code that works....
End Sub
Not sure why it didn't happen before. You don't need to use set to assign a value to a string.
AUM_Cash_Projections_folder_pathname = "https://iportal.casey.org/Risk Management/CFP Reporting/AUM Cash Projection"
AUM_Cash_Projections_FOLDER_YEARMONTH_pathname = Right(MCF_Current_ws.Cells(2, 1).Value, 7)
AUM_Cash_Projections_filenamedate = MCF_Current_ws.Cells(2, 1).Value
You also need to declare MCF_Current_ws and your other worksheets. It won't tell you unless you have "Option Explicit" at the top of your code, but it's good to do.
Dim MCF_Current_ws as Excel.Worksheet

VBA Excel concatenating two variable values to form a new variable

I am trying to write a code that reads in multiple entities, catorgorizes and sorts them. Each entity has a type (A, B, C, etc.) that should determine what sheet it gets put into and all of them get put into my "All" sheet. Each time I find an entity of any given type I'd also like to increment a variable specific to that type.
What I'd like to do if find the type and do two things:
Set the current sheet to that type.
Set the counter variable to that type.
Example:
Dim x As Integer, FindSlot As Integer
Dim CurrentSheet As String, CurrentPropertyNumb As String
Dim APropertyNumb As String, BPropertyNumb As String
Dim CPropertyNumb As String
For x = 1 to 2
If x = 1 Then
CurrentSheet = "All"
Else
CurrentSheet = Range("B" & FindSlot)
CurrentPropertyNumb = CurrentSheet & PropertyNumb
End If
Next x
In the else block, CurrentSheet will get set to "A", "B", "C" or whatever the type is. Then I'd like CurrentPropertyNumb to get set to "APropertyNumb" or "BPropertyNumb" etc. Obviously I could do this with several If statements but it would end up being 12 of them which I'd rather avoid plus I think this would be cool! :)
Is there any way to do this or am I being too lofty with my goals?
If you have a series of values which you'd like to index using a string value then a Dictionary is a good fit:
Dim x As Integer, FindSlot As Integer
Dim CurrentSheet As String, CurrentPropertyNumb As String
Dim PropNums as Object
Dim CPropertyNumb As String
Set PropNums = CreateObject("scripting.Dictionary")
For x = 1 to 2
If x = 1 Then
CurrentSheet = "All"
Else
CurrentSheet = Range("B" & FindSlot)
If Not PropNums.Exists(CurrentSheet) Then
PropNums.Add CurrentSheet, 1 '? what are the initial values here?
Else
PropNums(CurrentSheet) = PropNums(CurrentSheet) +1
End If
CurrentPropertyNumb = PropNums(CurrentSheet)
End If
Next x

excel VBA End method on Range object -- application-defined or object-defined error

I'm experiencing an error with using the End method of the range object.
The code below is an excerpt from my program, and when I get to the last line I get the following error: "Application defined or Object Defined error"
Sub DataGroup(testCell, j, k)
Dim archive As Range
Set dataCellsA = Worksheets("DATA").Range("B2")
Set dataCellsB = Worksheets("DATA").Range("I2")
For i = 0 To 30
Set currentLine = testCell.Offset(i, 0)
Set archive = Worksheets("ARCHIVE").Range("B4").End(x1Down)
However, in a later portion of my program I have what appears to be the same implementation, but I don't receive any errors with this portion.
Set dataCellsA = Worksheets("DATA").Range("C2")
Set dataCellsB = Worksheets("DATA").Range("J2")
Dim lastCellA As Range
Dim lastCellB As Range
Set lastCellA = dataCellsA.End(xlDown)
Set lastCellB = dataCellsB.End(xlDown)
Whats the difference between the two? I'm making sure to declare my variables as ranges, and I'm careful to use the "Set" prefex to ensure that it is an object, and not the value.
So, here is the answer: you have x1Down where you should have xlDown (1 instead of l)