Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How can I subtract a comma seperated string from another one in Excel?
If A1: 1,2,3,4,5,6,7,8,9
And A2: 2,6,9
My desired result should be in cell A3, after subtraction (A1-A2): 1,3,4,5,7,8
It was very easy when A2=2,3,4 (serially) via SUBSTITUTE function, however I can't find a solution to the above.
You have both tagged formula and VBA. So let me give you two options too:
1) Formula
=TEXTJOIN(",",1,FILTERXML("<t><s>"&SUBSTITUTE(A1&","&A2,",","</s><s>")&"</s></t>","//s[not(following::*=. or preceding::*=.)]"))
Note1: This is an array formula and needs to be confirmed through CtrlShiftEnter
Note2: This requires access to the TEXTJOIN function, available in office 365 and Excel 2019.
Note3: More usefull FILTERXML "tricks" can be found here
2) UDF
I'd recommend using Split and Join functions to do this:
Function TxtFilter(val1 As String, val2 As String, sep As String) As String
Dim arr1 As Variant, arr2 As Variant
Dim x As Long
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
arr1 = Split(val1, sep)
arr2 = Split(val2, sep)
'Iterate first array to load dictionary
For x = LBound(arr1) To UBound(arr1)
dict(arr1(x)) = 1
Next x
'Iterate second array to remove from dictionary
For x = LBound(arr2) To UBound(arr2)
If dict.Exists(arr2(x)) Then dict.Remove arr2(x)
Next x
'Join remainder back together
TxtFilter = Join(dict.keys, sep)
End Function
Call in any cell through =TxtFilter(A1,A2,",")
Related
I'm looking to learn how to create a function that's capable of dividing lists in excel. In python it can be done as shown.
https://www.geeksforgeeks.org/python-dividing-two-lists/
The user entry is in the form of a cell of numbers separated by commas
i.e.:
cell a2: 12,86,71,54
cell b2: 1,3,2,1
This needs to be a function as it will require user input that will be dynamic. It can't be done with the text to columns function. For example, I have a user enter a list such as 12,86,71,54 which needs to be divided by another user entry of another comma separated list 1,3,2,1 (12/1 + 86/3 + 71/2 + 54/1)
Thanks in advance!
Give this a try:
Option Explicit
Public Function DivList(s1 As String, s2 As String) As String
Dim arr1, arr2, arr3, i As Long
arr1 = Split(s1, ",")
arr2 = Split(s2, ",")
ReDim arr3(LBound(arr1) To UBound(arr1))
For i = LBound(arr1) To UBound(arr1)
arr3(i) = arr1(i) / arr2(i)
Next i
DivList = Join(arr3, ",")
End Function
An example:
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is there a way to create a text file and copy the content of, lets say, column J to column L inside the newly created text file? I know you can write some text inside the text file by writing the text directly inside the code but is there a methode or a fonction that copy content of a column?
Here is a code to manually create a CSV text file. You can modify this to suit your needs
Option Explicit
Public Sub ExportToCSV(ByVal rngStart As Range, ByVal fname As String, Optional n_rows As Long = 0, Optional n_columns As Long = 0)
Dim i As Long, j As Long
If n_rows = 0 Then
n_rows = rngStart.Worksheet.Range(rngStart, rngStart.End(xlDown)).Rows.Count
End If
If n_columns = 0 Then
n_columns = rngStart.Worksheet.Range(rngStart, rngStart.End(xlToRight)).Columns.Count
End If
Dim fso As New FileSystemObject
Dim fs As TextStream
Set fs = fso.CreateTextFile(fname)
Dim line As String, vals() As Variant
vals = rngStart.Resize(n_rows, n_columns).Value
For i = 1 To n_rows
line = CStr(vals(i, 1))
For j = 2 To n_columns
line = line & "," & CStr(vals(i, j))
Next j
fs.WriteLine line
Next i
fs.Close
End Sub
Make sure you have Microsoft Scripting Runtime added as a reference which gives you access to FileSystemObject.
You call the code with
ExportToCSV Range("G1"), "Export.txt", 0, 3
if either the number of rows or columns is zero, then the code will find the end of the data for the export. In the example above, all used rows, but three columns are going to be exported in to the file.
PS. To change to tab-delimited code, like what you would get from copying into the windows clipboard, change the "," with vbTab. That's it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In excel VBA how do I display the following numbers as they appear without generating the 'Number stored as text' error in the cell?
1.0
1.1
1.2
.
.
1.99
1.100
1.101
1.102
.
.
1.20
1.21
1.22
.
.
etc...
Just keep track of the number of decimal places that need to be displayed. For example:
Sub marine()
Dim s As String, i As Long, a
s = "1.0,1.1,1.2,1.99,1.100,1.101,1.102,1.20,1.21,1.22"
arr = Split(s, ",")
i = 1
For Each a In arr
With Cells(i, 1)
.Value = a
brr = Split(a, ".")
.NumberFormat = "0." & Application.Rept("0", Len(brr(1)))
End With
i = i + 1
Next a
End Sub
This is based on the curious fact that if VBA puts a number-like string into a cell, Excel will convert it into a number:
Sub demo()
Dim s As String
s = "1.230"
Range("A1").Value = s
End Sub
EDIT#1:
On the other hand, if you want to enter text that look like numbers, but avoid raising the error flag, then:
Sub Macro1()
Application.ErrorCheckingOptions.NumberAsText = False
End Sub
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I have the table below. Basically column A is the master data.
Column B, C and D will contain the some data in column A. Column B, C and D can have common data.
I want to compare data column A to column B,C and D.
In column Result, display all unmatch data.
Give this a try:
Sub MissingItems()
Dim N As Long, I As Long, J As Long, V As String
Dim r As Range
N = Cells(Rows.Count, 1).End(xlUp).Row
J = 1
For I = 1 To N
V = Cells(I, 1).Value
Set r = Nothing
Set r = Range("B:D").Find(V, After:=Range("B1"))
If r Is Nothing Then
Cells(J, 5).Value = V
J = J + 1
End If
Next I
End Sub
Well you can use this "big" array formula:
=INDEX($A$1:$A$10;SMALL(IF(NOT(COUNTIF($B$1:$B$2;$A$1:$A$10)+COUNTIF($C$1:$C$3;$A$1:$A$10)+COUNTIF($D$1:$D$2;$A$1:$A$10));ROW($A$1:$A$10)-ROW($A$1)+1;"");ROW(A1)))
Write the formula wherever you want to obtain the first an then copy down to obtain the others.
This is an array formula so dont forget to Ctrl Shift Enter
Depending on your Regional Settings you may need to replace field separator ";" by ","
This one is smaller and still works:
=INDEX($A$1:$A$10;SMALL(IF(NOT(COUNTIF($B$1:$D$3;$A$1:$A$10));ROW($A$1:$A$10)-ROW($A$1)+1;"");ROW(A1)))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Could anyone give me advice on how to look up a row in a spreadsheet which matches two criteria, then change the data in specific Cells.
The data will be like this..
Reference Version date1 date2 date3
ABC1 1 11/12/2013
ABC1 2 31/12/2013
ABC2 1 12/12/2013
ABC3 1 12/12/2013
ABC1 3 01/01/2014
In VBA I wish to be able to find the row that matches the reference and the version number, then datestamp column 4 of that 1 unique row.
Any help would be really appreciated.
Many Thanks
Solving for a position for two criteria can be solved by this array formula (in this sample looking up ABC3 and 1 in A2:B6
This formula can be used in VBA to provide the position for the timestamp:
VBA Equivalent
Sub OneWay()
Dim lngRow As Long
lngRow = Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")
Cells(lngRow + 1, 4) = Now()
End Sub
or just:
Sub OneWay2()
lngRow = Cells(Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")+ 1, 4) = Now()
End Sub
Try this code:
Sub test()
Dim refToFind As String
Dim versToFind As String
refToFind = "ABC1"
versToFind = "1"
'address of first reference
Set Rng = Sheet1.Range("B2")
lastrow = Sheet1.Range("B" & Sheet1.Rows.Count).End(xlUp).Row - Rng.Row
For i = 0 To lastrow
If CStr(Rng.Offset(i, 0).Value) = refToFind _
And CStr(Rng.Offset(i, 1).Value) = versToFind Then
Rng.Offset(i, 4) = Now
End If
Next i
End Sub