Parse data from one CSV file to another - csv

I'm trying to run this Macro but since my CSV file is really long and I don't want to count the column numbers, is there a way to reference the column by the letters such as "DZ"? The code works fine but I just need to reference these deep columns and don't want to count which column number it is and can't seem to find a way to display the column number. I have been setting 'c' & 't' as the column number such as 1, 2, etc.
Sub ParseColumn
Dim sheet1 as Object
Dim sheet2 as Object
Dim r as long
Dim c as long
sheet1 = ThisComponent.Sheets.getbyname("Sheet1")
sheet2 = ThisComponent.Sheets.getbyname("Sheet2")
c = F
t = DZ
for r = 1 to 18739
copyFrom = sheet1.getCellByPosition(c,r)
copyTo = sheet2.getCellByPosition(t,r)
copyTo.String = copyFrom.String
next r
End Sub
Update
Found a solution:
Sub ParseColumn
Dim sheet1 as Object
Dim sheet2 as Object
Dim r as long
sheet1 = ThisComponent.Sheets.getbyname("Sheet1")
sheet2 = ThisComponent.Sheets.getbyname("Sheet2")
for r = 2 to 18739
copyFrom = sheet1.getCellRangeByName("AR" & r)
copyTo = sheet2.getCellRangeByName("S" & r)
copyTo.String = copyFrom.String
next r
End Sub

Related

Matching values in one column with comma separated values in another column and returning the matched value

I have one column with values like
himaanshu
akshay
rahul
hgeet
And another column with values like
axs,fdvf,dasad
axs,fdvf,dasad, himaanshu
axs,fdvf,dasad, akshay
asz,wesd,hgeet
I need to return the matching name for every row in Column 2 from whole list of Column 1
Solution Should be:
1. None
2. himaanshu
3. akshay
4. hgeet
Can anyone help me with the formula that I can use in spreadsheet to solve this.
Try the below:
Sub test()
Dim str1 As String
Dim rngToSearch As Range, cell As Range
Dim LastRowA As Long, LastrowC As Long, i As Long, y As Long
Dim arr As Variant
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
LastrowC = .Cells(.Rows.Count, "C").End(xlUp).Row
Set rngToSearch = .Range("C2:C" & LastrowC)
For i = 2 To LastRowA
str1 = .Range("A" & i).Value
For Each cell In rngToSearch
arr = Split(cell.Value, ",")
For y = LBound(arr) To UBound(arr)
If Trim(arr(y)) = Trim(str1) Then
.Range("B" & i).Value = str1
End If
Next y
Next cell
Next i
End With
End Sub
Results:
See if this formula works (in a google spreadsheet)
=ArrayFormula(iferror(REGEXEXTRACT(C2:C5, textjoin("|", 1, A2:A5)), "none"))
The formula extracts any of the values in column A from the values in column C
[
=VLOOKUP("*"&A1&"*", B1:B4,1,0)

Input String was not in Correct Format on Web App using HTML and VB.NET

I inherited this code at my job and I am now having some issues. When I run this bit of code I keep getting errors on this line of code
p = CInt(v.Substring(1, c - 2))
I have 2 check boxes in a table The first one votes for a person and the second checkbox votes for the person whose name you type in to the text box beside the checkbox. Sometimes it works and sometimes it doesn't.
It is also not liking when a capital "C" is included in the textbox.
Public Function CreateVotingDataTable(votes As String) As DataTable
Dim dt As New DataTable
Dim c1 As New DataColumn("position", GetType(Integer))
Dim c2 As New DataColumn("sequence", GetType(Integer))
Dim c3 As New DataColumn("wivalue", GetType(String))
dt.Columns.Add(c1)
dt.Columns.Add(c2)
dt.Columns.Add(c3)
Dim votearray = votes.Split("|")
Dim p As Integer = 0
Dim s As Integer = 0
Dim writein As String = String.Empty
If votes.Length <> 0 Then
For Each v As String In votearray
writein = String.Empty
Dim w As Integer = InStr(v, "#")
Dim c As Integer = InStr(v, "C")
If c = 0 Then
c = InStr(v, "W")
End If
p = CInt(v.Substring(1, c - 2))
If w > 0 Then
s = CInt(v.Substring(c, w - c - 1))
writein = v.Substring(w, Len(v) - w)
Else
s = CInt(v.Substring(c, Len(v) - c))
End If
Dim r As DataRow = dt.NewRow
r("position") = p
r("sequence") = s
r("wivalue") = writein
dt.Rows.Add(r)
Next
End If
Return dt
End Function
The value that is typed into the textbox should be carried over to the next page by the datatable. Which works sometimes but when you go back to change the value to the other checkbox it gives me the input string is not in a correct format or argument out of range exception:
length cannot be less than zero.
Anybody have any ideas?

Populating access table from form multi-record textbox

I am trying to use this code to pick comma seperated numbers from ExcUID text box of form and then feed them into tblExcIndivList table.
However what I am trying to do it to split ex: 123,1213 into lines and put them in seperate rows of UID column of tblExcIndivList table but it gets saved as 1231213 in the same cell.
Sub Upd_UID()
Dim var As Variant
Dim i As Long
var = Split(Forms.Agen_Report.ExcUID.Value, vbNewLine)
CurrentDb.Execute "DELETE * FROM tblExcIndivList;", dbFailOnError
For i = 0 To UBound(var)
CurrentDb.Execute Replace("INSERT INTO tblExcIndivList ( UID ) VALUES ( '#V' );", "#V", var(i)), dbFailOnError
Next i
End Sub
Please help.
You are not splitting correctly your string, you say it is comma-separated (i.e. 123,1213) and try to split it with vbNewLine. You should specify the comma as separator:
var = Split(Forms.Agen_Report.ExcUID.Value, ",")
This will get you past this error and split correctly the input. However I cant make sure whether your query is well-formed.
I think you need something like this.
Option Explicit
Dim aCell As Range
Private Sub UserForm_Initialize()
'~~> Change Sheet1 to the relevant sheet name
'~~> Change A1:E1 to the relevant range
For Each aCell In ThisWorkbook.Sheets("Sheet1").Range("A1:E1")
If InStr(1, aCell.Value, ",") Then _
ComboBox1.AddItem Split(aCell.Value, ",")(0)
Next aCell
'~~> Remove duplicates
RemoveDuplicates ComboBox1
End Sub
Private Sub ComboBox1_Click()
Dim tmpStr As String
ComboBox2.Clear
For Each aCell In ThisWorkbook.Sheets("Sheet1").Range("A1:E1")
If InStr(1, aCell.Value, ",") Then _
tmpStr = Split(aCell.Value, ",")(0)
If Trim(ComboBox1.Value) = Trim(tmpStr) Then _
ComboBox2.AddItem aCell.Value
Next aCell
End Sub
'~~> Procedure to remove duplicates
Private Sub RemoveDuplicates(cmb As ComboBox)
Dim a As Integer, b As Integer, c As Integer
a = cmb.ListCount - 1
Do While a >= 0
For b = a - 1 To 0 Step -1
If cmb.List(b) = cmb.List(a) Then
cmb.RemoveItem b
a = a - 1
End If
Next b
a = a - 1
Loop
End Sub

Concatenate alternating fixed column headers and row values

I have a spreadsheet with fixed column headers and variable row data, I would like to create a simple tool (lets aim for 1 click) that will concatenate the column values and row data entered by the user into an attribute string (with '=' after each header and ';' after each value).
Before looks like this:
After looks like this:
The output is required in a separate worksheet and if possible saved as a value.
The number of columns could vary so a solution that uses a defined table would be useful.
Formula used:
=CONCATENATE(before!$A$1,"=",before!$A2,";",before!$B$1,"=",before!$B2,";",before!$C$1,"=",before!$C2,";")
Any assistance would be greatly appreciated.
The following UDF will do it:
Function unionText(ttl As Range, rng As Range) As String
Dim i As Long
If ttl.Cells.Count <> rng.Cells.Count Or _
ttl.Rows.Count <> 1 Or rng.Rows.Count <> 1 Then
unionText = CVErr(xlErrValue)
Exit Function
End If
For i = 1 To ttl.Cells.Count
unionText = unionText & ttl(i) & "=" & rng(i) & ";"
Next i
End Function
It is called in the sheet like this:
=unionText(before!$A$1:$C$1,before!A2:C2)
Then copied down
Mine is obviously on the same sheet but the formula above uses your sheet reference.
You can use this macro code temporary and assuming there's only one row of values :
Sub Macro1()
' Macro1 Macro
Range("A1").Select
Dim r As Byte
Dim c as Byte
Dim stringunion As String
r = 1
c = 1
Do While Cells(r, c) <> ""
stringunion = stringunion & Cells(r, c).Value & "=" & Cells(r + 1, c) & ";"
c = c + 1
Loop
MsgBox stringunion
End Sub

MS Excel VBA script

I haven't tried coding except in javascript in the past, however i'm pretty new.
I'm trying to create a macro for excel that will compare a the values in sheet1 in column B individually to corresponding column B in sheet2 to find a match. If no match is found the entire row is added to the bottom of the spreadsheet. any help on where to start would be appreciated.
I have 6 columns in the list
The key to what you are attempting is in understanding the nested Loops.
Begin by looping through Sheet 1
Set a temp Value for each row
Begin loop of Sheet 2, each row comparing the temp Value.
Use a boolean variable to track if there was a match or not
At the end of the loop of Sheet 2, If there was no match
Copy the row, by looping through the columns
Continue looping through Sheet 1
Code:
Sub CopyNoMatch()
Dim lastSourceRow As Long
Dim source As String, target As String
Dim tempVal As String
Dim tRow As Long, lRow As Long, lCol As Long, nRow As Long
Dim match As Boolean
source = "Sheet1"
target = "Sheet2"
lastSourceRow = Sheets(source).Range("A" & Rows.count).End(xlUp).row
For lRow = 2 To lastSourceRow 'Loop through Rows on Sheet1
match = False 'Reset boolean test for each new row
tempVal = Sheets(source).Cells(lRow, "B").Text 'Assign the tempValue to compare
For tRow = 2 To lastTargetRow 'Loop through entire target sheet
If Sheets(target).Cells(tRow, "B").Text = tempVal Then
match = True
End If
Next tRow
If match = False Then 'No Match found, copy row
nRow = Sheets(target).Range("A" & Rows.count).End(xlUp).row + 1
For lCol = 1 To 6 'Copy entire row by looping through 6 columns
Sheets(target).Cells(nRow, lCol).Value = Sheets(source).Cells(lRow, lCol).Value
Next lCol
Next lRow
End Sub