Object doesn't support this property or method - vba

I am essentially trying to match a series of cells from 2 worksheets and obtained values from a corresponding column.
When I run this code, I get an error:
object doesn't support this property or method
My Code
Sub Macro2()
Dim rowcount As Integer
Dim target As Variant
rowcount = Range("E2", Range("E2").End(xlDown)).Count
For i = 1 To rowcount + 1
target = Application.Match(ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3), Worksheets(14).Range("A6:A3000"), 0)
If ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3) = _
ActiveSheet.Cells(i + 1, 6) & "-" & Cells(i + 1, 5) & "-" & Cells(i + 1, 4) & "-" & Cells(i + 1, 3) Then
ActiveSheet.Cells(i, 17) = Worksheets(14).target.Offset(0, 10)
End If
Next i
End Sub

If you want to find the Row count, you need to use the syntax:
rowcount = Range(Range("E2"), Range("E2").End(xlDown)).Rows.Count , also it's better to use Long than Integer.
Also, you need to trap a possible error when unable to find a successful match with the Application.Match function, do it by using If Not IsError(target) Then.
Note: try to avoid using ActiveSheet, instead use fully qualified worksheet, by using Worksheets("YourSheetName") in your code.
Code
Sub Macro2()
Dim rowcount As Long
Dim target As Variant
rowcount = Range(Range("E2"), Range("E2").End(xlDown)).Rows.Count
For i = 1 To rowcount + 1
target = Application.Match(ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3), Worksheets(14).Range("A6:A3000"), 0)
If Not IsError(target) Then ' successful Match
If ActiveSheet.Cells(i, 6) & "-" & Cells(i, 5) & "-" & Cells(i, 4) & "-" & Cells(i, 3) = _
ActiveSheet.Cells(i + 1, 6) & "-" & Cells(i + 1, 5) & "-" & Cells(i + 1, 4) & "-" & Cells(i + 1, 3) Then
ActiveSheet.Cells(i, 17) = Worksheets(14).target.Offset(0, 10)
End If
Else
MsgBox "Unable to find a Match !"
End If
Next i
End Sub

target is not a Property or Method of a Worksheets object.
I believe you need to change Worksheets(14).target.Offset(0, 10) to Worksheets(14).Range("A5").Offset(target, 10).
You should also be consistent in your coding. In your code you have things like ActiveSheet.Cells(i + 1, 6) & "-" & Cells(i + 1, 5) ..., where you specifically qualify Cells(i + 1, 6) to be on ActiveSheet but allow Cells(i + 1, 5) to default to being on the ActiveSheet. Although it works, it will get very confusing if you ever need to reread your code later on.

Related

HLookup from one spreadsheet to Another

Please help HLookup from one workbook to another for n number of iteration
Windows(Files).Activate
Sheets("Total").Select
Set myrange = Sheets("Total").Range("1:5")
Windows(ThisFileName).Activate
Sheets("Acc").Select
Range("B" & n + 2, "V" & n + 2).Select
ActiveRange("B" & n + 2, "V" & n + 2).Formula = HLookup(Acc.Range("B" & n + 1, "V" & n + 1), myrange, 2, False)
You need to construct a string representing the worksheet formula.
Set myrange = workSheets("Total").Range("1:5")
with workSheets("Acc").Range("B" & n + 2, "V" & n + 2)
.Formula = "=hlookup(" & .address & ", " & myrange.address(external:=true) & ", 2, False)"
end with

Excel VBA To Concatenate

from some googling I found this function that will concatenate the data in columns A, B & C based off the value in column D. This code does not work for me for some reason. My data looks like such
Bob Jason 0123456789 Tim
Jim Jason 0123456789 Tim
Fred Jason 0123456789 Tim
Columns, A and B concat fine, but column C concats to
12,345,678,901,234,500,000,000,000,000
How would the VBA be altered so that the code will concatenate properly?
Sub Concat()
Dim x, i As Long, ii As Long
With Cells(1).CurrentRegion
x = .Columns("d").Offset(1).Address
x = Filter(Evaluate("transpose(if(countif(offset(" & x & ",,,row(1:" & .Rows.Count & "))," & x & ")=1," & x & "))"), False, 0)
For i = 0 To UBound(x)
For ii = 1 To 3
Cells(i + 2, ii + 5).Value = Join(Filter(Evaluate("transpose(if(" & .Columns(4).Address & "=""" & _
x(i) & """," & .Columns(ii).Address & "))"), False, 0), ",")
Next
Cells(i + 2, ii + 5).Value = x(i)
Next
End With
End Sub
You need to set the destination cells to a Text format:
Sub Concat()
Dim x, i As Long, ii As Long
With Cells(1).CurrentRegion
x = .Columns("d").Offset(1).Address
x = Filter(Evaluate("transpose(if(countif(offset(" & x & ",,,row(1:" & .Rows.Count & "))," & x & ")=1," & x & "))"), False, 0)
For i = 0 To UBound(x)
For ii = 1 To 3
Cells(i + 2, ii + 5).NumberFormat = "#"
Cells(i + 2, ii + 5).Value = Join(Filter(Evaluate("transpose(if(" & .Columns(4).Address & "=""" & _
x(i) & """," & .Columns(ii).Address & "))"), False, 0), ",")
Next
Cells(i + 2, ii + 5).NumberFormat = "#"
Cells(i + 2, ii + 5).Value = x(i)
Next
End With
End Sub

trying to join 3 statements together in a select case

I am trying to join these 3 statements together. I can get two to work but not the third.
Check is looking to see if the 5th character = a,b,c,d.
Check0 is looking to see if characterS 2-9 are all numbers.
Check3 is looking to see if the first 3 characters are numbers.
Desired effect
Before > After
Check 0 e01730101.pdf > S-173-0101
Check3 173d00510.pdf > S-173-D005
Check e173d0061.pdf > S-173-D006
Right now Check0 is not working. When I run the code it seems to skip my Case 13 Check0 statement. Anyway i can write this so the 3 checks are not conflicting each other?
Option Explicit
Sub Convert()
Application.ScreenUpdating = False
Dim rng As Range, aCell As Range
Dim val As String, Check, Check0, Check3
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Set rng = Range("A2:A" & LastRow)
For Each aCell In rng.Cells
Select Case Len(aCell)
'Case 12 left out
Case 13
Check = Mid(aCell, 5, Len(aCell) - 12)
If Check = "a" Or Check = "b" Or Check = "c" Or Check = "d" Then 'Existing Standard
val = "S-" & Left(aCell, Len(aCell) - 13) & Mid(aCell, 2, Len(aCell) - 10) & "-" & Mid(aCell, 5, Len(aCell) - 9)
Check0 = IsNumeric(Mid(aCell, 2, Len(aCell) - 5)) '|||PROBLEM|||
ElseIf Check0 = True Then 'Existing Three Line Diagrams
val = "S-" & Left(aCell, Len(aCell) - 10) & "-" & Mid(aCell, 4, Len(aCell) - 9)
End If
Check3 = IsNumeric(Left(aCell, 3))
If Check3 = True Then 'Standard after page 9
val = "S-" & Mid(aCell, 1, Len(aCell) - 10) & "-" & Mid(aCell, 4, Len(aCell) - 9)
End If
Check = ""
Check0 = ""
Check3 = ""
'Case 14 left out
Case Else 'All other pages
val = "_Mod " & Left(aCell, Len(aCell) - 4)
End Select
val = UCase(val)
val = val & " " & aCell.Offset(, 2) & aCell.Offset(, 3)
aCell.Offset(, 1).Value = val
Next
Application.ScreenUpdating = True
End Sub
The issue is that your Check0 variable is not initialised when you check it. It would be best to first do all your three checks and only then perform the If...ElseIf....
Secondly, this expression is using the wrong parts of the input string:
val = "S-" & Left(aCell, Len(aCell) - 10) & "-" & Mid(aCell, 4, Len(aCell) - 9)
I don't really understand why you use Len(aCell) - something, since it is known that the length is 13. Certainly Left(aCell, Len(aCell) - 13) is quite useless, as it will be the empty string.
Here is a correction with some other optimisations (pos is a Long):
Case 13
Check = InStr("abcd", Mid(aCell, 5, 1))
Check0 = IsNumeric(Mid(aCell, 2, 8))
Check3 = IsNumeric(Left(aCell, 3))
pos = IIf(Check3, 1, _
IIf(Check, 2, _
IIf(Check0, 3, 0)))
val = IIf(pos, "S-" & Mid(aCell, pos, 3) & "-" & Mid(aCell, pos+3, 4), "??")

How to specify the sheet a worksheet function should use via a range object?

I have some code that searches a column of values in sheet(3) in the format of a string "value1 - value 2"
value2 is the first value in a column in sheet(2) and value1 is a value in the same column, in a cell further down the sheet.
The setup I have is:
In sheet(1), cells C2:C6 have values a-e respectively
In sheet(2), cell C1 has value "yes" and cells C2:C6 have values 1-5
respectively
In sheet(3), cell A2 has the value "4 - yes"
So the code should countif a column in sheet2 with the first value being yes and look for cells with the value 4, and put the result in cell B2 on sheet(3)
What it actually does is find the yes column (column C) and search the same column on sheet(1) (so the message boxes show letters rather than numbers).
Is there a way I can more precisely specify the sheet the countif function uses?
I'm using Excel 2000 on Windows 7
Private Sub test_click()
scenario_count = 6
Dim i As Integer
i = 1
Sheets(2).Select
For j = 2 To 24
If Sheets(2).Cells(1, j).Value = Right(Sheets(3).Cells(i + 1, 1).Value, Len(Sheets(3).Cells(i + 1, 1).Value) - InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 1) Then
MsgBox ("number of scenarios is " & scenario_count)
MsgBox ("value searching for is " & "'" & Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2) & "'")
MsgBox ("Range searched is " & Range(Cells(2, j), Cells(scenario_count, j)).Address & " in " & ActiveSheet.Name)
MsgBox ("Number of occurrences " & Sheets(2).Application.WorksheetFunction.CountIf(Range(Cells(2, j), Cells(scenario_count, j)), Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2)))
Sheets(2).Select
Sheets(3).Cells(i + 1, 2).Value = Sheets(2).Application.WorksheetFunction.CountIf(Range(Cells(2, j), Cells(scenario_count, j)), Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2))
For Each c In Range(Cells(2, j), Cells(scenario_count, j))
MsgBox ("comparing " & c.Address & " " & c.Value & " with " & Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2))
Next c
GoTo endofif2
End If
Next
endofif2:
End Sub
Where you have 'WorksheetFunction.CountIf(Range(Cells(2, j)', simply insert the sheet before the range reference, like so:
Sheets(2).Range(Sheets(2).Cells(2, j), Sheets(2).Cells(scenario_count, j))
EDIT full formula which references the sheet for both the Cells and the Range functions blatently taken from #Rory's comment.

Excel VBA - Sum between two values

I have a report where I'm trying to get the sum of a dynamic number of rows in order to produce a subtotal.
If Cells(s, 1).Value = "start" Then
If Cells(r, 1).Value = "subtotal" Then
'Set the Monthly Subtotal Formulas
Cells(r, 44) = "=SUM(AR" & Trim(Str(s)) & ":AR" & Trim(Str(r - 1)) & ")"
Cells(r, 46) = "=SUM(AT" & Trim(Str(s)) & ":AT" & Trim(Str(r - 1)) & ")"
'Set the Weekly Subtotal Formulas
Cells(r, 48) = "=SUM(AV" & Trim(Str(s)) & ":AV" & Trim(Str(r - 1)) & ")"
Cells(r, 52) = "=SUM(AZ" & Trim(Str(s)) & ":AZ" & Trim(Str(r - 1)) & ")"
'Set the Daily Subtotal Formulas
Cells(r, 54) = "=SUM(BB" & Trim(Str(s)) & ":BB" & Trim(Str(r - 1)) & ")"
Cells(r, 56) = "=SUM(BD" & Trim(Str(s)) & ":BD" & Trim(Str(r - 1)) & ")"
'Set the Hourly Formulas
Cells(r, 60) = "=SUM(BH" & Trim(Str(s)) & ":BH" & Trim(Str(r - 1)) & ")"
Cells(r, 62) = "=SUM(BJ" & Trim(Str(s)) & ":BJ" & Trim(Str(r - 1)) & ")"
Cells(r, 1) = ""
End If
Cells(s, 1) = ""
End If
Basically, each work group is within the cell values "start" and "subtotal".
How can I find the 's' or row number and use that in the formula?
most of the time, built-in subtotals feature of Excel should be sufficient
in case you really need to use VBA solution and don't know how to iterate it over all "subtotal" tags already present in the data, place your code inside a loop like this:
header_column = Intersect(ActiveSheet.Range("A:A"), ActiveSheet.UsedRange).Value2
s = 1
For r = 1 To UBound(header_column)
If header_column(r, 1) = "start" Then
s = r
End If
If header_column(r, 1) = "subtotal" Then
' ... do your stuff here ... '
' s = r ' if the next "start" tag always follows a subtotal tag, no need for the "start" tags at all, just uncomment this line just before End If
End If
Next
P.S.: no need for "string" & Trim(Str(integer)), use "string" & integer instead