This question already has answers here:
VBA- variable in a range, inside a formula
(2 answers)
Closed 5 years ago.
Hi I am trying to refer to certain rows to do something in VBA.
The code is something like below:
Sub test()
Dim k As Long
k = 9
Rows("5:k").Select
End Sub
I am trying to make my rows dynamic by changing the k value each time. however I am not sure why it cannot select the rows and the code don't work. Any ways to go around this issue? If I substitute k with 9 directly, the code works. But this does not happen.
Am I supposed to select something like rows("5:k(value)).select ?
The problem is that VBA can't recognise the k variable inside the " "
Try something like:
Sub test()
Dim k As Long
k = 9
Rows("5:" & k).Select
End Sub
k is outside the " " so VBA recognises it as a variable and the & tells VBA to concatenate the value of k onto what's inside the " "
Related
This question already has answers here:
Deleting Duplicate Visible Rows
(2 answers)
Code not deleting every non numeric row
(1 answer)
VBA For loop not exiting
(3 answers)
Delete specific rows from selection in for each loop
(1 answer)
Excel ListObject Table - Remove filtered / hidden rows from ListObject table
(1 answer)
Closed 4 years ago.
I am trying to write a code that goes row by row checking to see if a certain column in that row contains the string values "Pick-Up" or "Pickup", and if it does delete that entire row. The code I currently have runs, and deletes some of the rows containing said values. But not all of them.
I have tried the AutoFilter method, and for my purposes I do not believe that it will work.
Any help would be greatly appreciated. Thanks!
Sub Delete()
Dim x As Long, y As Long
Dim testString As String
y = Worksheets("Data").Range("A1", Range("A1").End(xlDown)).Rows.Count
For x = 2 To y
testString = Worksheets("Data").Cells(x, 27).Value
testString = Trim(testString)
If testString Like "*Pick-Up*" Or testString Like "*Pickup*" Then
Rows(x).EntireRow.Delete
Else
x = x + 1
End If
Next x
End Sub
So in Excel, we know it's possible to test against multiple criteria via concatenation, like this:
MATCH(criteria1&criteria2, Range(), 0)
where criteria1 and criteria2 are 2 separate criteria.
I'm trying to automate this thing in Excel VBA, like this:
WorksheetFunction.Match(criteria1 + "&" + criteria2, Range(), 0)
My question is, how do I replicate the same concatenation of criteria with the ampersand, in VBA form? In the version above, Excel keeps telling me it can't use the Match function of the WorkSheetFunction class, which I'm attributing to the failed concatenation attempt above. Any suggestions or advice would be greatly appreciated.
Oh, and here's a link to a Microsoft Knowledge Base article about using multiple criteria in MATCH(): http://support.microsoft.com/kb/59482
EDIT: I realized I wasn't putting 2 ranges to correspond with my 2 criteria. The problem is I don't know how to concatenate 2 ranges, because mine are in the form:
Range(Cells(1,columnIndex),Cells(rowCount,columnIndex))
as opposed to A1:A200. Any ideas on how to convert, or to concat the ranges in their current form?
This works:
Sub dural()
crit1 = "find "
crit2 = "happiness"
N = Application.WorksheetFunction.Match(crit1 & crit2, Range("A1:A10"), 0)
MsgBox N
End Sub
with, say A3 containing:
find happiness
EDIT#1:
Consider the case of multiple criteria in several columns. For example:
and we want VBA to retrieve the name of a small, black, dog
without VBA in a worksheet cell we can use:
=INDEX(D1:D100,SUMPRODUCT(--(A1:A100="dog")*(B1:B100="small")*(C1:C100="black")*ROW(1:100)),1)
to get the name Oscar
we can use the same formula in VBA
Sub luxation()
Dim s As String, s2 As String
s = "INDEX(D1:D100,SUMPRODUCT(--(A1:A100=""dog"")*(B1:B100=""small"")*(C1:C100=""black"")*ROW(1:100)),1)"
s2 = Evaluate(s)
MsgBox s2
End Sub
Doesn't readily map to a VBA implementation, but you can cheat a bit using Evaluate:
Sub Tester()
Dim v1, v2, f
v1 = "y"
v2 = 11
Sheet1.Names.Add Name:="X", RefersTo:=v1
Sheet1.Names.Add Name:="Y", RefersTo:=v2
f = "MATCH(X&Y,$A$2:$A$5&$B$2:$B$5, 0)"
Debug.Print Sheet1.Evaluate(f)
End Sub
or skipping the names:
Sub Tester2()
Const F_TEMPL As String = "MATCH(""{V1}""&""{V2}"",$A$2:$A$5&$B$2:$B$5, 0)"
Dim v1, v2, f
f = Replace(F_TEMPL, "{V1}", "x")
f = Replace(f, "{V2}", 12)
Debug.Print Sheet1.Evaluate(f)
End Sub
You still need to send the MATCH argument body as a string. The '+' does not concatenate.
WorksheetFunction.Match(criteria1 & "&" & criteria2, Range(), 0)
Should concatenate the two values and execute the match.
This question already has answers here:
Code in VBA loops and never ends. How to fix this?
(2 answers)
Closed 5 years ago.
I am trying to delete entire rows based on whether the cell value in the D column is NULL or not. My entire code so far is:
Sub DeleteNULL()
Dim i As Long
For i = 2 To 119713
If IsEmpty(Range("Di")) = True Then
Rows([i]).EntireRow.Delete
Else
If IsEmpty(Range("Di")) = False Then
Next i
End If
End Sub
I keep getting compile errors, either If without Else or Next without For, how should I fix this?
Thanks in advance.
A few things:
Placement of a lot of syntax is off.
When adding or deleting rows, you need to loop backwards based on how Excel handles these events.
See code below:
Sub DeleteNULL()
Dim i As Long
For i = 119713 To 2 Step -1
If IsEmpty(Range("D" & i)) Then Rows([i]).EntireRow.Delete
Next i
End Sub
I'm trying to write a macro that takes a string variable as an input, with the string variable referencing a named range.
Currently what I have is:
Sub SubItems()
Dim M As String
M = "=R[-1]C"
'where M refers to row above, currently it is Manufacturers
Dim g As Range
Set g = Range(" & M & ")
ActiveCell.Value = g(2)
'For Example
End Sub
The problem is with the Set g = Range(" & M & ") syntax
I want the input argument for the Range function to be what M is, and not the literal letter M. Similar to how in C you would do printf('%s', M) for example.
Edit:
Currently how I have the excel sheet setup, is that you select a main item from a drop down menu. Then I want to select the cell below the main item and automatically fill in the rows with sub items. The sub items are stored in a named range that is named after the main item.
Hence I want my macro to automatically read the row above it (Main Item) hence why I have M = "=R[-1]C". Then I want to input that into the range function and that's the problem I'm currently facing.
I hope this clarifies my problem more clearly.
The problem is that your variable M describes a relative reference, and I do not believe you can apply a relative reference to a Range object in the manner you describe. Perhaps you want to attack your problem from a different angle? For instance:
You could use your method but rather set an absolute reference, e.g.
M = "A5"
Debug.Print Range(M).Value
Or alternatively you could specify a relative reference using code such as:
debug.print activecell.Offset(-1,0).Value
String literals are encased in double quotes, so you're essentially referencing a named Range called & M & .
If you're using a string variable, remember that its value is surrounded by quotes as well. So it should just be Range(M), or Range("=R[-1]C"), the two are the same.
Note: =R[-1]C is a very strange name, are you sure you meant this? Make sure you understand the difference between a named range and what's in the range (it looks like a formula)! Perhaps some description on what =R[-1]C is and I can help you more?
What I have understood so far - you have a named range, called M and you want to assign it to a VBA range. If this is the case, this is the code to achieve it:
Sub TestMe()
Dim g As Range
Set g = ActiveSheet.[M]
Debug.Print g.Address
End Sub
This question already has answers here:
How do I put double quotes in a string in vba?
(5 answers)
Closed 5 years ago.
I can't operate the code without it stating the sub or function is not defined in the formula I'm trying to paste down column v.
Sub SEALANTSCHEDULIZER()
' 1. Unhide All Columns
Columns.EntireColumn.Hidden = False
' 2. Clear All color highlights
ActiveSheet.UsedRange.Interior.ColorIndex = 0
' 3. Drag formulas in all columns down to last row (if necessary) – use column A (ID) as row count
LastRowColumnA = cell(Rows.count, 1).End(xlUp).Row
Range("V2:V" & LastRowColumnA).Formula = "=IF(OR(W2="Yes", AE2="Yes"), "Yes", "No")"
End Sub
You need to double the " around the text strings.
"=IF(OR(W2=""Yes"", AE2=""Yes""), ""Yes"", ""No"")"