I have a list of about 300 items, that I need spaced out every 8 cells as opposed to being one after the other. I'm sure there is an easy way to do this, however my brain is failing me. I have a feeling my terminology is hurting hence why I can't find an answer.
=IF(MOD(ROW()+7;8)=0;INDEX(A:A;INT(ROW()/8)+1);"")
Given that data begins at A1 and formula is used from row 1 (coulmn is not important).
Try below code
Sub Main()
Dim lastRow As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastRow
If i = 1 Then
Cells(i, 5).Value = Cells(i, 1)
Else
Cells((i - 1) * 9, 5).Value = Cells(i, 1)
End If
Next
End Sub
Output
Can you please post a screen shot or add some additional detail?
Two things come to my mind for possible solutions:
1) Use Text to columns (if that is where you're going with this) or
2) Use a formula like =LEFT(A1, 10) in the 8th column and fill down (10 can be changed to whatever the first part of the string is that needs to be separated).
Provide some additional info and I'll take another look!
Related
So basically, I have a table, where in Column A, I have a repeated list of data with multiple duplicates. In Column B, I have different sets of data related to column A, and then same as Column C. What I wish to have excel do for me is to give me the table (A15:C18), where the Column A data is one cell for each value, and then all the related information is shown right next to it in a concatenated manner.
My approach so far
So basically, what I have done so far is the following approach:
Since Column A are the only thing common in defining the different
lines, so I came up with a way to define each line as a unique line,
using the following =(A1&"--"&countif($A$1:A1;A1). This formula then
helps me identify each row as A--1, A--2, A--3, B--1, B--2 and so
on. Hence I have numbered each data in each row, in a way.
Since I want a unique list, therefore using VBA I get a unique list of variables in a new sheet. Hence, I get the list of just A, B and C in a column instead of them being duplicates.
Once I have the unique list, I then use the =countif() function to count how many times, the variable shows up in the original list (Column A). Like for example, A shows up three times, hence I know that I need to extract 3 rows for this specific data point.
Then using that information, I then proceed to use the =vlookup() function, using the equation =VLOOKUP(A&"--"&1;TABLE;COLUMN)&", "&VLOOKUP(A&"--"&2;TABLE;COLUMN)&", "&VLOOKUP(A&"--"&3;TABLE;COLUMN) .... so on depending on how many times the variable is in the list.
Finally, for example, I then write the vlookup code, and I can then extract all the cells into one place.
PROBLEMS and HELP
So the above approach I have described works on a pilot scale with lots of manual changes for different data points. The problems are the following:
The process of using =vlookup() is based on the =countif() function. I wrote down a very long formula using IF statements saying that =IF(COUNTIF()=1;VLOOKUP(VAR&--&1;TABLE..);IF(COUNTIF()=2;VLOOKUP(VAR&--&1;TABLE..)&","&VLOOKUP(VAR&--&2;TABLE..); ...
Basically, I wrote the IF statement depending on the value of the countIf value. If it is 1, then extract the values for --1, but if it is 2, then do it for --1 and --2, and vice versa. But this is such a bad approach because I can not write this equation for 100 duplicates and if a data point shows up more than 100 Times, then this approach is useless. hence, I would like to know, if it is somehow possible to use a loop VBA code for excel to do a vlookup? So if countif is 4, then do a vlookup from --1 until --4 and so on?
Another limtation is that even though B shows up 2 times in column A, but it only has 1 data point in Column B, and that is the information I need. Hence I should be focusing on that instead of using the =countif() on Column A. **Any suggestions on how I can count, how much information is infront of a data point in the second column? The data in Columns B and C are unique so there are no duplicates. **
I am stuck with the above two points, which kind of makes up the engine of the workbook. So any help or suggestion on how to approach this problem would be greatly appreciated!
Below is the updated image to show the general approach:
Interesting problem, here's my approach.
Pre-requisites:
You need Microsoft Scripting Runtime: Tools -> References -> Microsoft Scripting Runtime -> check the box
There are 2 components of this code.
First the sub you will run:
Sub Concatenate_Data()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("YourWorksheetsName")
Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Dim r As Long
Dim dict As New Scripting.Dictionary
Dim letterStr As String
For r = 2 To lastRow
letterStr = ws.Cells(r, 1).Value
If dict.Exists(letterStr) Then
If Not ws.Cells(r, 2).Value = "" Then
dict(letterStr).Candy = dict(letterStr).Candy & ", " & ws.Cells(r, 2).Value
End If
If Not ws.Cells(r, 3).Value = "" Then
dict(letterStr).Juice = dict(letterStr).Juice & ", " & ws.Cells(r, 3).Value
End If
Else
dict.Add letterStr, New Letter
If Not ws.Cells(r, 2).Value = "" Then
dict(letterStr).Candy = ws.Cells(r, 2).Value
End If
If Not ws.Cells(r, 3).Value = "" Then
dict(letterStr).Juice = ws.Cells(r, 3).Value
End If
End If
Next r
Dim k As Variant
r = 2
For Each k In dict.Keys
ws.Cells(r, 5).Value = k
ws.Cells(r, 6).Value = dict(k).Candy
ws.Cells(r, 7).Value = dict(k).Juice
r = r + 1
Next
End Sub
Next is the Class (it is named Letter):
Public Candy As String
Public Juice As String
Here is the input and output I get:
Good Luck!
I am trying to make a sum formula on a dynamic range. Much like in a pivot table.
Taking a look on the picture I want L15 to be the sum of the range from L16 to the blank row. As the range is dynamic I am not sure how to write it on my code. So far what I have is this:
If out.Range("A15").Cells(i, 1) = "Aktier" Then
out.Range("L15").Cells(i, 1) = Application.WorksheetFunction.Sum(Range("L15").Cells(i + 1, 1), Range("L15").Cells(i + 1, 1).End(xlDown))
End If
So my question is basically how do I write something like Sum(A1:End(xlDown))? :)
Hope you can help me out guys! :D
Thanks in advance!
First I would recommend that every time that's possible, you get a structure more Data Base-like, so I would have a column repeating the concept and then you could use the Excel SUMIF function easily.
Probably that's not your case (that seems the output of an accounting program). Taking advantage of using VBA macros, you can use a loop to generate the column I mentioned before (you could do the sum as well using the concept, but I believe is cleaner generating a better data format). Please see the image below:
Sub Add_Concepts()
i = 1
Concept = Cells(i, 2)
i = 2
Do While (Cells(i, 2) <> "")
Cells(i, 1) = Concept
If Cells(i + 1, 2) = "" Then 'Change in concept
Concept = Cells(i + 2, 2) 'New concept
i = i + 2 'add 2 to skip the New concept line and the white space
End If
i = i + 1
Loop
End Sub
Now you can use the regular Excel functions.
Hope this helps!
Skipping Steps
Snippet:
If out.Range("A15").Cells(i, 1) = "Aktier" Then
With out.Range("L15")
.Cells(i, 1) = Application.WorksheetFunction _
.Sum(.Parent.Range(.Offset(1), .Offset(1).End(xlDown)))
End With
End If
Working example:
Option Explicit
Sub SumToBlank()
Dim out As Worksheet: Set out = ThisWorkbook.Worksheets("Sheet1")
Dim i As Long: i = 1
If out.Range("A15").Cells(i, 1) = "Aktier" Then
With out.Range("L15")
.Cells(i, 1) = Application.WorksheetFunction _
.Sum(.Parent.Range(.Offset(1), .Offset.End(xlDown)))
End With
End If
End Sub
I have some code that loops through columns to find a specific ending in a column (_END). If it finds that, then it will loop through the rows in that column, changing date formatting. This works as intended and I am having no issues with it. However, I need to UCase the rows as well. Right now, it would output a date like "01-Jan-2016". However, I need it to be "01-JAN-2016". I have code below that is giving me trouble.
lngColHeaders = Cells(5, Columns.Count).End(xlToLeft).Column
For X = 1 To lngColHeaders
If (Right(Cells(5, X), 4)) = "_END" Then
LastRowDates = Cells(Rows.Count, X).End(xlUp).Row
For ZZ = 6 To LastRowDates Step 1
Cells(ZZ, X).NumberFormat = "dd-MMM-YYYY"
UCase (Cells(ZZ, X))
Next ZZ
End If
Next X
Like I said, it seems to format them correctly, but UCase (Cells(ZZ,X)) seems to do nothing. Any help is much appreciated.
I think you'll need to format the cell as text, and then do your UCase, along with a Format.
Cells(ZZ, X).NumberFormat = "#"
Cells(ZZ, X).Value = UCase(Format(Cells(ZZ, X).Value, "dd-MMM-YYYY"))
For i = i To nr_commercial
Cells(i, 1) = commercial & RandBetween(1, 6)
Next i
Hi All - I have the above but I am not able to solve it. It returns a compiler error every time. I'm having issues putting a string plus a formula together.
commercial is defined as a string
I have even tried
Cells(i, 1) = commercial & = Randbetween(1,6)
and it returns error saying expecting expression. Is there something not right with the randbetween functionality?
I need to have output in cells such like:
commercial-1
commercial-5
commercial-3
Try:
Application.WorksheetFunction.RANDBETWEEN(1,6)
For example:
Sub qwerty()
Dim nr_commercial As Long, commercial As String
nr_commercial = 10
commercial = "whatever"
For i = 1 To nr_commercial
Cells(i, 1) = commercial & Application.WorksheetFunction.RandBetween(1, 6)
Next i
End Sub
(also fixed the loop index)
Try this
For i = i To nr_commercial
Cells(i, 1) = "commercial-" & Application.WorksheetFunction.RANDBETWEEN(1,6)
Next i
Edited : Application.WorksheetFunction.RANDBETWEEN(1,6) is the correct formula to be used
Also it is not advisable to use Cells try to use Sheet or Worksheet in your code.
I think your issue is that you are using For i = i To nr_commercial which means every iteration it is changing "For 1 = 1 to nr_commercial" and then "2 = 2 to nr_commercial" which doesn't make sense. It should be For i = 1 to nr_commercial or whatever cell you want to start at, probably not in the first cell, as you are likely to have headers.
There seems to be lots of similar questions but nothing that quite seems to answer this one.
I have created the below sub to convert a column of text to dates. It works in some scenarios, i.e. where the date has 8 characters such as 10022017, but not if the value has 7 characters such as 1022017.
Any ideas appreciated.
Sub convertDate()
'Find the last Row with data in a Column
Dim lastRow As Long
Dim i As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, ColumnSelect).End(xlUp).Row
End With
'convert column from string to date
For i = RowSelect To lastRow
Cells(i, ColumnSelect).Value = Format(Cells(i, ColumnSelect).Value, "00/00/0000")
Next i
End Sub
Hi again all, still having a few problems, the ideal formula seems to be =DATEVALUE(TEXT(A1,"00-00-0000")) if for example A1 = e.g. 02022017. Still haven't found the best way to do this with VBA as everything seems to run up against the truncated zeros problem. Thanks in advance
If your format is going to be consistent then try this.
This adds a 0 before Cells(i, ColumnSelect).Value when there are 7 characters.
For i = RowSelect To lastRow
Select Case Len(Trim(Cells(i, ColumnSelect).Value))
Case 8
Cells(i, ColumnSelect).Value = Format(Cells(i, ColumnSelect).Value, "00/00/0000")
Case 7
Cells(i, ColumnSelect).Value = Format("0" & _
Cells(i, ColumnSelect).Value, "00/00/0000")
End Select
Next i
But be careful with numbers like 1122017... this can be 11/2/2017 or 1/12/2017
Edit
Looking at 10022017, I realised that 1122017 will be 1/12/2017 and not 11/2/2017 as your mm zeros are not getting truncated.
OP wants a formula as well
=IF(LEN(A1)=8,TEXT(DATEVALUE(LEFT(A1,2)&"/"&MID(A1,3,2)&"/"&RIGHT(A1,4)),"dd/mm/yyyy"),TEXT(DATEVALUE(LEFT(A1,1)&"/"&MID(A1,2,2)&"/"&RIGHT(A1,4)),"dd/mm/yyyy"))