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 7 years ago.
Improve this question
I am Working with many Excel files. The first column is fixed with the heading product-name. The other column name is price.
I am trying to merge the columns that are between these two columns, Each file contains different number of columns between these two columns product-name and price
I am having trouble writing a macro to run each Excel file, can anyone show me an example?
I am assuming that only 1 st row need to be checked. assuming these are headers.
You can use the below VBA to check the the column headers and merge the in between columns
I have used random variables, you may change accordingly.
Sub test()
Cells(1, 1).Select
j = 1
Do
k = Cells(1, j).Value
Cells(1, j).Select
j = j + 1
Loop Until (k = "Product-name")
c1 = j
Do
k = Cells(1, j).Value
Cells(1, j).Select
j = j + 1
Loop Until (k = "Price")
c2 = j - 2
If (c2 > c1) Then
Range(Columns(c1), Columns(c2)).Select
Selection.Merge
End If
End Sub
k variable will store the column value and will be compared to "Product-name" and "Price".
c1 will store the next column number of the "Product-name"
c2 will store the column number of the "Price" -1, in logic it advances by 2 so subtracting the column number with 2.
Now you have column numbers in between both the headers.
Here there is small scenario which need to be considered, if there are no columns or if there is at least one column between these 2 columns, in that case directly you cannot merge columns. so additional comparison of column values.
I hope this answers your query. The code is tested and is working fine.
Related
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 4 years ago.
Improve this question
I'm trying to count the total number of cells that have values for the row that I'll be looking up using their name. I have a different sheet for looking up the value I tried COUNTA and VLOOKUP. Is there any way to combine these two so I'll end up with the correct result?
Please see screenshot.
Thank you!
Use INDEX(,MATCH()):
=COUNTA(INDEX('Sheet1'!C:X,MATCH("Jessel Rayes",'Sheet1'!A:A,0),0))
Here is a vba function that looks up the value you define (first input) in a specified range (second input) and then returns the number of empty cells in the same row right from this cell for a specified amount of columns (third input).
Function TLookupT(Value As Variant, arr As Range, column As Long)
x = 0
For Each Cell In arr
If Cell.Value = Value Then
For i = 1 To column - 1
If Cell.Offset(0, i) = "" Then
x = x + 1
End If
Next i
End If
Next Cell
TLookupT = x
End Function
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 have got two sheets, one with data like this:
For one person there can be couple of rows with answers.
And second sheet with this data:
Here one person had only one row with answers.
Based on the surnames from second table I want to search the person in first table and if the person is present copy the whole row to another sheet. So my final output table will looks like this:
My idea is that the algorithm should take the surname from table in Data 2 sheet on look for it in Data 1 sheet if is present then copy whole row into A3 Output sheet, next search the rest of Data 2 for another appearance. If blank cell then take another surname from Data 2 table and do it up to the point where in Data 2 table blank space is detected. But I have no idea how to translate it into VBA code.
Could anyone help with some clues? Or macro that I can use here?
I would be really thankful for any help.
Here's a VBA subroutine that I believe will do what you asked, not sure if it is what you want. This is assuming you already have a tab to receive the data to be copied; the tabs being used are MRWV1 = names to select, MRWV2 = Data sheet of rows to copy and MRWV3 = sheet to receive the copied data.
Sub MRWV()
'
' MRWV Macro
'
' Housekeeping
vFoundKt = 1
vSourceRows = 0
vDataRows = 0
' Select the sheet with the five rows with the names to select
Sheets("MRWV1").Select
vSourceRows = Cells(Rows.Count, 1).End(xlUp).Row
Sheets("MRWV2").Select
vDataRows = Cells(Rows.Count, 1).End(xlUp).Row
'Get names from source sheet
For iSource = 2 To vSourceRows
Sheets("MRWV1").Select
Range("A" & iSource).Select
vSourceName1 = ActiveCell.Value
Range("B" & iSource).Select
vSourceName2 = ActiveCell.Value
'Look through data sheet for matching names
For iData = 2 To vDataRows
Sheets("MRWV2").Select
Range("A" & iData).Select
vDataName1 = ActiveCell.Value
Range("B" & iData).Select
vDataName2 = ActiveCell.Value
If vSourceName1 = vDataName1 And vSourceName2 = vDataName2 Then
vFoundKt = vFoundKt + 1
Range("A" & iData & ":I" & iData).Select
Selection.Copy
Sheets("MRWV3").Select
Range("C" & vFoundKt).Select
Selection.PasteSpecial Paste:=xlPasteValues,Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Next
Next
End Sub
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
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
How to create a combination generator where order does not matter but being limited from a specific range of Sum? Using Excel VBA macro. Pls help ive been trying to solve this for years not really good at excel vba.. need actual precise codes..
Here is a very simple example that uses an incrementing binary pattern to generate combinations of a set of items.
The items can be either numbers or text values. I am using column B as a "helper column" to hold the binary pattern, but an array could be substituted.Place your items in column A and run this short macro:
Sub Generate()
Dim i As Long, s As String
Dim j As Long, K As Long, N As Long
Dim wf As WorksheetFunction
Dim answer As String
Set wf = Application.WorksheetFunction
K = 1
N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To (2 ^ N) - 1
s = wf.Dec2Bin(i, N)
For j = 1 To N
Cells(j, 2).Value = Val(Mid(s, j, 1))
Next j
answer = ""
For j = 1 To N
If Cells(j, 2) = 1 Then answer = answer & "," & Cells(j, 1)
Next j
Cells(K, 3) = Mid(answer, 2)
K = K + 1
Next i
End Sub
For example:
Because there are (2^N)-1 combinations for N items, there is a practical limit to the number of items that can be placed in column A.
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 7 years ago.
Improve this question
I cannot figure out how to do a loop within a loop.
There is a list of words on sheet 1 that need to be copied and pasted if they match up with any of the 20 desired key words on sheet two, column 1.
This then needs to be copy pasted onto sheet 3. Then I need to look at the same list from sheet 1 and copy paste those onto sheet 4 if they match up with any of the key words from sheet 2, column 2. I could use any help.
Single loop
Dim i As Integer
For i = 1 To 6
Cells(i, 1).Value = 100
Next i
Double Loop
Dim i As Integer,
Dim j As Integer
For i = 1 To 6
For j = 1 To 2
Cells(i, j).Value = 100
Next j
Next i
Good Luck
You don't need 2 loops (which are slow)
Loop through the values in sheet 1 testing if they exists using this:
If WorksheetFunction.CountIf(Sheets("Sheet2").Range("A1:A20"), Range("A1")) > 0 Then
'Your copy and paste code goes here for sheet 3
ElseIf WorksheetFunction.CountIf(Sheets("Sheet2").Range("B1:B20"), Range("A1")) > 0 Then
'Your copy and paste code goes here for sheet 4
End If