I am new to Excel VBA.
I have these random columns:
Range("Y1").Value = "LITIGATE_PERSON_ADDRESS"
Range("Z1").Value = "LITIGATE_PERSON_CITY"
Range("AA1").Value = "LITIGATE_PERSON_TK"
Range("AB1").Value = "LITIGATE_PERSON_ADDRESS_TYPE"
Here's the part of code that is being used for these columns.
Worksheets("MAIN_CONTROL").Cells(i, 25).Value = sourceADR
sourceADR = Replace(sourceADR, "Attica", "")
'...................................................................
sourceADR = Replace(sourceADR, "-", " ")
sourceADR = Replace(sourceADR, " ", " ")
sourceADR = Trim(sourceADR)
auxC = sourceADR
Worksheets("MAIN_CONTROL").Cells(i, 26).Value = sourceADR
'..............................
If (Len(sourceADR) < 1) Then GoTo aseAddr
'..............
mainAddress = Split(sourceADR)
addrAA = ""
Worksheets("MAIN_CONTROL").Cells(i, 24).Value = Str(UBound(mainAddress)) & "##" & Str(LBound(mainAddress))
For jA = UBound(mainAddress) To LBound(mainAddress) Step -1
'......................................................
If (regex.Test(Trim(mainAddress(jA)))) Then
auxC = Replace(auxC, Trim(mainAddress(jA)), "")
destws.Range("BT" & i).Value = Trim(mainAddress(jA))
destws.Range("AA" & i).Value = Trim(mainAddress(jA))
destws.Range("Z" & i).Value = addrAA
auxC = Trim(auxC)
destws.Range("Y" & i).Value = auxC
'--------------------------------------------------------
'-------------------------------------------------------
GoTo aseAddr
End If
auxC = Replace(auxC, Trim(mainAddress(jA)), "")
addrAA = mainAddress(jA) & " " & addrAA
'
'.....................................................
Next jA
'.........................................................
'destws.Range("Y" & i).Value = addrAA
'.....................
aseAddr:
'.................................
My problem is that these columns may change order.
I was suggested to use application.match so that my code may follow but i don't know how to put it inside my code.
Can anyone help?
Thanks in advance
Welcome to SO.
In general, the way you can use an excel function in VBA is the following:
application.WorksheetFunction.Match() 'where match() can be replaced by one of the available worksheet functions.
The function's arguments work pretty much the same way as they do when you use the formula in your worksheet.
The Match functionality is explained thoroughly here
So for example let's say you have an array like the following one in cells A1:A13
And you want to find the location of "Friday". You would do it like so:
Debug.Print Application.WorksheetFunction.Match("Friday", sht.Range("A1:A13"), 0)
And you would get 5 as a result.
That should pretty much cover the "How to use Application.Match()".
Related
I need to find away to be able to combine consecutive numbers. It could be either by formula or VBA code, but I honestly don't know where to start and I could not find anything similar posted that I could use for my case.
Ex:
This:
9630184784, 9630184786, 9630184787, 9630184788, 9630184814, 9630184815, 9630184816, 9630184817
To:
9630184784, 9630184786-9630184788, 9630184814-9630184817
Thanks in advance!
Hyra
=MID(CONCAT(IFERROR(CHOOSE(MMULT(COUNTIF(A:A,A$1:A$8+{1,-1}),{1;3}/2)+1,",","-")&A1:A8,"")),2,99)
#Excel 2019
=MID(CONCAT(IFERROR(CHOOSE(MMULT(COUNTIF(A1,"*"&FILTERXML("<a><b>"&SUBSTITUTE(A1,","," </b><b>")&"</b></a>","a/b")+{1,-1}&"*"),{1;3}/2)+1,",","-")&FILTERXML("<a><b>"&SUBSTITUTE(A1,","," </b><b>")&"</b></a>","a/b"),"")),2,99)
#Excel 365
=LET(x,FILTERXML("<a><b>"&SUBSTITUTE(A1,","," </b><b>")&"</b></a>","a/b"),MID(CONCAT(IFERROR(CHOOSE(MMULT(COUNTIF(A1,"*"&x+{1,-1}&"*"),{1;3}/2)+1,",","-")&x,"")),2,99))
Answer change from 象山海鲜.
This VBA function worked fine for me. It was posted by mikerickson at mrexcel.
https://www.mrexcel.com/board/threads/convert-list-of-sequential-numbers-into-ranges.978486/
Function IntoRanges(aString As String, Optional Delimiter As String = ",") As String
Dim NextBit As String
Dim i As Long
Dim Items As Variant
Items = Split(aString, Delimiter)
IntoRanges = Items(0)
For i = 0 To UBound(Items) - 1
If Val(Items(i)) + 1 = Val(Items(i + 1)) Then
NextBit = "-" & Val(Items(i + 1))
Else
If NextBit = vbNullString Then
IntoRanges = IntoRanges & Delimiter & Val(Items(i + 1))
Else
IntoRanges = IntoRanges & NextBit & Delimiter & Val(Items(i + 1))
NextBit = vbNullString
End If
End If
Next i
IntoRanges = IntoRanges & NextBit
End Function
I have a vba function that receives a row from a workbook and looks in come columns for data, setting variables or not depending on what is found. For rows 2-16, everything works fine; empty cells get skipped. Suddenly on row 17, a cell which seems empty triggers the first if-condition.
I've tried adding an additional check for cells that contain an empty string, and in the worksheet itself I checked CODE(H17) which was #VALUE
Function calcID(r As Long) As Variant
If (Not IsEmpty(allProps.Cells(r, 8))) Or (Not allProps.Cells(r, 8).Value = "") Then
MsgBox "Found ID: " & allProps.Cells(r, 8).Value & " in allProps row " & r
calcID = CDate(allProps.Cells(r, 8).Value)
ElseIf Not IsEmpty(allProps.Cells(r, 9)) Or Not allProps.Cells(r, 9).Value = "" Then
MsgBox "Found reverse ID: " & allProps.Cells(r, 9).Value & " in allProps row " & r
calcID = CDate(allProps.Cells(r, 9).Value)
Else
calcID = ""
End If
End Function
I use CDate elsewhere and it works fine. Ultimately the error shows up once I'm inside the if because I think CDate doesn't have a string to work with.
I think you only want to process the cells if they are both non-empty AND not equal to emptystring. This will prevent strange behavior if a cell is somehow not empty but is equal to emptystring.
Change your logical ORs to ANDs.
Function calcID(r As Long) As Variant
If (Not IsEmpty(allProps.Cells(r, 8))) And (Not allProps.Cells(r, 8).Value = "") Then
MsgBox "Found ID: " & allProps.Cells(r, 8).Value & " in allProps row " & r
calcID = CDate(allProps.Cells(r, 8).Value)
ElseIf Not IsEmpty(allProps.Cells(r, 9)) And Not allProps.Cells(r, 9).Value = "" Then
MsgBox "Found reverse ID: " & allProps.Cells(r, 9).Value & " in allProps row " & r
calcID = CDate(allProps.Cells(r, 9).Value)
Else
calcID = ""
End If
End Function
Some of the value in the cell which starts with = is taken as formula in excel.
For eg : =test is taken as a formula with error like this in excel.
I do had a similar issue and sorted it out by comparing the value in the cell and trimming off unwanted = in cell value.
Please make sure you are not getting any value in that cell which makes excel think that it is a formula or cell reference.
I want replace the x input from the user to the Cell Address value that is stored in N2. For example Cells(2, i+10) is J2 Cell, in the first loop in cell A2 it will be =J2^2 if the user wrote x^2 and the next loop would be B2=K^3 if the user wrote x^3 and so on.
numero_formulas = InputBox("¿Cuántas fórmulas vas a ingresar?")
Sheets("ResultadosContinua").range("I6") = numero_formulas
For i = 1 To numero_formulas
N2 = Cells(2, i + 10)
formula_user = InputBox("Escribe la fórmula:" & i & "")
Cells(2, i).Select
Sheets("ResultadosContinua").Select
ActiveCell.Formula = "=" & Replace(formula_user, "x", " & N2 & ")
If you want the address of the N2, (4 columns away from the activecell) you need you use this:
numero_formulas = InputBox("¿Cuántas fórmulas vas a ingresar?")
Sheets("ResultadosContinua").Range("I6") = numero_formulas
For i = 1 To numero_formulas
n2 = Cells(2, i + 10)
formula_user = InputBox("Escribe la fórmula:" & i & "")
Cells(2, i).Select
Sheets("ResultadosContinua").Select
ActiveCell.Formula = "=" & Replace(formula_user, "x", ActiveCell.Offset(0, 4).Address(False, False))
But as I said, you code will delete the formula inside the cel N2, after the 4th iteration.
I'd suggest reading up on how to avoid using .Select as per this question. I would also encourage using the full name of the object you're targeting. ie Cells(2,10) = "Text" will work but it's always better to write Cells(2,10).Value = "Text". Your code could then be modified to read:
Dim rngNumeroRange as Range
Dim intNumeroFormulas as Integer
Dim rngN2 as Range
Set rngNumeroRange = Sheets("ResultadosContinua").Range("I6")
intNumeroFormulas = InputBox("¿Cuántas fórmulas vas a ingresar?")
rngNumeroRange.Value = intNumeroFormulas
For i = 1 To intNumeroFormulas
set rngN2 = Sheets("ResultadosContinua").Cells(2, i)
formula_user = InputBox("Escribe la fórmula:" & i & "")
rngN2.Formula = "=" & Replace(formula_user, "x", rngN2.Offset(0, 9).Address(False, False))
Next
In sheet 1 the value is 45 so I want in sheet RESULTADO the format like this: 0045
Worksheets("RESULTADO").Range("B" & row_counter) = Format(Worksheets(1).Range("B" & row_counter).Value, "0000")
Why this doesn't work?
I've tried also this, but neither it works:
Worksheets("RESULTADO").Range("B" & row_counter) = CStr(Format(Worksheets(1).Range("B" & row_counter).Value, "0000"))
You can do that in two(2) ways:
Set the number format first
With Worksheets("RESULTADO").Range("B" & row_counter)
.NumberFormat = "#"
.Value = Format(Worksheets(1).Range("B" & row_counter).Value, "0000")
End With
Use ' apostrophe
Worksheets("RESULTADO").Range("B" & row_counter) = "'" & _
Format(Worksheets(1).Range("B" & row_counter).Value, "0000")
this doesn't work because in the first case you're trying to format a number n to 00n wich always return n, in the second case, you do exactly the same then you transtype the result to a string.
You have to convert n to a string first. So in your case :
Worksheets("RESULTADO").Range("B" & row_counter) = Format(CStr(Worksheets(1).Range("B" & row_counter).Value), "0000")
This is what I have, so the H needs to be followed by the number of the cell, I want to use the counter i here, but it doesn't work. What am I doing wrong? :)
For i = 60 To 63
Range("Hi").AddComment
Range("Hi").Comment.Visible = False
Range("Hi").Comment.Text Text:=""
i = i + 1
Range("Hi").Select
i = i - 1
Next
End Sub
Use this:
Range("H" & i)
As you wrote it, "Hi" does not use the variable i because you put it in quotes.
You need to do this instead:
For i = 60 To 63
Range("H" & i).AddComment
Range("H" & i).Comment.Visible = False
Range("H" & i).Comment.Text Text:=""
i = i + 1
Range("H" & i).Select
i = i - 1
Next
End Sub
The & operator does concatenation in VBA.
You should format it like this:
For i = 60 To 63
Range("H" & i).AddComment
Range("H" & i).Comment.Visible = False
Range("H" & i).Comment.Text Text:=""
i = i + 1
Range("H" & i).Select
i = i - 1
Next i
End Sub
The reason is that the letter H is a character and i is a variable. anything inside of double quotes "Hi" like that Excel will read as just a string of text.
When Excel reads a word or letter outside of quotes i it will assume it is a variable. The & character joins the two together as text.
This means that each time the loop runs Excel will read it as "H" and i and translate it to "H1", "H2", "H3", .... "H60" and input it into the Range() like you are looking for.
Would this work better for you? I've always had issues when the value of i starts chaging inside the loop code
For i = 60 To 63
With Range("H" & i)
.Select
.AddComment
.Comment.Visible = False
.Comment.Text Text:=""
end with
Next
End Sub