Got any Way to Arrange The data like the picture attached?
*the requirement is when the Column A Cell is empty then Column B Value will Move to previous not Empty Column A Row at Column B Cell.
Example Photo
Try this code
Sub Macro1()
Dim i As Long
i = 1
Do Until Cells(i, 2) = ""
If Cells(i, 1) = "" Then
On Error Resume Next
Cells(i - 1, 2) = Cells(i - 1, 2) & "," & Cells(i, 2)
Cells(i, 2).EntireRow.Delete
i = i - 1
End If
i = i + 1
Loop
End Sub
Related
I have some data like below :
A
1
2
B
1
2
3
C
1
2
3
Every group separated by space and title is appeared in first line of group
I need to add title to first every below rows like this :
A1
A2
B1
B2
B3
C1
C2
C3
I find this code but not working properly :
Sub InsertRowBelowHeader()
Rows(ActiveWindow.SplitRow + 1).Insert
End Sub
Maybe something like this:
Code:
Option Explicit
Sub GroupName()
Dim lRow As Long
Dim i As Long
Dim GroupName As String
lRow = Cells(Rows.Count, "A").End(xlUp).Row 'Go to last row
For i = 1 To lRow 'Loo from 1st row to last row
If i = 1 Then 'If loop is first row
GroupName = Cells(1, "A").Value 'Then take the value for group name
Else
If Cells(i, "A").Value = "" Then 'Check if cell is empty
'If empty do nothing
Else
If Not IsNull(Cells(i, "A").Value) And Cells(i - 1, "A").Value = "" Then 'If current cell is not null and previous cell is not blank then
GroupName = Cells(i, "A").Value 'take the group name
Else
Cells(i, 2).Value = GroupName & Cells(i, "A").Value 'Else print the combination of group name + cell value
End If
End If
End If
Next i
Dim j As long
For j = lRow To 2 Step -1 'To delete empty spaces (row 5, row 10 in example)
If Cells(j, "B").Value = "" And Cells(j - 1, "B").Value = "" Then
Range(Cells(j, "A"), Cells(j, "A")).EntireRow.Delete
End If
Next j
End Sub
I am working on a project where I want end users to be able to check of the items they need. the checked off items then need to be added to a list beside the items. the problem im having is that i can't figure out how to get excel to add info to the lines without skipping lines where items aren't checked. this is difficult to explain, but the picture attached show exactly what i mean. At the end of the list there should be a sum of the prices and this sum should always be shown after the last line with data so it should be able to move depending on how many lines of data there is. so this is my problem.
Sub MakeList()
Dim i As Long
For i = 1 To Rows.Count
If Cells(i, 4) = True Then
Cells(i, 6) = Cells(i, 1)
Cells(i, 7) = Cells(i, 2)
Cells(i, 8) = Cells(i, 3)
End If
Next i
For i = 1 To Rows.Count
If Cells(i, 4) = True Then
Sum = Sum + Cells(i, 3)
End If
Next i
Cells(24, 4) = Sum
End Sub
One way to do this is to simply add an offset value every time a cell is not included. This would just be a simple counter in the Else portion of the If statement.
Sub MakeList()
Dim i As Long
Dim Sum As Double
Sum = 0
Dim n As Long
n = 0
For i = 1 To Rows.Count
If Cells(i, 4) = True Then
Cells(1 + i - n, 6) = Cells(i, 1)
Cells(1 + i - n, 7) = Cells(i, 2)
Cells(1 + i - n, 8) = Cells(i, 3)
Sum = Sum + Cells(i, 3)
Else
n = n + 1
End If
Next i
'Place sum at the bottom of the original table
'Cells(24, 4) = Sum
'Place sum at the bottom of the output table
Cells(i + 1 - n, 8) = Sum
End Sub
This tells your code that every time an item is not included, the output should be offset by that row. I also moved the summation inside the original loop, to avoid looping twice. Let me know if this works for you.
My Data : http://imgur.com/a/R9wZp
My code so far:
Sub Leads()
ActiveSheet.Range("J:J").Select
For i = 1 To 100
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "Another Car" Then Range("J1").Copy ("L1")
Next i
End Sub
I want to scroll down J column and everytime the value "Another Car" and a portion of "Mikes Auto Shop" springs up I want to copy and paste the row RIGHT under it into the "L,M, and O" column within the same row.
Just like this http://imgur.com/a/Bt3A5 but would cycle through hundreds of lines of code
Really really appreciate the help everyone, thanks!
This will work given a few assumptions, like, there's no apostrophe in Mikes Auto Shop and that the first space in the car model is the correct place to split the data.
Option Compare Text
Sub test()
Dim DataRange As Range
Dim LastRow As Integer
Dim i As Integer
Dim SplitVal() As String
LastRow = Cells(Rows.Count, "J").End(xlUp).Row
For i = 1 To LastRow
If ActiveSheet.Cells(i, 10).Value = "Another Car" Then
If InStr(1, Cells(i + 3, 10).Value, "Mikes Auto Shop", vbTextCompare) <> 0 Then
SplitVal = Split(Cells(i + 1, 10).Value, " ", 2)
Cells(i + 1, 12).Value = SplitVal(0)
Cells(i + 1, 13).Value = SplitVal(1)
Cells(i + 1, 15).Value = Cells(i + 4, 10).Value
End If
End If
Next i
End Sub
Edit as per comment request. I'm not sure where you want the output, you can adjust OutputOffset, Mikes Auto Shop row is 0, -1 is up, +1 is down.
Sub test()
Dim DataRange As Range
Dim LastRow As Integer
Dim i As Integer
Dim SplitVal() As String
Dim OutputOffset As Long
OutputOffset = 0
LastRow = Cells(Rows.Count, "J").End(xlUp).Row
For i = 2 To LastRow
If InStr(1, Cells(i, 10).Value, "Mikes Auto Shop", vbTextCompare) <> 0 Then
SplitVal = Split(Cells(i - 1, 10).Value, " ", 2)
Cells(i + OutputOffset, 12).Value = SplitVal(0)
Cells(i + OutputOffset, 13).Value = SplitVal(1)
Cells(i + OutputOffset, 15).Value = Cells(i + 1, 10).Value
End If
Next i
End Sub
Let's start from your code:
Sub Leads()
ActiveSheet.Range("J:J").Select
For i = 1 To 100
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "Another Car" Then Range("J1").Copy ("L1")
Next i
End Sub
What I would do:
Get rid of ActiveSheet.Range("J:J").Select as it might be slow to work with selection
Note: For i = 1 To 100 will take the rows from 1 to 100. You might want to actually use a dynamic method to check that number. You can check the following: https://stackoverflow.com/a/11169920/2012740.
If you get rid of the selection, remove also the ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "Another Car" Then Range("J1").Copy ("L1") will become:
If Cells(i,10).Value = "Another Car" Then 'This condition is the same as before
SplitedValue = Split(Cells(i+1,10).Value," ") ' With this code you will split the value from the first row below the row which contains "Another Car" text. The value is splitted by " " (empty space). For more references and parameters you can read about the other parameters of `split` function
Cells(i+1,12).Value = SplitedValue(0) 'This will add the first part of the splitted string in the cell which is one row below the current row, and on column 12 (L)
Cells(i+1,13).Value = SplitedValue(1) 'This will add the second part of the splitted string in the cell which is one row below the current row, and on column 13 (M)
Cells(i+1,15).Value = Cells(i+4,10).Value ' This will add the value from the cell which is located 4 rows below the current cell, to the cell which is located one row below the current row and on column 15 (O)
EndIf 'Close the if statement here
Remember to declare dim SplitedValue as Variant
I received many Excel files from a client.
Their system extracted the data into a spreadsheet, but one column is having issues. If the text was too long, it would put the remaining text into the cell below it.
This causes all the other fields in that row to be blank, except for the overflow.
How can I merge cells at issue into one for all files I received?
I uploaded a screen shot of the file as an example. Notice on row 8 that H8 is the only cell. That needs to be merged with H7. Not every row is at issue though.
asuming A is the main (and empty for doubles)
asuming H holds the text
then in L1 and copy down
=H1&IF(LEN(A2),H2,"")
simplest way (then copy values from L to H and delete empty lines (simply with filter)
when having unknown number of lines (after splitting) you better use vba (or simply repeat the whole procedure till there no empty lines anymore...
doing it in VBA:
Sub testing()
Dim i As Long
While Len(Cells(i + 1, 8))
i = i + 1
While Len(Cells(i + 1, 1)) = 0 And Len(Cells(i + 1, 8))
Cells(i, 8) = Cells(i, 8) & Cells(i + 1, 8)
Rows(i + 1).Delete
Wend
Wend
End Sub
most programs skip spaces so you may want to use:
=H1&IF(LEN(A2)," "&H2,"")
or for vba change Cells(i, 8) = Cells(i, 8) & Cells(i + 1, 8) to Cells(i, 8) = Cells(i, 8) & " " & Cells(i + 1, 8)
This will concatenate the texts in H and delete the row that is not useful :
Sub test_bm11()
Dim wS As Worksheet, _
LastRow As Long, _
i As Long
Set wS = ActiveSheet
With wS
LastRow = .Range("H" & .Rows.Count).End(xlUp).Row
For i = LastRow To 2 Step -1
If .Cells(i, "A") <> vbNullString Then
Else
.Cells(i, "H").Offset(-1, 0) = .Cells(i, "H").Offset(-1, 0) & .Cells(i, "H")
.Cells(i, "H").EntireRow.Delete
End If
Next i
End With
End Sub
I have started learning VBA programming and thought of creating one small application for inserting student details into an Excel sheet.
In one Excel sheet named "Main", I have created a form to take user inputs and in another sheet named "Database" I am trying to insert a row of records every time a user clicks the button.
I am successfully able to insert one row of data i.e. first row of data in the database sheet, but my problem is - I want to go to the next row in the sheet once user enters the record and clicks on the button a second time. Similarly for the third time and so on.
My code is:
Private Sub CommandButton1_Click()
Dim i As String
Dim j As String
Dim k As String
Dim l as integer
i = Sheets("Main").Cells(1, 2).Value
j = Sheets("Main").Cells(2, 2).Value
k = Sheets("Main").Cells(3, 2).Value
Sheets("Main").Cells(1, 2).Value = ""
Sheets("Main").Cells(2, 2).Value = ""
Sheets("Main").Cells(3, 2).Value = ""
l=2
Sheets("Database").Cells(l, 1).Value = i
Sheets("Database").Cells(l, 2).Value = j
Sheets("Database").Cells(l, 3).Value = k
End Sub
I want to increment value of l by 1, every time user clicks on the command button so that the new record is inserted into the next row of the Database sheet.
We can get the last row of a particular column by :-
dstRw = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1
Private Sub CommandButton1_Click()
Dim i As String
Dim j As String
Dim k As String
Dim m As String
i = Sheets("Main").Cells(1, 2).Value
j = Sheets("Main").Cells(2, 2).Value
k = Sheets("Main").Cells(3, 2).Value
m = Sheets("Main").Cells(4, 2).Value
Sheets("Main").Cells(1, 2).Value = ""
Sheets("Main").Cells(2, 2).Value = ""
Sheets("Main").Cells(3, 2).Value = ""
Sheets("Main").Cells(4, 2).Value = ""
dstRw = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("Database").Cells(dstRw, 1).Value = i
Sheets("Database").Cells(dstRw, 2).Value = j
Sheets("Database").Cells(dstRw, 3).Value = k
Sheets("Database").Cells(dstRw, 4).Value = m
End Sub
Thanks :-
Nitish Gaurav
Quite confused about what is you want. How is the user inputting the next row? Also at the top of your code (where you assign values to i,j and k) you are iterating the row and essentially copying a column, not a row.
I'll try to answer the remainder of your question as best I can. To make VBA know how to copy data to the next row you can do this:
Sub test()
i = "John"
j = "25"
k = "Male"
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Cells(lastRow + 1, 1) = i
Cells(lastRow + 1, 2) = j
Cells(lastRow + 1, 3) = k
End Sub
Try running that a couple of times in a row and you'll see how it works. If I misunderstood please clarify what you want.