I have a large set of Excel rows and i have empty row in between .
So i wanna count that empty rows as groups .
put every group count above row then delete all empty rows .
ex.
data row .
data row . i wanna count all empty row below.put on cell on that row (3)
empty .
empty .
empty .
data row.
data row.
data row.
data row . (2).
empty row.
empty row .
data row . (4)
empty.
empty.
empty.
empty.
.
.
.
.
etc
Suppose you have your data in column A of your spreadsheet (starting in cell A1) and you would like to count the empty spaces and delete the rows as follows:
Col A Col B Col A Col B
1 AAA AAA 2
2 BBB 1
3 CCC 0
4 BBB DDD 2
5 ---- Output ---> EEE
6 CCC
7 DDD
8
9
10 EEE
The following code will achieve that outcome:
Sub CountEmptyRows()
Dim lastRow As Long, rw As Long, count As Integer
lastRow = Range("A65536").End(xlUp).Row - 1
count = 0
For rw = lastRow To 1 Step -1
If IsEmpty(Cells(rw, 1)) Then //If cell is empty increment the count and delete the row
count = count + 1
Cells(rw, 1).EntireRow.Delete
Else
Cells(rw, 2) = count //Display emtpy row count and then reset counter
count = 0
End If
Next rw
End Sub
Related
I have an excel table that should contains only unique values in each row. If any of the previous cell values repeating anywhere in the other rows, the complate row should be deleted. The example table is like so
Example
Table The result
______ _____
0 1 3 0 1 3
6 4 1 5 -> 8 9 2
8 9 2
The second row should be cleared because the first row already contains 1 . But the third row should be there because it doesn't contains any duplicates from the previous rows.
I need excel formula to filter like so or vba code with multi dimensional array clearing the unwanted rows.
Try this one...
It works with all the samples I took.
Dim i, j, k, l As Long
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
For k = 1 To LastRow
For i = k + 1 To LastRow
For j = 1 To LastCol
For l = 1 To LastCol
If (Sheets(1).Cells(i, j).Value2 <> "") And _
(Sheets(1).Cells(k, j).Value2 <> "") And _
(Sheets(1).Cells(i, j).Value2 = Sheets(1).Cells(k, l).Value2) Then
Sheets(1).Cells(i, j).EntireRow.ClearContents
End If
Next
Next
Next
Next
Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
I have a huge Excel sheet with about 2000 rows.
So Row 1 is the first header, the columns are: Unique Name, Length, Elevation. Then there is a bunch of data related to these columns.
Row 8 is another header, the columns are: Unique Name, Elevation, Element type. Again some data follows these columns.
And so on goes the Excel sheet with many such rows which are headers.
These headers are not in same order. Here is an example of Excel Sheet1:
Unique Name Length (ft) Elevation (ft) this is Row 1 (header1)
A 20 4 this is Row 2
B 5 10 this is Row 3
C 10 3
D 11 40
E 3 60
Row 7 is blank
Unique Name Elevation (ft) Element Type this is Row 8 (header2)
1 20 Pipe
2 5 Pipe
3 10 Pipe
Row 12 is blank
Unique Name Element Type Elevation Status this is Row 13 (header 3)
A1 VALVE 10 Open
A2 VALVE 2 Open
A3 VALVE 100 Open
. . . .
. . . .
. . . .
. . . .
I need to copy every single columns data from Sheet1 based on specific headers and paste it to Sheet2.
Here is an example of Sheet2, this is what I need:
Unique Name Length (ft) Elevation (ft) Status Element Type this is the only header I need
A 20 4
B 5 10
C 10 3
D 11 40
E 3 60
1 20 Pipe
2 5 Pipe
3 10 Pipe
A1 10 Open VALVE
A2 2 Open VALVE
A3 100 Open VALVE
. . . . .
. . . . .
. . . . .
. . . . .
I have searched a lot, and Alex's VBA code below is the closest I found in this help forum. But it obviously works only for data belonging to Row 1 Header.
Sub CopyPasteData()
Dim header As Range, headers As Range
Set headers = Worksheets("Sheet1").Range("A1:Z1")
For Each header In headers
If GetHeaderColumn(header.Value) > 0 Then
Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=Worksheets("Sheet2").Cells(2, GetHeaderColumn(header.Value))
End If
Next
End Sub
Function GetHeaderColumn(header As String) As Integer
Dim headers As Range
Set headers = Worksheets("Sheet2").Range("A1:Z1")
GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)),
Application.Match(header, headers, 0), 0)
End Function
Thank you.
Should be easy enough to do if you can key off of the "Unique Name" in the first column as an indicator that a new header has been reached. You basically just need to keep track of 3 different mappings - columns for headings that have already been located, rows for unique names that have already been located, and the positions of the headers in the current section.
Dictionaries from the Microsoft Scripting Runtime work well for this. Something like this should do the trick:
Private Sub MergeSections()
Dim source As Worksheet, target As Worksheet
Dim found As Dictionary, current As Dictionary, uniques As Dictionary
Set source = ActiveSheet
Set target = ActiveWorkbook.Worksheets("Sheet2")
Set found = New Dictionary
Set uniques = New Dictionary
Dim row As Long, col As Long, targetRow As Long, targetCol As Long
targetRow = 2
targetCol = 2
Dim activeVal As Variant
For row = 1 To source.UsedRange.Rows.Count
'Is the row a header row?
If source.Cells(row, 1).Value2 = "Unique Name" Then
'Reset the current column mapping.
Set current = New Dictionary
For col = 2 To source.UsedRange.Columns.Count
activeVal = source.Cells(row, col).Value2
If activeVal <> vbNullString Then
current.Add col, activeVal
'Do you already have a column mapped for it?
If Not found.Exists(activeVal) Then
found.Add activeVal, targetCol
targetCol = targetCol + 1
End If
End If
Next col
Else
activeVal = source.Cells(row, 1).Value2
'New unique name?
If Not uniques.Exists(activeVal) Then
'Assign a row in the target sheet.
uniques.Add activeVal, targetRow
target.Cells(targetRow, 1).Value2 = activeVal
targetRow = targetRow + 1
End If
For col = 2 To source.UsedRange.Columns.Count
'Copy values.
activeVal = source.Cells(row, col).Value2
If source.Cells(row, col).Value2 <> vbNullString Then
target.Cells(uniques(source.Cells(row, 1).Value2), _
found(current(col))).Value2 = activeVal
End If
Next col
End If
Next row
'Write headers to the target sheet.
target.Cells(1, 1).Value2 = "Unique Name"
For Each activeVal In found.Keys
target.Cells(1, found(activeVal)).Value2 = activeVal
Next activeVal
End Sub
I want to replace the values in column 1 of sheet 2(which is same as col 2 of sheet 1) by the corresponding values of column 1 of sheet 1.
SHEET 1 SHEET 2 RESULT SET
Col 1 Col 2 Col 1 Col 1
Row 1 A 1 Row 1 1 Row 1 A
Row 2 B 2 Row 2 2 Row 2 B
Row 3 C 3 Row 3 3 Row 3 C
Row 4 D 4 Row 4 4 Row 4 D
Row 5 E 5 Row 5 5 Row 5 E
Hope my question is not absurd
Thanks in Advance``
I used the macrorecorder and then tweaked the code a little bit. This will adapt if you ever have a different number of rows. Hope this helps! Let me know if it's not quite what you meant.
Sub Macro1()
Dim Count As Double
Count = 1
While Range("A" & CStr(Count)) <> ""
Count = Count + 1
Wend
Sheet1.Range("A1:A" & CStr(Count - 1)).Copy
Sheets("Sheet2").Select
Range("A1").Select
ActiveSheet.Paste
End Sub
How using VBScript I can count the number of rows filled with data and how many column has a value for a particular row?
Col1 Col2 Col3 ........ColN-1 ColN+1...ColN+2.......ColN+2
Row1 A B Null Null
Row2 1 2 Y .
Row3 2 .
Row4 P Z
. .
.
.
RowN-2 .
RowN-1 T L Null.......Null
RowN S
RowN+1 Null ........(till the last column of the excel sheet that its version supprts.)
So here my required Loop iteration which i would use for my other logic is N for rows and for columns it would be N+1
Update
Option Explicit
Dim rg,CountBlank
For C=0 to 10
Set rg = Ob6.Range(Ob6.Columns(c),Ob6.Columns(c))
CountBlank = objExcel1.Application.WorksheetFunction.CountBlank(rg)
MsgBox("Column"&C": "&"Has"&(rg.rows.Count-CountBlank))
Next
Thanks
Try this to get you started. It will show for each row the number of columns that have data, and for each column the number of rows that have data. You can then modify it to more suit your needs:
EDIT: Code updated to capture first row / column with only 1 column / row of data:
Option Explicit
Dim rg
'loop through columns
For C=0 to 10
Set rg = Ob6.Columns(c)
If objExcel1.Application.WorksheetFunction.CountA(rg) =1 Then
Exit For
End If
Next
MsgBox("Column" & C & " is first column with only 1 row")
'loop through rows
For C=0 to 10
Set rg = Ob6.Rows(c)
If objExcel1.Application.WorksheetFunction.CountA(rg) =1 Then
Exit For
End If
Next
MsgBox("Column" & C & " is first row with only 1 column")
I need some help converting three colums into a matrix using excel macro.
Here is an example:
From this:
A A 0
A B 23
A C 3
B A 7
B B 56
B C 33
C A 31
C B 6
C C 5
to this:
A B C
A 0 23 3
B 7 56 33
C 31 6 5
Hope you can help me.
Thanks
Not quite sure what exactly you are meaning by matrix. For the code below I assumed you were looking for a way to read the data in the first two columns as Row and Column data of the output table. Assume the input data is in the Columns 1 - 3 of "Sheet1"
Sub ConvertTableOfData()
Dim testArray(1 to 3)
Dim chkROW as Integer
Dim chkCOL as Integer
Dim chkVAL as Integer
'// index the Row and Column headers
testArray(1) = "A"
testArray(2) = "B"
testArray(3) = "C"
'// Iterate through every row in the initial dataset
For i = 1 to Worksheets("Sheet1").Cells(1, 1).End(xlDown).Row
With Worksheets("Sheet1")
'// Assign the Output Row and Column values
'// based on the array indices
For j = 1 to UBound(testArray, 1)
If .Cells(i, 1) = testArray(j) Then
chkROW = j
End If
If .Cells(i, 2) = testArray(j) Then
chkCOL = j
End If
Next j
'// store the actual value
chkVAL = .Cells(i, 3)
End With
'// output table (in Sheet2)
With Worksheets("Sheet2")
.Cells(chkROW, chkCOL) = chkVAL
End With
Next i
'// Add headers to Output table
For i = 1 to 3
With Worksheets("Sheet2")
.Cells(i + 1, 1) = testArray(i)
.Cells(i, i + 1) = testArray(i)
End With
Next i
End Sub
You can also perform this without VBA.
Assume your table of data is in the range A1:C9.
Assume the first number (0) in the 3 by 3 grid of data is cell F3, with A, B, C in the row above, and A, B, C in the column to the left.
Enter the formula in cell F3 as
=INDEX($C$1:$C$9,SUMPRODUCT(--($A$1:$A$9=$E3),--($B$1:$B$9=F$2),ROW($A$1:$A$9)))
Copy this formula to all 9 cells in the 3 by 3 grid.
This generalized to any size of data.