VB syntax (named arguments) - vba

I have a VB script with a line that looks like this:
Set startCell = referenceCell.EntireColumn.Find(tmp).offset(0,columnOffset)
But I want to specify the find (to search exactly for the given word in each column) to something like:
Set startCell = referenceCell.EntireColumn.Find(tmp, lookat:=xlWhole).offset(0,columnOffset)
all according to
http://msdn.microsoft.com/en-us/library/office/ff839746(v=office.15).aspx
But that gives me a syntax error. (I hate VB)
I have also tried
Set tmp = "Precondition" & preconditionNumber
Set startCell = ReferenceCell.EntireColumn.Find(what:=tmp,lookat:=xlWhole).offset(0,columnOffset)
and even
Set startCell = ReferenceCell.EntireColumn.Find(what:=tmp).offset(0,columnOffset)
none of which works.
How should I call the Find function to get a Whole Word-Search?
The variable declaration looks like this:
Dim startCell
for preconditionNumber = 0 to 15
Set startCell = Nothing
tmp = "Precondition" & preconditionNumber
Set startCell = referenceCell.EntireColumn.Find(tmp).offset(0,columnOffset)
...
Here is the exact syntax error message.

Use something like:
Sub luxation()
Dim ReferenceCell As Range, rCol As Range, tmp As String
Dim GotIt As Range, MoveOver As Range, columnOffset As Long
Set ReferenceCell = Range("B9")
Set rCol = ReferenceCell.EntireColumn.Cells
tmp = "happiness"
columnOffset = 2
Set GotIt = rCol.Find(what:=tmp, after:=rCol(1), lookat:=xlWhole)
Set MoveOver = GotIt.Offset(0, columnOffset)
MoveOver.Select
End Sub
Fixing VBA hatred is even easier, just repeat:
VBA is my friend.
30 times every morning

Related

Error 13 - Type mismatch - Index Match

Getting the above error on the Index/Match. Will try and keep this short and sweet but I am a VBA noob. Everything that is called has data in. One thing I noticed was that RefCol (a range of numbers) has leading and trailing whitespace when I do a Debug Print. However when I tested the length of the value it returned the correct values.
I can't understand what is breaking it, I did an index match in the workbook itself and it works perfectly.
Private Sub Ref_Change()
Dim ws As Worksheet
Dim tbl As ListObject
Set ws = Worksheets("Details")
Set tbl = ws.ListObjects("Call_Log")
Dim RefCol As Range
Dim NameCol As Range
Dim PhoneCol As Range
Dim DateCol As Range
Set RefCol = tbl.ListColumns("Ref Number").DataBodyRange
Set NameCol = tbl.ListColumns("Caller Name").DataBodyRange
Set PhoneCol = tbl.ListColumns("Telephone").DataBodyRange
Set DateCol = tbl.ListColumns("Date").DataBodyRange
Me.CallDate.Value = Application.WorksheetFunction.Index(DateCol, Application.Match(Me.Ref.Value, RefCol, 0))
End Sub
Have I set this up correctly?
Thanks
Evan
As stated most likely the Match is not being found and an error is being passed to the INDEX.
Pull the MATCH out and test for the error before finding the correct cell in the data.
Private Sub Ref_Change()
Dim ws As Worksheet
Dim tbl As ListObject
Set ws = Worksheets("Details")
Set tbl = ws.ListObjects("Call_Log")
Dim RefCol As Range
Dim NameCol As Range
Dim PhoneCol As Range
Dim DateCol As Range
Set RefCol = tbl.ListColumns("Ref Number").DataBodyRange
Set NameCol = tbl.ListColumns("Caller Name").DataBodyRange
Set PhoneCol = tbl.ListColumns("Telephone").DataBodyRange
Set DateCol = tbl.ListColumns("Date").DataBodyRange
Dim mtchRow As Long
mtchRow = 0
On Error Resume Next
mtchRow = Application.WorksheetFunction.Match(Me.ref.Value, RefCol, 0)
On Error GoTo 0
If mtchRow > 0 Then
Me.CallDate.Value = DateCol.Cells(mtchRow, 1).Value
Else
MsgBox "'" & Me.ref.Value & "' not found, or lookup array is more than one column or row"
End If
End Sub

Defined Variable as a reference in an Offset Range

I have run into trouble writing a code in VBA that will allow me to describe a range of non-consecutive cells when one of those cells is a variable. When I run this line of code I get an error at the line beginning with Range(copyToRange) = Application.WorksheetFunction.Average(copyFromRange). Sorry if this is a simple fix, I've been banging my head against a wall all day:
Sub GetPCData()
'Get PC response ratios
PCanalytes = Array("Furosemide", "Caffeine", "Ketoprofen", "Phenylbutazone", "Flunixin")
PCanalytePositions = Array("J32", "J33", "J34", "J35", "J36")
Set SQWorkbook = Application.ActiveWorkbook
Dim sourceSheet, targetSheet As Worksheet
Dim copyFromRange, copyToRange As Range
Dim Y As Range
Set targetSheet = ThisWorkbook.Sheets("QC data")
For i = 0 To SQWorkbook.Worksheets.Count
Set sourceSheet = SQWorkbook.Worksheets(PCanalytes(i))
Set Y = sourceSheet.Range("A7").End(xlDown)
Set copyToRange = targetSheet.Range(PCanalytePositions(i))
Set copyFromRange = sourceSheet.Range(("H8"), Y.Offset(0, 7))
Range(copyToRange) = Application.WorksheetFunction.Average(copyFromRange)
Next i
End Sub
Incorrect syntax!
Try this:
copyToRange.Value = Application.WorksheetFunction.Average(copyFromRange)
Why?, because you are telling Excel "copyToRange" is a RANGE already:
Dim copyFromRange, copyToRange As Range
Hope this help you.

Excel VBA find address and assign to variable then reuse value

I'm trying to find if there's a given title on a cell, pass the address of that cell to a variable and use such location to adjust the size of the column. The reason I'm doing this is because I'm writing several functions which will shift the position of the columns. I'd appreciate it if someone could take a look and tell me what I'm doing wrong.
Option Explicit
Sub adjustColumns()
Dim PONumberCell As String
Dim PONumberAddress As Range
Dim TopLabelinColumn As Range
For Each TopLabelinColumn In Range("A1:Z1").Cells
If TopLabelinColumn Like "PO_NUMBER" Then TopLabelinColumn.Value = "PO"
PONumberCell = TopLabelinColumn.Address
Set PONumberAddress = PONumberCell
PONumberAddress.ColumnWidth = 70
Next TopLabelinColumn
End Sub
edited after OP's further request:
you are confusing a Range object (such as PONumberCell is meant to be) with a String variable one (like PONumberAddress), so
Set PONumberAddress = PONumberCell
doesn't work because you are trying to assign an object variable to a String one
but you can be more effective avoiding the loop and using the Find() method
Option Explicit
Sub adjustColumns()
Dim PONumberAddress As String
Dim PONumberCell As Range
Set PONumberCell = Range("A1:Z1").Find(what:="PO_NUMBER", LookIn:=xlValues, lookat:=xlPart, MatchCase:=False)
If Not PONumberCell Is Nothing Then
With PONumberCell
.value = "PO"
PONumberAddress = .Address
.EntireColumn.ColumnWidth = 70
End With
Else
Set PONumberCell = Range("A1:Z1").Find(what:="PO", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False) '<--| if it didn't find "PO_NUMBER" then it seaches for a complete match of "PO"
If Not PONumberCell Is Nothing Then PONumberCell.EntireColumn.ColumnWidth = 70
End If
End Sub
Following the comments above, there are a few erros in your code:
Setting the PONumberAddress Range, you need to use the syntax : Set PONumberAddress = Range(PONumberCell) using the address string found in brackets.
To set the column width, use : PONumberAddress.Columns.ColumnWidth = 70.
According to your post, I think you want to do this only for columns where the header text is "PO_NUMBER", therefore you need all the code below inisde your If : If TopLabelinColumn.Value Like "PO_NUMBER" Then.
Code
Option Explicit
Sub adjustColumns()
Dim PONumberCell As String
Dim PONumberAddress As Range
Dim TopLabelinColumn As Range
For Each TopLabelinColumn In Range("A1:Z1").Cells
If TopLabelinColumn.Value Like "PO_NUMBER" Then
TopLabelinColumn.Value = "PO"
PONumberCell = TopLabelinColumn.Address
Set PONumberAddress = Range(PONumberCell)
PONumberAddress.Columns.ColumnWidth = 70
End If
Next TopLabelinColumn
End Sub

someExcel VBA: Cannot create a range object successfully

This is my first question here, so bear with me. I'm a security consultant working on a huge firewall migration, for which I got my VBA skill from under a thick layer of dust. So far I have managed to get all my issues resolved by searching, but this issue: I get errors when doing exactly how I find it everywhere.
What I want to do:
I have an array that contains (among other things), strings formatted like this: "A3:P59", representing a cell range.
Now, this are ranges within a table. When I get the address of a certain cell in the table, I want to test if it's in that range.
I wrote a test function:
Function TestCellRange() As Boolean
Dim tbl As ListObject
Dim cell, rng, test As range
Dim range As range
Dim bRow, eRow As Integer
Set tbl = shRulebase.ListObjects("tblBFFirewallRules")
shRulebase.Activate
With shRulebase
cell = tbl.DataBodyRange(5, 1).Address(False, False) 'it's this command that gives me issues
Set range = .range(.Cells(bRow, 1), .Cells(eRow, 16))
Debug.Print cell
'Set rng = shRulebase.range(range)
Debug.Print rng
Set test = Application.Intersect(cell, range(range(A3), range(P59)))
If test Is Nothing Then
MsgBox ("oops")
TestCellRange = False
Else
MsgBox ("yup yup")
TestCellRange = True
End If
End With
End Function
Now whatever I try, I keep getting blocked on the set range:
set range = .Range("A3:P59") -> will return "object required", on the "set test" line (if i use intersect (cell, range))
Set range = range("A3:P59") -> will return object variable or with block variale not set on the same line
Set range = .range(.Cells(bRow, 1), .Cells(eRow, 16)) -> will step through, but debug.print returns a type mismatch and "Set test = Application.Intersect(cell, range)" returns a "object required"
Any help would be really appreciated...I'm all to familiar with networks ip's and the bits and bytes of it, but here I am a bit out of my comfort zone and I need to finish this by tomorrow :(
Greetings,
Kraganov
EDIT Some More tries:
rng and cell as variant:
cell = tbl.DataBodyRange(5, 1).Address(False, False)
rng = .range("A3:P59").Address(False, False)
Set test = Application.Intersect(cell, rng)
==>I would get objects required
just using rng as range and trying to set it without "set"
rng = .range("A3:P59")
EDIT 2 : I found a way around using the range.
So what I was trying to do, was the following:
I had a table that contains information about firewall rules. However, not every line describes a rule. There are also lines that described the context in which the rules below that line were to be placed.
Outside of the table, aside of those lines there would be a cell with the range of cells for that context. I wanted to use that to describe the context for those rules, if I pulled them.
I ended up looping through the table rows and identifying those specific rows and setting a "context" variable when, a row like that was met.
Try setting the cell as well as following:
set cell = tbl.DataBodyRange(5, 1).Address(False, False)
What is cell? A Range?
You do not need to add 'set' to the range value assignment.
Try just
range = .Range("A3:P59")
Function TestCellRange() As Boolean
Dim tbl As ListObject
Dim cellToTest As Range
Dim testResult As Range
Set tbl = shRulebase.ListObjects("tblBFFirewallRules")
Set cellToTest = tbl.DataBodyRange.Cells(5, 1)
'or with one more level of indirection
'Set cellToTest = shRulebase.range(tbl.DataBodyRange.Cells(5, 1).Value)
Set testResult = Application.Intersect(cellToTest, [A3:P59])
If testResult Is Nothing Then
MsgBox ("oops")
TestCellRange = False
Else
MsgBox ("yup yup")
TestCellRange = True
End If
End Function
Thanks to the post of VincentG I found the working solution. Thanks for that.
Function TestCellRange() As Boolean
Dim tbl As ListObject
Dim cellToTest As range
Dim testResult As range
Set tbl = shRulebase.ListObjects("tblBFFirewallRules")
shRulebase.Activate
Set cellToTest = tbl.DataBodyRange.Cells(5, 1)
'or with one more level of indirection
'Set cellToTest = shRulebase.range(tbl.DataBodyRange.Cells(5, 1).Value)
Set testResult = Application.Intersect(cellToTest, range("A3:P59"))
If testResult Is Nothing Then
MsgBox ("oops")
TestCellRange = False
Else
MsgBox ("yup yup")
TestCellRange = True
End If
End Function

how to use variable in range using vba

Hello I have written code for generating graph using vba. everything working correctly ,but problem is i want to use variable for selecting particular column
the code is :
Set x = Range("$CF$2", Range("$CF$2").End(xlDown))
Set y = Range("$CG$2", Range("$CG$2").End(xlDown))
Dim c As Chart
Set c = ActiveWorkbook.Charts.Add
Set c = c.Location(Where:=xlLocationAsObject, Name:=assume)
With c
.ChartType = xlXYScatterLines
' set other chart properties
With .Parent
.Top = Range("cl1").Top
.Left = Range("cl12").Left
.Name = "c"
End With
End With
Dim s As Series
Set s = c.SeriesCollection(1)
With s
.Values = y
.XValues = x
' set other series properties
End With
I want to use variable COLs in first to line they are
Set x = Range("$CF$2", Range("$CF$2").End(xlDown))
Set y = Range("$CG$2", Range("$CG$2").End(xlDown))
COLs is variable of string
I'm not sure I understand, but if you want a Range object based on a string, why not try this:
Option Explicit
Sub TestRange()
'***** Declare variables
Dim oX As Range
Dim sCOLs As String
'***** Select column
sCOLs = "A"
'***** Set Range based on column from sCOLs
Set oX = Range(sCOLs & "2", Range(sCOLs & "2").End(xlDown))
'***** Do something with oX
Debug.Print TypeName(oX)
'***** Clean up
Set oX = Nothing
End Sub
You could also try and have the whole range as a string, maybe a bit cleaner code?
Dim sRange as String
sRange = "A2"
Set oX = Range(sRange, Range(sRange).End(xlDown))
You could also use Inputbox to have the user click on a certain cell. This then creates a variable "UserRange" which contains the cell reference you can use.
Sub test()
Dim UserRange As Range
Set UserRange = Application.InputBox(Prompt:="Please Select Range", Title:="Range Select", Type:=8)
UserRange.Value = "Test"
End Sub