I am trying to create a VBA macro to fill in cells that are empty in a range ("INV_Nums") without overwriting the cell if it contains data already. In order to accomplish this I am trying to use an if statement to check if the cell in question is blank...if it is not, then I want the loop to continue on to the next cell, however if it IS blank then I want to input the index(__,(match()) function into the cell.
I keep getting a "compile error: mismatch" on the True statement but I'm at a loss as to why my synatax would be wrong. Any help would be appreciated.
Here is my code:
Dim i As Integer
Dim Rng As Range
Dim ARwkb As Excel.Workbook
Dim ARwks As Excel.Worksheet
Dim Samwkb As Excel.Workbook
Dim Samwks As Excel.Worksheet
Set Samwkb = Excel.Workbooks("Samples - one sheet")
Set Samwks = Samwkb.Worksheets("samples shipment")
Set ARwkb = Excel.Workbooks("AR balance.xlsx")
Set ARwks = ARwkb.Worksheets("Total Trading")
Set Rng = Samwkb.Range("INV_Nums")
For i = 6 To Rng.Rows.Count + 6
If Range("AAi") = "" Is True Then
Range("AAi").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('AR balance.xlsx'!AR_Invoice_Nums,MATCH(RC[-21],'AR
balance.xlsx'!AR_PL_Nums,0))"
End If
Next i
The problem is in how you are identifying the range and administering the criteria.
For i = 6 To Rng.Rows.Count + 6
If IsEmpty(Range("AA" & i)) Then
Range("AA" & i).FormulaR1C1 = _
"=INDEX('AR balance.xlsx'!AR_Invoice_Nums, MATCH(RC[-21],'AR balance.xlsx'!AR_PL_Nums, 0))"
End If
Next i
The .SpecialCells method can quickly determine the blank cells and an xlR1C1 formula can be used to insert all of the formulas at once..
...
with Samwkb.Range("INV_Nums")
.specialcells(xlcelltypeblanks).FormulaR1C1 = _
"=INDEX('AR balance.xlsx'!AR_Invoice_Nums, MATCH(RC[-21],'AR balance.xlsx'!AR_PL_Nums, 0))"
end with
...
I'm trying to find something with a wild card search in a cell value. If the value in sheet("FC")Range("I2:I" & LastRowC) - match with the Sheets("Instr"),Range("A130:A190"). means sheet Instr match if few characters match with the other range mentioned above then do something code.
eg in sheet Instr above range a cell value is "Ajith" and In sheet FC above mentioned range one of the cell value is "Aji" the code should identify it.
All the below steps are okay for me except the wild card search through the loop range , please go through the code and range (rename the sheets if necessary as below) and provide an update.
Sub Exception()
Dim mfc As Worksheet
Dim mfp As Worksheet
Dim mfo As Worksheet
Dim instr As Worksheet
Set mfc = Sheets("FC")
Set mfp = Sheets("FP")
Set mfo = Sheets("OSLR")
Set inst = Sheets("Instr")
Dim irng As Range
Dim icel As Range
Set irng = inst.Range("A130:A190")
Dim LastRowC As Long
LastRowC = mfc.Cells(Rows.Count, 1).End(xlUp).Row
Dim fcphr As Range
Dim fcphc As Range
Set fcphr = mfc.Range("I2:I" & LastRowC)
For Each icel In irng.Rows
For Each fcphc In fcphr.Rows
If icel.Value = "" Then
Exit For
End If
If fcphc.Value = "" Then
Exit For
End If
If fcphc.Value = icel.Value Then
msgbox fcphc
msgbox icel
'***(i need a wild card search for the above step)***
End If
Next fcphc
Next icel
End Sub
You could use the Like operator. For example:
If fcphc.Value Like "*" & icel.Value & "*" Then
If you wanted the comparison to work both ways:
If _
fcphc.Value Like "*" & icel.Value & "*" Or _
icel.Value Like "*" & fcphc.Value & "*" _
Then
I am writing a little Excel-Macro with VBA. Now I would like to concat two Strings and save them into a String-Array.
What I got:
Dim rowNumberString As String
Dim colIndexString As String
Dim headerArray(1 To colIndexArrayRange) As String
colIndexNumber = 14
colCount = 5
rowNumberString = "12"
addAnotherColumnToArray = True
' Fill the column array with all the month's entries
While addAnotherColumnToArray
colCount = colCount + 1
colIndexNumber = colIndexNumber + 1
If colIndexArray(colCount) = "" Then
colIndexString = Split(Cells(1, colIndexNumber).Address(True, False), "$")(0)
colIndexArray(colCount) = colIndexString & rowNumberString
End If
Debug.Print colCount & "=" & colIndexArray(colCount)
If (colIndexNumber > 61) Then
addAnotherColumnToArray = False
End If
Wend
The output:
6=O
7=P
8=Q
9=R
10=S
11=T
12=U
' ....
So it seems that this line:
` colIndexArray(colCount) = colIndexString & rowNumberString`
is not concatenating the String the way it should. What did I do wrong? I thought the &-Operator would always work for Strings in VBA.
As I stated in my comment, you could be going about this in a completely different way.
Not sure what you are trying to accomplish, but a For...Next statement using Objects, rather than Strings should help you accomplish your task.
Option Explicit
Sub TEST()
Dim ws As Worksheet, Rng12 As Range, Cell As Range
Set ws = ThisWorkbook.Worksheets(1)
Set Rng12 = ws.Range("L12:Z12") 'Adjust your range
For Each Cell In Rng12
Debug.Print Cell.Address
Next Cell
End Sub
I have a nestled If And Or formula that I am trying to convert into probably a Case formula using VBA (or any other suggestion would be great), but I am a beginner and not sure how. The reason is that this formula currently is in every cell AG12:ACG500 and takes up so much memory that the spreadsheet is extremely slow.
Basically, I am trying to match the date in column Z12:Z500 (Outage Month Start) to the date in Row AG6:ACG6 (DATES), then looking in column C12:C500 (ACTV_NAME)... which provides the output of either R, S, L, MR, MS, ML, ?R, ?S or ?L in the cell where the two dates (column Z and row 6) intersect .. this should coincide with the where the Gantt Chart bar starts .. I do not need any help with the Gantt chart bars/color coding .. I just need help basically labeling them with the fore-mentioned.
=IF(OR($Z12="",$AA12=""),"",IF(AND(AG$6=$Z12,$Z12<>"",$C12="Relay",$L12="No"),"R",IF(AND(AG$6=$Z12,$Z12<>"",$C12="Substation",$L12="No"),"S",IF(AND(AG$6=$Z12,$Z12<>"",$C12="Line",$L12="No"),"L",IF(AND(AG$6=$Z12,$Z12<>"",$C12="Relay",$L12="YES"),"MR",IF(AND(AG$6=$Z12,$Z12<>"",$C12="Substation",$L12="YES"),"MS",IF(AND($AF$6=$Z12,$Z12<>"",$C12="Line",$L12="YES"),"ML",IF(AND(AG$6=$Z12,$Z12<>"",$C12="Relay",$L12="Maybe"),"?R",IF(AND(AG$6=$Z12,$Z12<>"",$C12="Substation",$L12="Maybe"),"?S",IF(AND($AF$6=$Z12,$Z12<>"",$C12="Line",$L12="Maybe"),"?L",""))))))))))
Your formula can be simplified to:
=IF(OR($Z12="",$AA12=""),"",IF(AND(AG$6=$Z12,$Z12<>""),IF($L12 = "YES","M" & LEFT($C12,1),IF($L12 = "Maybe","?" & LEFT($C12,1),LEFT($C12,1))),""))
Converting single formula into a routine that loops
This is Scott Craner's simplified version of your formula, converted into a routine that will loop through all the cells in AG12:ACG500 and check each cell for their intersecting criteria.
I am not able to test this code because I have no data set to base it off of. That being said, I'm not sure that it will perform in the way you desire. Let me know if it works for you.
Sub compareDates()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1)
Dim dataRange As Range: Set dataRange = ws.Range("AG12:ACG500")
Dim oMS As Range, aN As Range, idk As Range, d As Range
Dim yNM As Range, myCell As Range, myRow As Long, myCol As Long
For Each myCell In dataRange
myRow = myCell.Row
myCol = myCell.Column
Set oMS = ws.Range("Z" & myRow) 'Outage Month Start
Set aN = ws.Range("C" & myRow) 'ACTV_NAME
Set idk = ws.Range("AA" & myRow) 'not sure what AA is for
Set d = ws.Cells(6, myCol) 'DATES
Set yNM = ws.Range("L" & myRow) 'yes no maybe
If oMS.Value = "" Or idk.Value = "" Then
myCell.Value = ""
ElseIf d.Value = oMS.Value And oMS.Value <> "" Then
If UCase(yNM.Value) = UCase("Yes") Then
myCell.Value = "M" & Left(aN.Value, 1)
ElseIf UCase(yNM.Value) = UCase("Maybe") Then
myCell.Value = "?" & Left(aN.Value, 1)
Else: myCell.Value = Left(aN.Value, 1)
End If
Else: myCell.Value = ""
End If
Next myCell
End Sub
I have a spread sheet that look like so:
Group | Name | Title
-----------------------------------
X WS -
X DH -
X M -
X DH -
X WS -
I want to loop through all the cells in name and replace the initial there with their full name in addition to adding the correct title. My script is failing to accurately compare the strings and go into the if-statement:
Sub enterNameAndTitle()
lastCell = InputBox("Last cell")
rInitials = InputBox("Initials")
rFullName = InputBox("Full Name")
rTitle = InputBox("Title")
Dim cell As Range
For Each cell In Range("b2:b" & lastCell).Cells
MsgBox (cell.Text & " : " & rInitials)
If StrComp(UCase(cell.Value), UCase(rInitials)) = 0 Then
cell.Value = rFullName
ActiveSheet.Cells(cell.Row, cell.Column + 1).Value = rTitle
End If
Next cell
End Sub
So I first collect the data and then loop through all the values. Does anyone know what I am doing incorrectly? Why doesn't it compare the string accurately?
I don't see anything wrong, but there are 2 things I would try
One is to use TRIM to make sure neither string has leading or trailing blanks
The 2nd is to change the if to if(ucase(trim(cell.value))=ucase(trim(rInitials)))
The problem was one of differing types and the only way that seemed to work for me was to re-cast both variables as type String using CStr()
Sub enterNameAndTitle()
Dim lastCell As String
lastCell = InputBox("Last cell")
'Cast to string
Dim rInitials As String
rInitials = CStr(InputBox("Initials"))
Dim rFullName As String
rFullName = InputBox("Full Name")
Dim rTitle As String
rTitle = InputBox("Title")
Dim cell As Range
For Each cell In Range("b2:b" & lastCell).Cells
Dim cellText As String
'Cast to string
cellText = CStr(cell.Text)
If (Trim(UCase(cell.Value)) = Trim(UCase(rInitials))) Then
MsgBox ("test1")
cell.Value = rFullName
ActiveSheet.Cells(cell.Row, cell.Column + 1).Value = rTitle
End If
Next cell
End Sub