Copy data in a row and spread them on separated columns - vba

My row data is as follows:
Column A
Datapoint1
Datapoint2
Datapoint3
Datapoint4
I can transpose, but the result will be like this.
Column A Column B Column C
Datapoint1 Datapoint2 Datapoint3
What I am trying to achieve is
Column A, Column B, Column C, Column D, Column E, Column F, Column G
Datapoint1, empty, empty, empty, empty, empty, Datapoint 2
Datapoint 3 would appear in Column M. There are 5 columns in between.
There are about 3,000 Datapoints that need to be transposed into columns (separated by 5 columns in between).

You can use this function below. I tested it with 3000 items and it ran almost instantly. The problem is after 2732 items, you run out of rows as the Excel maximum column number is 16,384 as of Office 2013 (see Office 2010 Excel Specifications and Office 2013 Excel Specifications). In the code below, once it passes that, it simply clears out the remaining rows and prints a statement to the debug window. Handle this how you want.
Sub TransposeIt()
Const maxColumns As Integer = 16384
On Error GoTo er
Application.ScreenUpdating = False '' shut off screen updating to speed up if possible
Dim x As Range
Set x = Range("A1", Range("A1").End(xlDown)) '' Selects all cells until there is a blank
'' if you might have blanks, then calculate the last row manually
Dim c As Range, col As Integer, val As Variant
For Each c In x.Cells
val = c.Value '' so as not to overwrite A1
c.Value = ""
col = (c.row - 1) * 6 + 1
If col > maxColumns Then '' max number of rows we can actually use is 2732
Debug.Print "Out of Columns, clearing Row " & c.row '' do what you want at this point
Else
Cells(1, col).Value = val
End If
Next
ex:
Application.ScreenUpdating = True
Exit Sub
er:
Debug.Print "An Error has occured: " & Err.Description
Resume ex
End Sub

Related

Removing loops to make my VBA macro able to run on more data

in my data there are more than a thousand different six digit numbers that are reoccurring in no specific pattern. I need to find all six digit codes that exist in column A and for each number. For example 123456, then find summarize the value in column B for every row that has 123456 in column A. My code is not very effective but the runtime is not a problem if I run with only 10 rows. However, in the real data sheet there are 80 000 rows and my code will take to much time. Can someone help me edit my code but removing certain loops within loops or some stop conditions. I'm new to VBA and can't do it myself in the limited time I have.
Sub Test2()
Dim summa As Long
Dim x As Long
Dim condition As Boolean
Dim lRows As Long
Dim k1 As Integer
Dim i As Long
x = 1
Worksheets("Sheet1").Activate
For i = 100000 To 999999
k1 = 1
lRows = 10
condition = False
While k1 <= lRows
If Cells(k1, "A").Value = i Then
condition = True
End If
k1 = k1 + 1
Wend
If condition = True Then
Cells(x, "F").Value = Application.SumIf(Range("A:A"), CStr(i), Range("B:B"))
Cells(x, "E").Value = i
x = x + 1
End If
Next i
MsgBox "Done"
End Sub
You don't need VBA for this task. Follow these steps.
Insert a blank column C in a copy of the original data sheet.
Insert a SUMIF formula, like =SUMIF(A:A, A2, B:B) in C2 and copy all the way down.
Now all items 123456 will have the same total in column C
Copy column C and Paste Values (to replace the formulas with their values).
Delete column B.
Remove duplicates.

Invalid Outside Procedures and Syntax in VBA/Excel

I'm new to programming/coding, and I'm having problems with my code.
I'm currently trying to write a macro in excel that will go through an excel document and insert a partially blank row on duplicate columns.
I'm currently using Excel 2010
So for example if the excel sheet contained:
Column A, Column B, Column C, Column D, Column E
1 Apples 1 40 Blue
1 Bananas 2 50 Red
1 Oranges 3 60 Pink
2 Cherries
3 Kiwis
Then the script would sort it into:
Column A, Column B, Column C, Column D, Column E
1 Apples 1 40 Blue
1 Bananas
1 Oranges
2 Cherries 2 50 Red
3 Kiwis 3 60 Pink
Thus sorting the data by Column A and C while also creating blank spaces in C, D, and E if the value in Column A does not equal the value found in Column C.
My code so far:
Sub Main()
Dim a As Long, c As Long
Dim objRange As Range
Dim strCIDCol1 As String, strCIDCol3 As String
a = 1 'row counter for Column A
c = 1 'row counter for Column C
Do Until ActiveSheet.Cells(a, 1) = ""
'sets the loop to run until A1 is blank
strCIDCol1 = ActiveSheet.Cells(a, 1)
'sets the value of A1 as CIDCol1
strCIDCol3 = ActiveSheet.Cells(c, 3)
'sets the value of C1 as CIDCol3
If (strCIDCol1 <> strCIDCol2) Then
'runs until A(a) and C(c) are not equal
Set objRange = ActiveSheet.Cells(c, 3).Range(Cells(c, 3), Cells(c, 5))
objRange.Activate
'Selects Columns C, D, and E
objRange.Insert (xlShiftDown)
'Inserts "Shift Row Down"
strCIDCol1 = ActiveSheet.Cells(a + 1, 1)
'moves on
End If
a = a + 1 'adds 1 to counter to move to next row
c = c + 1 'adds 1 to counter to move to next row
Loop
End Sub
I keep getting "invalid outside procedure" errors on
a = 1
c = 1
and
a = a + 1
c = c + 1
I'm also getting a 1004 error "insert method of range class failed" on
objRange.Insert (xlShiftDown)
I have no idea what I'm doing with objRange. I saw the code online and tried to adapt it to fit what I needed. The way it was explained was that objRange was used to highlight the selected areas Column C, D, and E in this case. And then Insert would shift the cells down by inserting a row above the highlighted cells.
If (strCIDCol1 <> strCIDCol2) Then
You have never defined strCIDCoL2. I think you meant str CIDCol3 here.
Set objRange = ActiveSheet.Cells(c, 3).Range(Cells(c, 3), Cells(c, 5))
This is wrong syntax for setting range. Try using:
Set objRange = Range(ActiveSheet.Cells(c,3).Range(Cells(c,3), Cells(c,5)))
Also are you sure about using objRange.activate instead of objRange.select ?

Excel Formula/VBA to search partial strings in other sheet

I am having names in two cells of sheet1 (e.g. : B1 (Gina Williams) & B2 (Patrick Rafter)) and the corresponding bank statement narratives are in sheet 2 (column C) e.g: "Deposit from Gina towards rent for connaught place apt".
Now I need to search all the four partial texts available in cells B1 & B2 of sheet 1 (ie. "Gina", "Williams", "Patrick", "Rafter" in the entire column B of sheet 2. if there is a match i need to capture the corresponding column B & D value for the matching row.
SHEET1
Column A Column B Column C Column D
1 GINA WILLIAMS OUTPUT (matching col b of sheet2) OUTPUT (matching col D of sheet2)
2 PATRICK RAFTER OUTPUT (matching col b of sheet2) OUTPUT (matching col D of sheet2)
SHEET2
Column A Column B Column C Column D
1 12/7/2015 Deposit from Gina towards rent for connaught place apt 320
2 13/7/2015 Deposit from Rafter towards rent for connaught place apt 720
I have tried with vlookup, find, match (along with left, right, mid functions) functions.
You could use VBA to achieve this, but if you've not done VBA before, this might not be a good idea.
I would favour adding another column to sheet 2 when you manually enter the name from sheet 1 into each cell. In every cell of this new column, you can give the user a drop down list of all names that can be entered by using the excel ribbon>Data>Data Tools>DataValidation option.
This solution will work - so long as your bank statement is not enormous! If it is then you might want to do it differently. It also gets around the issue of two people on sheet1 having the same forename or surname, and is probably something you will be able to do quite quickly.
Once the above is done, you can simply use VLOOKUP in sheet 1 to fin the data on sheet 2.
KISS.
Harvey
I got one for you. I already tested the code. It work perfectly for me.
But, not grantee for duplicate naming , means, it can't give right result for duplicate names and duplicate deposit.
Here the code:
Sub findAndGet()
Dim sh1, sh2 As Worksheet
Dim tempRow1, tempRow2 As Integer
Dim strList() As String
Dim name As String
Dim index As Integer
'Set sheets
Set sh1 = Sheets("list")
Set sh2 = Sheets("search")
'Set the start row of Sheet1
tempRow1 = 1
'Loop all row from starRow until blank of column A in Sheet1
Do While sh1.Range("A" & tempRow1) <> ""
'Get name
name = sh1.Range("B" & tempRow1)
'Split by space
strList = Split(Trim(name), " ")
'Set the start row of Sheet2
tempRow2 = 1
'Reset flag
isFound = False
'Loop all row from startRow until blank of column A in Sheet2
Do While sh2.Range("A" & tempRow2) <> ""
For index = LBound(strList) To UBound(strList)
'If part of name is found.
If InStr(UCase(sh2.Range("C" & tempRow2)), UCase(strList(index))) > 0 Then
'Set true to search flag
isFound = True
'exit do loop
Exit Do
End If
Next index
'Increase row
tempRow2 = tempRow2 + 1
Loop
'If record is found, set output
If isFound Then
'set date
sh1.Range("C" & tempRow1) = sh2.Range("B" & tempRow2)
'set amount
sh1.Range("D" & tempRow1) = sh2.Range("D" & tempRow2)
End If
'Increase row
tempRow1 = tempRow1 + 1
Loop
End Sub

VBA Excel - select all rows with value in a column

I have a spreadsheet named "Copy" sorted by numeric values (lowest to highest) in column H. The sheet has headers in row 1. The lowest value in column H is 0. I would like to find the last row that has column H equal 0 and then select the range of rows where column H equals 0 along with the header row.
Thanks!
This will do the job
Sub CustomSelect()
Dim i As Long: i = 2
Do While Range("H" & i).Value = 0
i = i + 1
Loop
Range("H1:H" & i - 1).EntireRow.Select
End Sub

Comparing the cell values and printing the count in Excel using a formula or function?

I need a formula or function which is going to fulfill my below mentioned need. I have a excel data of around 11000 rows and data looks somewhat like in Column A:
Now in column B i want the result to be printed like it mentioned below: which literally means it should count the values present in column A and print it in the column B, I don't need to repeat count:
Column A Column B
PC-101 1
PC-101 1
PC-102 2
PC-102 2
PC-103 3
PC-104 4
PC-106 5
PC-107 6
PC-104 4
PC-106 5
PC-106 5
I tried with the "count" series formulas but the result was null.
Even i wrote the macro as given below( which i got from stackoverflow) but even it is printing the repeating count:
Sub CountOccurence()
' Reference: Microsoft Scripting Runtime
Application.ScreenUpdating = False
Set oDict = New Dictionary
Dim wS As Worksheet
Dim r As Integer, rLast As Integer
Set wS = Sheet1
rLast = wS.Cells(1, 1).CurrentRegion.Rows.Count
For r = 3 To rLast Step 1
If Not (oDict.Exists(wS.Cells(r, 1).Value)) Then
oDict.Add wS.Cells(r, 1).Value, 1
Else
oDict.Item(wS.Cells(r, 1).Value) = oDict.Item(wS.Cells(r, 1).Value) + 1
End If
wS.Cells(r, 2).Value = oDict.Item(wS.Cells(r, 1).Value)
Next r
Set oDict = Nothing
Application.ScreenUpdating = True
End Sub
Can anyone help me regarding this? Thanks in advance.
I tried with the "count" series formulas but the result was null.
A simple Excel formula can do this.
Put 1 in Cell B1 and then put this formula in cell B2 and pull it down.
=IF(COUNTIF($A$1:$A2,A2)>1,VLOOKUP(A2,A:B,2,0),B1+1)
Assuming that your data in column a is sorted, you can simply place 1 in B2 this formula in B3 and copy it down:
=IF(A2<>A3,B2+1,B2)
:-)