Merge columns where cells have same values, excluding "0"s [closed] - vba

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've seen many questions on merging, but nothing that I can manipulate (in my entry-level abilities) to answer this particular question and would be forever be grateful for your expertise and help!!!
I am looking to merge different groups of cells with the same values for particular ranges.
Below is an example of inputs and the desired outcome. I have the file formatted so that the "0"s and "no"s don't show, however the actual descriptions (substituted with like 50% off) are quite long and cannot be viewed in a single cell, hence the need for merge cells to better display information. There are also multiple stores with new stores being added weekly, thus I would like to avoid merging cells manually.
Input
Month January February March April May
Store 1 Campaign Period no yes yes yes no
Campaign Details 0 50% off 50% off 50% off 0
Store 2 Campaign Period no no no yes yes
Campaign Details 0 0 0 spring fling spring fling
Desired Output
Month January February March April May
Store 1 Campaign Period no yes yes yes no
Campaign Details 0 50% off 0
Store 2 Campaign Period no no no yes yes
Campaign Details 0 0 0 spring fling

This should get you started. The data is assumed to be in "Sheet1". It will merge cells in rows that are labeled with "Campaign Details" in Column 'A'. The merge is performed on adjacent row cells that have the same value - at least two adjacent cells with the same value will be merged.
Option Explicit
Sub MergeSameDetails()
Dim sht As Worksheet
Set sht = Worksheets("Sheet1")
Application.DisplayAlerts = False
With sht
Dim lastrow As Integer, i As Integer, j As Integer, cnt As Integer
Dim val As Variant
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To lastrow
If .Cells(i, "A").Value = "Campaign Details" Then
cnt = 1
val = .Cells(i, 2).Value
For j = 3 To 7
If val = .Cells(i, j).Value Then
cnt = cnt + 1
Else
If cnt >= 2 And val <> "0" Then
.Range(Cells(i, j - cnt), Cells(i, j - 1)).Merge
.Cells(i, j - cnt).HorizontalAlignment = xlCenter
End If
cnt = 1
val = .Cells(i, j).Value
End If
Next
End If
Next
End With
Application.DisplayAlerts = True
End Sub

Related

Delete/hide 9 out of every 10 rows? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I cannot find a method to do the following:
Say I have a data set of anywhere between 5-50,000 rows of data output (only ~20ish columns). I would like to graph this data set but it is quite memory taxing. Is there a macro to hide 9 out of every 10 rows to essentially cut the dataset down to 10% (more manageable to graph)? The data is collected every few seconds so I can easily cut out superfluous data without impact to the graph.
I've tried "delete/hide every other row loops" but it still leaves a large amount of information. I would think a 90% (or, if tunable, an X% cut) would help best. Thanks for any comments.
This code will randomly select/delete 90% of rows. It might not be exactly 90% because of randomness. Plenty of methods of ensuring exactly 90% but I prefer this for simplicity.
Sub RemoveRows()
Dim inputRange As Range, removeRange As Range, r As Range
Set inputRange = Sheet1.Range("A1:A1000")
Randomize
For Each r In inputRange
If Rnd < 0.9 Then
If removeRange Is Nothing Then
Set removeRange = r
Else
Set removeRange = Union(removeRange, r)
End If
End If
Next r
removeRange.EntireRow.Select
'removeRange.EntireRow.Delete
End Sub
Below code will hide every x number of rows in the range. You can assign value of x in variable rowCnt which is =9 in the code.
Sub Demo()
Dim rng As Range, cel As Range, hideRng As Range
Dim lastRow As Long, rowCnt As Long, i As Long
Dim ws As Worksheet
rowCnt = 9 'number of rows to hide
Set ws = ThisWorkbook.Sheets("Sheet5") 'change Sheet5 to your data sheet
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row with data using Column A
For i = 2 To lastRow Step rowCnt + 1 'loop through Column A starting from Row 2
If hideRng Is Nothing Then
Set hideRng = Range(.Cells(i, 1), .Cells(i + rowCnt - 1, 1)) 'get range of 9 cells
Else
Set hideRng = Union(hideRng, Range(.Cells(i, 1), .Cells(i + rowCnt - 1, 1))) 'union range of 9 cells
End If
Next i
End With
hideRng.EntireRow.Select 'use this line to select rows
'hideRng.EntireRow.Hidden = True 'use this line to hide rows
'hideRng.EntireRow.Delete 'use this line to delete rows
End Sub

VBA code to transpose one multiple data column into several columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Hello I have a list of 65000 rows of stocks price for one column, and I would like to adapt it like in the second pic, anyone has any idea how to code it with vba ? Thank you!
Assuming that the data structure is always the same (12 months of data and 3 rows for data set id's). Change Sheet Name on Code Line 9 to suit your Sheet Name
Sub TRANSPOSE_DATA()
Dim lRow, i, x As Long
'------------------
With ThisWorkbook
'ADD OUTPUT WORKSHEET
.Sheets.Add After:=.Sheets(.Sheets.Count) 'add a sheet for output
.ActiveSheet.Name = "OUTPUT" 'sheet rename
'COPY DATA
With .Sheets("Hoja1") 'Change sheet name for yours
lRow = .Range("A1048576").End(xlUp).Row 'last row definition
.Range("A1:B15").Copy Destination:=ThisWorkbook.Sheets("OUTPUT").Range("A1") 'First dataset copy (including headers)
x = 3 'first iteration column definition
For i = 16 To lRow - 14
.Range("B" & i & ":B" & i + 14).Copy Destination:=ThisWorkbook.Sheets("OUTPUT").Cells(1, x) 'copy data from each iteration
x = x + 1 'transpose 1 column for next iteration
i = i + 14 'transpose 12 months plus header rows for next iteration
Next i
End With
End With
End Sub

Split and color the duplicate row value if the defect status is ready to retest, using VBA macro [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Rewording based on the Comment.
1.If the status in F column = ready to retest or passed
2.then if there are any values in Column C, take/split the Duplicate ID in column C separated with comma(,)
3.search the duplicate id in Column A and mark it with Green color
ex. In Row 1 for the defect ID JIRA1 there are 2 duplicate id ALM3 and ALM7. so i need to look for those values in Column A. 4.If the status of those defects (ALM3 and ALM7) is NOT closed then i need to mark the entire row in Green color
Sub findduplicateColoreIt()
'Get the last row
Dim Report As Worksheet
Dim i As Integer, j As Integer
Dim lastRow As Integer
Set Report = Excel.Worksheets("Sheet2")
lastRow = Report.UsedRange.Rows.Count
Application.ScreenUpdating = False
For i = 2 To lastRow
For j = 2 To lastRow
If Report.Cells(i, 4).Value <> "" And Report.Cells(i, 7).Value = "Ready to retest" And Report.Cells(i, 1).Value = "Jira" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(j, 2).Value, Report.Cells(i, 3).Value, vbTextCompare) > 0 Then
' need to get a logic where i need to get value from Colum D, split it and find the value in column A and color the row with green/any.
a = Split(ActiveCell.Value, ",")
Exit For
Else
End If
End If
Next j
Next i
I am interpreting your comments to mean:
Column I contains information regarding the current status. It will have certain key words such as "Closed", "New", "blocked", "Open". Now, you have a new key word "Passed" (I am guessing).
Each key word will have a corresponding color that is used to highlight some of the cells in the same row. In the case of the new key word "Passed", you want the color to be Green.
And I am interpreting your question to be: How do I modify the code I already have, to make the cell colors green when I find the key word "Passed"?
Your code, modified to address that question, is below.
Private Sub Worksheet_Activate()
Dim rng As Range, cell As Range
Set rng = Range("I2:I250")
For Each cell In rng
Select Case cell.Offset(0, 0).Value
Case "Closed"
cell.Resize(1, 12).Interior.ColorIndex = 4
Case "New"
cell.Resize(1, 12).Interior.ColorIndex = 31
Case "blocked"
cell.Resize(1, 1).Interior.ColorIndex = 50
Case "open"
cell.Resize(1, 1).Interior.ColorIndex = 27
Case "Passed"
cell.Resize(1, 1).Interior.ColorIndex = 4
Case Else
cell.Resize(1, 1).Interior.ColorIndex = 3
End Select
Next
' if the "Winner" Defect passed retest, now "Loser" can retest which should be green in color , i havee multiple Losed defect id associated to one id separated with (,), so if Winner is passed i need to make all loser to green color
End Sub
I choose the number 4 to represent green based on the default color palette as documented by MS. Since you already are using the number 4 for "Closed", you may already be highlighting with green, or you may be using a customized color palette.

VLOOKUP through dates and copying the values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table in Prices worksheet which consists of prices and dates. I am trying to use VLOOKUP with VBA to look for prices pertaining to the month of January and then copying these prices onto another worksheet. But because the dates are in DATE format, I am stuck with the VBA code.
Eg.
Column A shows the dates
Column B shows the prices.
There maybe prices shown on different dates e.g. 01/01/2014 or 02/01/2011 etc
I wanted to copy the prices for January.
You can use this, but it is only taking into account the prices that are for January. It's easy enough to make it work for all the months, but I'm not sure how you would want to lay that out. This grabs all January, no matter what the year. If you wanted to add the year as well, you'd just nest another IF statement. Make the first IF YEAR, and the second Month.
Sub MonthFromDate()
Dim tempMonth As Long
Dim tempDate As Date
Dim lastSourceRow As Long, tRow As Long
Dim source As String, target As String
source = "Prices" 'Source Sheet Name is set here
target = "Annual Prices" 'Target Sheet
tRow = Sheets(target).Range("A" & Rows.count).End(xlUp).row + 1
lastSourceRow = Sheets(source).Range("A" & Rows.count).End(xlUp).row
For lRow = 2 To lastSourceRow 'Start looping through source sheet at Row 2 for Headers
tempDate = Sheets(source).Cells(lRow, 1)
tempMonth = Month(tempDate)
If tempMonth = 1 Then 'This is where you would insert a VARIABLE for 1.
Sheets(target).Cells(tRow, 1).Value = Sheets(source).Cells(lRow, 1).Value
Sheets(target).Cells(tRow, 2).Value = Sheets(source).Cells(lRow, 2).Value
tRow = tRow + 1
End If
Next lRow
End Sub

Is it possible to write a VBA code that removes duplicates from only cells that are highlighted? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I would like to only remove duplicates of cells that are highlighted (in yellow). I was wondering if it is possible to do that.
Say we have a column of values, some of which are hi-lighted in yellow. Say the column contains duplicates. For example:
This macro will examine the yellow cells and remove duplicates:
Sub TheKingInYellow()
Dim N As Long, i As Long, wf As WorksheetFunction
N = Cells(Rows.Count, 1).End(xlUp).Row
Set wf = Application.WorksheetFunction
For i = N To 2 Step -1
With Cells(i, 1)
v = .Value
c = .Interior.ColorIndex
If wf.CountIf(Range(Cells(i - 1, 1), Cells(1, 1)), v) > 0 And c = 6 Then
.Delete
End If
End With
Next i
End Sub
Here is the result:
Because you didn't specify what range you want to effect, I wrote this so you have to select the data first and then run the code.
It loops through each cell in the selection and checks the background color. While there are many shades of yellow, Interior.ColorIndex = 6 is the common preset most people go with. Any rows with that glorious "highlighter" yellow get deleted :)
Sub NoYellow()
Dim r As Range
For Each r In Selection
If r.Interior.ColorIndex = 6 Then r.EntireRow.Delete
Next
End Sub