How to Split and Copy its values - excel

I am a VBA beginner, and am stunned with splitting and copying.
I have a workbook (book1) which contains some student's name in column A and Pay school fee Date in Column B. Some names have more than one date, while some have only one:
A...............B.
Kaka.........10/07/2018
Thy............12/05/2018, 15/08/2018, 15/08/2018
Then I want to search their name in another workbook and copy its entire row to new workbook. All help is welcome; thanks in advance.

You can perform operation this way although You had not show Your code but I have written my own code as my understand about code.
Sub transferData()
'Declare an array
Dim LArray() As String
'Start For loop
For i = 1 To Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A" & i) = Sheets("Sheet1").Range("A" & i).Value
'Check Condition for multiple date exits or not
If (UBound(Split(Sheets("Sheet1").Range("B" & i).Value, ",")) > 0) Then
LArray = Split(Sheets("Sheet1").Range("B" & i).Value, ",")
'Split Date and store inside Array
For j = 0 To UBound(LArray)
Sheets("Sheet2").Cells(i, 2 + j) = LArray(j)
Next j
Else
Sheets("Sheet2").Range("B" & i) = Sheets("Sheet1").Range("B" & i).Value
End If
Next i
End Sub
If You find it helpful then pleasure for me and vote my answer or in case any doubt feel free to ask me.
thanks

Related

excel vba cycle through rows and output to new sheet

I have a DATA sheet which contains data rows as follows:
And I have a sheet named ROWBUILDER that has formulas and produces results like this:
I would like to write a VBA code that will cycle through every row in the ROWBUILDER sheet and output data to a new worksheet.
NOTE: The ROWBUILDER sheet must remain as is. Only the resulting data must be copied to the OUTPUT sheet.
I have no idea how to do it and from where to start. Will appreciate any help, examples or links.
Many thanks in advance!
This moves all the data to one output sheet.
Sub MoveROWBUILDER()
Sheets("ROWBUILDER").Range("A1").CurrentRegion.Copy
Sheets("Output").Range("A1").PasteSpecial xlPasteValues
End Sub
If you are looking for one sheet per row, try this. You may run into a limit depending on memory and the amount of data.
Sub DataToSheets()
Dim sh As Worksheet
Dim rowCount As Integer, colCount As Integer
Dim i As Integer, j As Integer
Dim data() As Variant
Sheets("ROWBUILDER").Select
rowCount = Range("A1").CurrentRegion.Rows.Count
colCount = Range("A1").CurrentRegion.Columns.Count
data() = Sheets("ROWBUILDER").Range("A1").CurrentRegion.Value2
For i = 2 To rowCount
Set sh = Sheets.Add(After:=ActiveSheet)
sh.Name = "Data" & (i - 1)
For j = 1 To colCount
sh.Cells(1, j) = data(1, j)
sh.Cells(2, j) = data(i, j)
Next j
Next i
End Sub
If you can estimate the maximum number of rows in your data sheet then - instead of cycling through every row - you can use
rowbuilder.Range("A1:B" & lastrow).Copy
newworksheet.Range("A1").PasteSpecial xlValues
Let me try this again now that I have a better idea of what you need.
You can process all of the rows at once using dynamics arrays in ROWBUILDER. Set C1 in ROWBUILDER to
=COUNTA(DATA!A:A) - 1
Then set A2 to
=OFFSET(DATA!A2,0,0,C1,1) & " " & OFFSET(DATA!B2,0,0,C1,1)
This will spill all of the full names to column A. You can then set B2 to
=OFFSET(DATA!H2,0,0,C1,1) & " " & OFFSET(DATA!G2,0,0,C1,1) & " " & OFFSET(DATA!F2,0,0,C1,1) & OFFSET(DATA!E2,0,0,C1,1)
This will spill the Full Address to Column B. IF you still need to copy it to OUTPUT, then this code should do the job.
Range("A2#").Copy
offsetrows = Sheets("OUTPUT").Range("A1").CurrentRegion.Rows.Count
Sheets("OUTPUT").Select
Range("A1").Offset(offsetrows, 0).Select
Selection.PasteSpecial xlPasteValues
You can use loop like this:
numberRows = Range("A2").End(xlToDown).Row
Sheets.Add.Name = "OUTPUT"
For i = 2 To numberRows
Sheets("OUTPUT").Cells(i, 1).Value = Sheets("DATA").Cells(i, 1).Value
Sheets("OUTPUT").Cells(i, 2).Value = Sheets("DATA").Cells(i, 8).Value & " " & Sheets("DATA").Cells(i, 7).Value & " " & Sheets("DATA").Cells(i, 6).Value & " " & Sheets("DATA").Cells(i, 5).Value
Next i

Comparing values from 2 sheets

compare values from 1 column in a sheet to another column in a different sheet. If the value is the same, update the cell string to “Special”
I have a code that checks for type of each case I have, so when macros identifies that type it is the string in the cell changes for a specific name.
However, I need to create a new type named Special, when the value on alert_list.Range("E") is NOT "Multi" and the value on alert_list.Range("C") is "Corp". Then the value on column F should be compared to the value on column B in a sheet named child_list to see if it is the same value and if it is the same then a string Special need appear in the cell on the place of "Corp" on alert_list.Range("C").
I create a list on column B in a sheet named child_list and I need to compare this values with the information in another sheet named alert_list column F.
everything above is working I am trying to create another step to be able to have a new type named Special.
If ABC.Range("E" & i).Value <> "Multi" And ABC.Range("C" & i).Value = "Corp" Then
For k = 2 To Ch_lRow
If ABC.Range("F" & i).Value <> TEXT.Range("B" & k).Value Then
'do nothing
Else
If alert_list.Range("F" & i).Value = TEXT.Range("B" & k).Value Then
ABC.Range("C" & i).Value = "GOOD"
End If
Next
End If
Exit Sub
But for some reason unknown for me it is not working, I am not getting error However, or it jumps directly to exit sub or just nothing happen after run the code.
It's a bit tricky to explain in comments so here is an answer to explain.
You are executing an Exit Sub statement after your first If...Then...Else block.
If we use some proper indentation, it's much easier to identify this:
Dim Ch_lRow As Long
Ch_lRow = child_list.Cells(Rows.Count, 2).End(xlUp).Row
If alert_list.Range("E" & i).Value <> "Multi" And alert_list.Range("C" & i).Value = "Corp" Then
For k = 2 To Ch_lRow
If alert_list.Range("F" & i).Value <> child_list.Range("B" & k).Value Then
'do nothing
Else
If alert_list.Range("F" & i).Value = child_list.Range("B" & k).Value Then
alert_list.Range("C" & i).Value = "Special"
End If
Next
End If
Exit Sub
This additional block you want to add in is obviously within your first loop:
For i = 2 To lRow
...
Dim Ch_lRow As Long
Ch_lRow = child_list.Cells(Rows.Count, 2).End(xlUp).Row
If alert_list.Range("E" & i).Value <> "Multi" And alert_list.Range("C" & i).Value = "Corp" Then
For k = 2 To Ch_lRow
If alert_list.Range("F" & i).Value <> child_list.Range("B" & k).Value Then
'do nothing
Else
If alert_list.Range("F" & i).Value = child_list.Range("B" & k).Value Then
alert_list.Range("C" & i).Value = "Special"
End If
Next
End If
Exit Sub
Next i
So this means you have an Exit Sub within your loop, so, within the first iteration the code will fire Exit Sub and your code will do nothing as you put it.
Remove the exit sub from this location and put it outside your For i = 2 to lRow loop as required.

How to copy cell contents from multiple rows into one cell if 3 column values are the same within a worksheet?

I am trying to clean an excel dataset provided to me using VBA in the most efficient way possible. I want to compare row values (# may vary) of 3 columns within a worksheet range, if the row values are the same for all 3 columns, then i want the values of the same rows but different columns copied into one cell.
Sample Set: red should be copied into one cell:
Expectation with black removed and red in one cell
Ultimate Want
Before/ After Expectation
In the future, SO questions should be about specific issues you are having, not a general question.
Here is a VBA function that will:
Go through each cell, until it finds an empty cell. We will assume once an empty cell is found we are at the end of your data set.
Check if any of the first three columns have changed their data from the previous cell. If they have, this is our new 'working row'. The row where we will consolidate your dataset into.
For each row, add its value from the data set column to the 'working row', unless it already exists in that row.
Once finished, go back and delete empty cells.
Here's the subroutine:
Sub clean_dataset()
Dim sh As Worksheet
Dim rw As Range
Dim workingRow As Integer
Dim col1Value As String
Dim col2Value As String
Dim col3Value As String
Dim rowCount As Integer
workingRow = 1
'Iterate through all rows on the sheet. Stop if we get to an empty row.
Set sh = ActiveSheet
For Each rw In sh.Rows
' Exit if we get to an emptry row.
If sh.Cells(rw.Row, 1).Value = "" Then
Exit For
End If
' Check if our three columns to watch have changed value. If they have, we should be in a new 'working row'
If rw.Row > 1 Then
If (Range("A" & rw.Row).Value <> Range("A" & rw.Row - 1).Value) Or (Range("B" & rw.Row).Value <> Range("B" & rw.Row - 1).Value) Or (Range("C" & rw.Row).Value <> Range("C" & rw.Row - 1).Value) Then
workingRow = rw.Row
End If
End If
' Get the values in the current row from the dataset we are trying to consolidate.
col1Value = Range("D" & rw.Row).Value
col2Value = Range("E" & rw.Row).Value
col3Value = Range("F" & rw.Row).Value
' Add the values to the working row cells, if they do not already exist
If InStr(Range("D" & workingRow).Value, col1Value) = 0 Then
Range("D" & workingRow) = Range("D" & workingRow).Value & vbLf & col1Value
End If
If InStr(Range("E" & workingRow).Value, col2Value) = 0 Then
Range("E" & workingRow) = Range("E" & workingRow).Value & vbLf & col2Value
End If
If InStr(Range("F" & workingRow).Value, col3Value) = 0 Then
Range("F" & workingRow) = Range("F" & workingRow).Value & vbLf & col3Value
End If
' As long as we are not in the working row, delete the values
If rw.Row <> workingRow Then
Range("D" & rw.Row) = vbNullString
Range("E" & rw.Row) = vbNullString
Range("F" & rw.Row) = vbNullString
End If
rowCount = rw.Row
Next rw
' End of for each
' Go back through, and delete any rows that do not have values in column D
For iter = rowCount To 1 Step -1
' If all three columns are blank, delete the row.
If Range("D" & iter).Value = vbNullString Then
sh.Rows(iter).Delete
End If
Next
End Sub
Hope this helps.
After quite a bit of searching I was able to finally find this very applicable post as my issue is similar to the OP.
I'm working with three sheets where Sheet 1 is the source, Sheet 2 is a check sheet and Sheet 3 is where I would be pasting/cleaning up my data from Sheet 2.
In Sheet1, I copy the value in Col C and filter it in Sheet2 - Col C and for each company in Col J, I need to check the volume in Col K and if volume is >/= 1, the row needs to be copy/pasted into Sheet 3 while consolidating the corresponding unique values in each cell of the row and removing duplicates and summing the values in col K. The third sheet is the expected sheet. Thanks for your help if possible.

How can I test for Three Conditions, and then delete an entire row if all conditions are TRUE?

I have a sheet that I want to check the language in Column R for <> ‘Cash’; if ‘Cash’ do skip to the next row. If Column R <> ‘Cash’, check Column A for duplicate ID (there may or may not be duplicates). If duplicates are found, I want to check Column K for positive/negative values, like 100000 & -100000, then delete the entire row where the negative value appears in Column K. How can I do that?
Following the roles described above, row 6 would be deleted in the image below.
I could use VBA in Excel, or SQL/VBA in Access.
This seems to work pretty well.
Sub TryMe()
Dim i As Long
Dim j As Long
Dim ws As Worksheet
Set ws = Sheet1
For i = 2 To 13
For j = 2 To 13
If ws.Range("A" & i).Value = ws.Range("A" & j).Value Then
If ws.Range("A" & i).Offset(0, 10).Value = -(ws.Range("A" & j).Offset(0, 10).Value) Then
If ws.Range("A" & i).Offset(0, 17).Value <> "Cash" Then
Rows(i).EntireRow.Delete
End If
End If
Exit For
End If
Next j
Next i
End Sub

If function to write to either sheet1 or both sheet1 and sheet2

I'm trying to write an if function into a save button on a user-form so that if the data entered into the user-form is already on sheet 2 then it only gets written to sheet 1. But if it does not exist on sheet 2 then the data from the user-form gets written to both sheet 1 and sheet 2. This is because I want the data on sheet 2 to act like a sort of database and obviously do not want duplicates. I've made the write procedures into two separate modules (I figured this would make it easier to differentiate). Here is my code (Be gentle I'm still learning)
Sub writetosheet1()
Dim i As Integer
i = 1
While ThisWorkbook.Worksheets("Sheet1").Range("A" & i).Value <> ""
i = i + 1
Wend
ThisWorkbook.Worksheets("Sheet1").Range("a" & i).Value = UserForm1.txt1.Value
ThisWorkbook.Worksheets("Sheet1").Range("b" & i).Value = UserForm1.txt2.Value
ThisWorkbook.Worksheets("Sheet1").Range("c" & i).Value = UserForm1.txt3.Value
ThisWorkbook.Worksheets("Sheet1").Range("d" & i).Value = UserForm1.txt4.Value
ThisWorkbook.Worksheets("Sheet1").Range("e" & i).Value = UserForm1.txt5.Value
End Sub
Sub writetosheet2()
Dim i As Integer
i = 1
While ThisWorkbook.Worksheets("Sheet1").Range("A" & i).Value <> ""
i = i + 1
Wend
ThisWorkbook.Worksheets("Sheet2").Range("a" & i).Value = UserForm1.txt1.Value
ThisWorkbook.Worksheets("Sheet2").Range("b" & i).Value = UserForm1.txt2.Value
ThisWorkbook.Worksheets("Sheet2").Range("c" & i).Value = UserForm1.txt4.Value
ThisWorkbook.Worksheets("Sheet2").Range("d" & i).Value = UserForm1.txt5.Value
End Sub
Private Sub CMDSAVE_Click()
Dim id As Long
id = txt1.Value
If id <> Sheets("Sheet2").Range("a:a").Value Then
Call writetosheet1
Call writetosheet2
Else
Call writetosheet1
End If
End Sub
Any help on this would be fantastic! Thanks.
I think that you can not compare one single value with whole range like this:
If id <> Sheets("Sheet2").Range("a:a").Value Then
You need to go trough all cells in that range separately.
If Application.CountIf(Sheet2.Range("A:A"), id) > 0 then
'write only to sheet1
else
'write to both sheets
end if

Resources